当前位置:网站首页>Static keyword and inheritance, polymorphic and special classes

Static keyword and inheritance, polymorphic and special classes

2022-06-12 04:59:00 なんでもないゃ

static keyword : use static Modifiers represent methods and variables that belong to a class ; Non static member variables cannot be used in static methods , Static variables can be used in non static methods , You can also use non static variables , This is because , Static variables and methods are loaded as classes are loaded , The common constructor and variables are loaded when the object is created , Generally, the class is loaded first and then the object is loaded ; Static member variables are common , Loaded in the method area , Normal member variables are in the heap ;

Loading order : Static code block > Code block > Construction method ; Parent static code block > Subclass static code block > Parent code block > Parent class constructor > Subclass code block > Subclass construction method ;

Constructor and member methods : The constructor has the same name as the class , The constructor does not return a value , Member methods do not have the same name as the class , To write the return value ;

Singleton design pattern : A class provides only one object ; technological process :1、 Privatized construction method , use private modification ;2、 Declare the object of this class in this class , use private static modification ;3、 Write the get Method returns the object , use public modification ;

Slacker type : The second step is to create the object when declaring the object ;

Hungry Chinese style : The second step is to declare only objects , The third step is get Method , That is, create the object when calling ( recommend );

final A decorated class indicates that it cannot be inherited ,final A modifier means that the method cannot be overridden ,final Decorating a member variable means that the variable must be initialized and its value cannot be changed before use , Usually use public static final Modifier variables represent constants , Use capital letters to indicate , Underline between letters _ Connect ;

polymorphic : Many forms of the same thing ; Parent type Object name = new Subclass type (); This object can call methods unique to the parent class , You cannot call methods unique to subclasses ; When a subclass overrides a method in the parent class , Methods in the parent class are called during compilation , The runtime calls methods in subclasses , If the method is static , Methods in the parent class are called both during compilation and operation ;

Polymorphism means : Shielding the differences of different subclasses and realizing general programming will bring different effects ;

原网站

版权声明
本文为[なんでもないゃ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010622133650.html