Chapter 6 Repeat Classes (10)

xiaoxiao2021-03-06  52

Chapter VI? Repeat the use of Classes ?? Repeating the code in the process-oriented language is just a simple copy code to achieve the purpose of repetition, and in the object-oriented Java program, the reuse is mainly reflected in 2 points? 1. Use existing classes in the new Class, which is called "combination". But this reuse is only a very simple repetition to use some code functions, rather than repeatedly using its form. ?? 2, let the new Class become a class of both Class, and add new features as needed without being able to fill the original Class, which is called "inheritance". ??

Combined syntax ???? In fact, we have used a lot in the previous example, and we only need to place the object handle in the Class is a combination!

???? Class compost ???? {????? private string s; ????? compost () ????? {?????? system.out.println ("compStion () "); ?????? s = new string (" hello "); ?????} ????? public string toString () ????? {?????? Return S ?????} ????} ???? public class test ???? {????? compostion c ;? // object handle ????? INT i; ???? amblic Void show () ????? {?????? system.out.println ("int =" i); ?????? system.out.println ("compStion =" c); ?????} ????? public static void main (string args []) ????? {?????? test t = new test (); ?????? t.show (); ?????} ????} ???? The objects of each non-basic data type have a toString () method, which is used to convert the ComPstion into a string, and other String phases. The basic data type in the Class will be initialized to the default, and the object handle will be initialized to NULL. If you want to use this handle, remember to initialize, otherwise you will be aid that the pointer is wrong! ?? Inherited ???? Inheritance is an extremely important part of the Java language, implements the keyword extends, so that automatically allows the subclass to get all the member data and functions in the parent class. All classes in Java even include yourself or the classes that will be defined are inherited from the Object class, implicit inheritance inside the compiler ???

Class Base ???? {????? I = 10; ????? public? void show () ????? {?????? system.out.println ("Base Method "); ?????} ????? public static void main (string args []) // java allows Class to have its own main () ????? {??? in the same file ??? new base (). show (); ?????} ????} ???? Class Derived Extends Base // Inherited ???? {????? public void show () / / Cover the Base function ????? {?????? system.out.println ("derived method"); ?????? super.show (); // Call the base function ?? ???} ????? public void newMethod () // Subclass newly added functions ????? {?????? system.out.println (i); // Print Base Data ?????} ????? public static void main (string args []) ????? {????? Derived D = new deerid (); ?????? d. Show (); ?????? d.newmethod (); ?????} ????} ????

BASE initialization ????? When the subclass is initialized, the system will initialize the parent class that is inherited, and the Java compiler will call the parent class's constructor before calling the subclass constructor ????

Class Base ????? {?????? Base () ?????? {??????? system.out.println ("base method"); ?????? } ??????????} ????? Class Derived Extends Base ????? {?????? Derived () ?????? {??????? // Super () ;? System will automatically join the call to the parent class ??????? system.out.println ("derived method"); ??????} ?????? public static Void main (string args [] ??????? Derived D = new derived (); ??????} ?????}

????? If your parent class is a class with quotes, the compiler does not automatically call the constructor, you must use Super to call, otherwise the system will be wrong???

?? Class Base ????? {?????? Base (string s) ?????? {??????? system.out.println (s); ?????? } ??????????} ????? Class Derived Extends Base ????? {?????? Derived () ?????? {??????? Super ("Base Method"); / / Must be the first line statement of constructor ??????? system.out.println ("derived method"); ??????} ????? Public static void main (string args []) ?????? {??????? derived d = new derived (); ??????} ?????}

???? Compatible combination and inheritance ????? Sometimes, we will not only use the combination when writing CLASS, but also inherited. ?? The choice between combination and inheritance ???? If you just want to use both CLASS functions in the new Class, it is preferred to use a combination. ???? If you want to develop a special version for the existing Class, then inherits it. ???? If you have both types and new categories (one) relationship, use inheritance, if it is a HAS A (one) relationship, use a combination? Protected ??? ? Let's review the meaning of Protexted: Inheriting the subclass of this class, you can access the members of the class, and for other classes in a package??

One of the advantages of progressive development ???? Inheritance is to support progressive development, in this development mode, you can add a new program code in the program, but does not affect the code of the parent class ?? Due to the relationship of inheritance, all functions of the parent class can be used in the subclass, and any message sent to the parent class can also be sent to the subclass ????

Class Base ???? {????? protected void show () ????? {?????? system.out.println ("base method"); ?????} ??? ?? protected void getsomene (base b) ????? {???????? b.show (); ?????} ????} ???? Class Derived Extends Base ???? {????? public static void main (string args []) ????? {?????? derived d = new derived (); ?????? d.getsomeone (d);? ????} ????} ???? Note that in GetSomene's function definition, we define the handle of him only accepts an Base class, but he actually accepted the handle of the subclass. Because the subclass is not very the same as the parent class, he is a function of the parent class, which is also applicable to the parent class. Of course, it also applies to subclavab. We call this kind of subclass into the parent class is called up. Transformation Upcasting ????

Class Base ???? {????? public void method () ????? {?????? system.out.println ("Base.Methodone"); ?????} ?? ??? public void methodTwo () ????? {?????? system.out.println ("base.methodtwo"); ?????} ????} ?????? ?? Class Derived? EXTENDS BASE ???? {????? public void method () ????? {?????? system.out.println ("derived.methodone");??? ??} ????? public void methodTwo () ????? {?????? system.out.println ("derived.methodtwo"); ?????} ????? public Void methodnew () ????? {?????? system.out.println ("derived.methodnew"); ?????} ????? public static void main (String [] args ????? {?????? base d = new derived (); ?????? d.Methodone (); // Although the transformation has become Base, the function called the base or base class Function, show derived.methodone ?????? D.MethodTwo (); // 同 ??????? // D.MethodNew (); because of the upward transformation, the sub-class unique function is lost ?? ???} ????} ???? Why need you need to transform up? ?????? Subclass is a supercharge of the parent class, so at least there is at least the function in the parent class in the subclass, and may be more. However, the upward transformation will make the subclass loss and the parent class, and the up transformation must be safe because it changes from a special type to a common type. ???? combination VS inheritance ?????? When you need to transition, it is the best time to use inheritance ??

Keyword finAl ???? What is Final? It is the meaning of "final", that is, the meaning of can't change, we are here discussed 3 Final: Data, Method, Class ????

Final Data ?????? Final data is fixed, the constant data is constant, which is very useful because it ?????? 1, can be the compile period of never change constant. Compiling period constants can perform certain computations in compile periods, reduce the burden on the execution period, such constants must be the basic data type, use Final modification, the initial value must be given. ?????? 2, can be initialized during the execution period, but you don't want to change him again. ?????? If a data has both static, there is Final, then he will have a storage space that cannot be changed. When Final is used for objects, Final makes the handle unchanged that it cannot refore to other objects, but the object itself can be changed, this and final data type cannot change its value. Different. ????

?? Class value ?????? {??????? I = 1; ??????} ?????? Class FinalData ?????? {????? ?? // Compile period constant ??????? final int i = 10; ??????? static final INT II = 20; ??????? // Typical constant ???? ??? public static final int III = 30; ??????? // execution period constant ??????? final int IIII = (int) () * 20);??? ???? static final int Iiiii = (int) * 20); ?????????????? Value v = new? value (); ????? ?? final value vv = new value (); ??????? static factory value vvv = new value (); ??????? // array ??????? final int [] a = {1, 2, 3, 4, 5, 6}; ?????????????? public void show (String ID) ??????? {?????? ?? System.out.println (ID ":" "iiii =" iiii ", IIII =" IIII); ???????} ??????? public static void main (String [ ] args) ??????? {???????? finalData fd = new finalData (); ????????? // fd.i ;? Cannot change the value ????? ??? fd.vv.i ; // final object can change its object value ???????? fd.v = new value (); // Non-final ???????? for (int i = 0; i ????????? fd.a [i] ;

/ / Final object can change its value ???????? // fd.vv = new value (); // final object's handle cannot change the object it referenced ???????? // fd .vvv = new value (); // final object's handle does not change the objects of its reference ???????? // fd.a = new int [3]; / / Final object's handle cannot change its reference Object ?????????????????????????? fd.show ("fd"); ???????? System.out.Println ("Creat New FinalData");?? ?????? finaldata nfd = new finaldata (); ???????? fd.show ("fd"); ???????? nfd.show ("NFD"); ?? ?????} ??????}

?????? / * Display FD: IIII = 2, IIII = 3 ?????? Creat New FinalData ?????? FD: IIII = 2, IIII = 3 ?????? NFD: IIII = 1, IIII = 3 * / where IIII is non-STIC, each time the value is different. Static's Final data will only be initialized when loading, and will then be initialized once each time a new object is generated ?????

Blank Final ?????? Final simultaneously declares that the data member is final, but does not initialize, however, you must ensure that the data member is initialized before using the member data. The advantage of doing this is to ensure the constant diligence of the data, and can also produce different FINAL data depending on the different objects, with greater flexibility. ?????? class finaltest ?????? {??????? final string s; ??????? finaltest (string s) ?????? {???? ???? this.s = s; ???????? system.out.println (this.s); ???????} ??????? public static void main (String [] args) ??????? {???????? new factorytest ("Hello"); ???????? New factoryTest ("bye"); ?????? ?} ??????} ??????

Final quotes ???????? When the core of final is the basic data type, its value is not allowed to change. When it is an object, the pointing point of the handle is not allowed. ????? final function ?????? The meaning of using the final function is two: ??????????????? 1, lock the function, not allowing the subclass to change, Isn't it possible to overwrite ??????????????? 2, let the short function increase the execution efficiency ??????

Final and private ???????? Class's private function is actually a natural Final function, because you can't use the private function, of course, you can't overwrite the function, and when you try to override him, actually It is a new function that adds Final to the private function, does not have any meaning! ????

Final Class ????? When you are exact hope that the class cannot be inherited, you need to use Final, but whether the Class is final, the data member can be fina or not? Initialization and Class loading ???? Java will only load related classes in Java, which is generally the initial use and inheritance of CLASS, the so-called initial use, not just when the object is generated, it is also possible. Is a Static function or when the data member is used. When this time point of the Class is used, when Static is initialized, when any Static's Class member is loaded, it will initialize according to the order they appear, and only once initialization will only be initialized. ???? Inherited and initialization ?????? The following program shows the order of inheritance and loading ??????

Class Base ?????? {??????? INT i = 10; ??????? INT J; ??????? Base () // 6, execute the base constructor? ?????? {???????? show ("i =" i ", j =" j); ???????? j = 20; ?????? ?} ??????? static int m = show ("static base.m init"); // 2, perform static members' initialization to load ??????? public static int show (String S) ??????? {???????? system.out.println (s); ????????} ???????} ?????? } ???????????? Class Derived? EXTENDS BASE ?????? {??????? INT? K = show ("derived.kinit"); // 7, Initialization of basic data type ??????? derived () // 8, has completed the initialization action of the data member, start execution function ??????? {???????? show (" K = " k); ???????? show (" j = " j); ???????} ??????? static int n = show (" static deerived. n init "); // 3, perform static member initialization to load ??????? public static void main (String [] args) // 1, enabled Derived.main () static method, Class Derived is used Causes the loader to start, find Extends, so loaded Base ??????? {???????? show ("derived constructor"); // 4, load, start executive ???? ???? Derived D = new deerived (); // 5, starting to generate objects, due to Extends, first produce BASE objects ???????} ??????} ?????? Output / * static base.m init ???????? static derived.n init ???????? derived constructor ???????? i = 10, j = 0 ???? ???? derived.kinit ???????? k = 30? ??????? j = 20 * / ??

Summary ???? Inheritance and combination allow you to generate new classes based on the already class. Use a combination to repeat it to have a class to make it a part of the new class; inheritance is used for the multiplexing of the interface. Since the subclass inherits the parent class interface, it can be transformed upwards, which is extremely important. . It is recommended to use a combination, combined with elastic, inherited is flexible. ?????

Question answer ???? In fact, there is no difficulty of this chapter, all of which is the consolidation exercise of knowledge points. Everyone can do it seriously, it can be done, and will not pay attention to reading! ?? Next chapter, my notes will write to object-oriented essence - polymorphism! Thank you for your attention and support!

转载请注明原文地址:https://www.9cbs.com/read-117089.html

New Post(0)