7 Reuse Class
7.1 Combination
That is, using another class as a member variable in one class, which is the function of reusing the existing program code, not the form.
7.2 Inheritance
- Keywords: extends, this reuse is a form of reuse, is a scalable and limited reuse;
- Reuse: Automatically gets all public (protected) members and methods of the base class, if the members and methods of the base class do not have access qualifiers, then the infinite method and members of the base class can be accessed in the subclass of the same package, not in the other packages (access to the package);
- initializes the base class: The constructor of the parent class is called before the subclass constructor can be accessed, and if the parent class has a parent class, it will continue to search for and invoke the constructor, and if the parent class constructor is not explicitly called, the default lunch constructor for the parent class is called, and if the parent class does not have a default constructor, will not compile, it is necessary to display a non-default constructor in the subclass constructor that invokes the parent class;
1 PackageCom.chenlei.study.thinkinginjava;2 3 Import Staticcom.chenlei.study.thinkinginjava.print.*;4 5 Public classClassconstructor {6 7 Public Static voidMain (string[] args) {8 NewC ();9 }Ten One } A - classa{ - PublicA () { thePrint ("A constructor"); - } - } - + - classBextendsa{ + Private intFlag; A PublicBintflag) { at This. Flag =Flag; -Print ("B constructor"); - } - } - - classCextendsb{ in PublicC () { - Super(0);//If you do not specify which parent class the constructor will fail to compile, it is obvious that the parent class constructor will be called before the base class constructor is called to } + } - /** the * Output: * * A Constructor $ * B ConstructorPanax Notoginseng */
7.3 Agents
- Java does not directly support proxies;
- If you want to reuse a class, using a member variable combination or using inheritance will expose all accessible methods of the reused class, which can be combined with inheritance and combination, using "proxy" to implement the "inheritance" of some methods;
- The boss's secretary is an agent, want to know the boss's meaning, can not "inherit" from the boss, can not be the boss as a "member variable", then only through the Secretary to convey the notice, request, etc., so that employees know what to know, shielding should not know;
1 PackageCom.chenlei.study.thinkinginjava;2 3 Public classProxy {4 Public Static voidMain (string[] args) {5Bossproxy proxy =Newbossproxy ();6 Print.print (Proxy.getemployeenum ());7 }8 }9 Ten Abstract classcompanymember{ One Private intAge ; A PrivateString name; - Public intGetage () { - returnAge ; the } - Public voidSetage (intAge ) { - This. Age =Age ; - } + PublicString GetName () { - returnname; + } A Public voidsetName (String name) { at This. Name =name; - } - } - - classBossextendscompanymember{ - Private DoubleMoney//can't let employees know in Private intEmployeenum; - Private intWifenum;//can't let employees know to +Boss (DoubleMoneyintEmployeenum,intwifenum) { - This. Money =Money ; the This. Employeenum =Employeenum; * This. Wifenum =Wifenum; $ }Panax Notoginseng - Public DoubleGetmoney () { the returnMoney ; + } A Public voidSetmoney (DoubleMoney ) { the This. Money =Money ; + } - Public intGetemployeenum () { $ returnEmployeenum; $ } - Public voidSetemployeenum (intemployeenum) { - This. Employeenum =Employeenum; the } - Public intGetwifenum () {Wuyi returnWifenum; the } - Public voidSetwifenum (intwifenum) { Wu This. Wifenum =Wifenum; - } About } $ - classBossproxyextendscompanymember{ - Privateboss boss; - PublicBossproxy () { A if(Boss = =NULL){ +Boss =NewBoss (1000d,100,3); the } - } $ Public intGetemployeenum () { the returnboss.getemployeenum (); the } the}
This shows that the agent let some of the exposed exposure, should not be exposed to hide, which seems to be very useful in the management of rights. Although I have not seen the proxy mode in design mode, I think I can wrap some methods and actions before and after the proxy method to do specific processing, such as SPRINGAOP, spring transaction agent? Or is it possible to ensure security in an atomic way when implementing a synchronous lock? Lazy loading (Spring injection)?
7.4 Overloading in base classes
The method of overloading the parent class in the base class is possible, as long as the overloaded method of the base class satisfies the method name and some methods of the parent class, but the number, order, type, or return type of the parameter list is different, it can be overloaded normally, if the method name and return type, the parameter list is identical, it will be overwritten instead of
7.5 Differences of combinations and inheritance
- Combination: Has-a
- Inheritance: is-a
7.6 Protected Keywords
- Subclass access: Inherits from the subclass of the base class, can access the protected domain of the base class;
- Package access: Classes with protected domains that are in the same packages can access the class's protected domain
7.7 Upward transformation
A common approach requires processing of one type of object, and the benefits of inheritance are apparent; just set the parameter to the base class, then any subclass of that base class can pass in as a parameter without error, because the compiler will automatically transform these subclasses into a base class without having to write a method for each subclass. Easy to manage and operate.
7.8 Final keyword
- Final base data: The final base type must be instantiated first, and once the value is given it cannot be changed; static all class objects share this data, can be changed, but this value is only one, change all, do not have to initialize, so final static must be initialized, And after initialization, all class objects cannot change this value, sharing one; the difference between private static final and public static final is that the former is called intelligently through object references, which can be called directly from the class name (different access rights);
- Final Reference object: cannot be re-assigned with the new operator, nor can it be assigned to a final object directly, but it is possible to change the value of a reference object, such as
1 PackageCom.chenlei.study.thinkinginjava;2 3 Public classFinal {4 PublicString string = "haha";5 Public FinalString astring =string;6 Public Static voidMain (string[] args) {7Final Final1 =NewFinal ();8final1.string = "123";9 System.err.println (final1.astring);Ten } One } A /** - * Output: - * haha the */
- The final data can be initialized at the time of declaration, or it can be initialized in the constructor, but must be properly initialized before use;
- Final parameter: The final parameter in the list of method parameters, the same cannot be changed by any operation in the method;
- Final method: One is to avoid inheriting the implementation of class modification method, and the second is to improve the efficiency, once the compiler discovers the final method, the method body will be directly nested in the program rather than call the method stack (if the final method is large, this increase in efficiency will be offset, The new version of the JDK has been optimized for this: When the final method body is large, it is no longer nested);
- Private Final method: Private method is not inherited, and final with no add, but the compiler does not error, because all private methods are implicitly declared final;
- Final class: The final class cannot inherit;
- The class is loaded only when it is used, and the constructor method can be considered a static method, that is, when the static member of the class is loaded, the class is loaded;
[Thinking in JAVA] multiplexing class