Category and inheritance said from a grammatical perspective, there is nothing difficult. Class, is the basic unit of Java, but also an embodiment of the OOP language. Starting from C , one or two chapters of each book are very cumbersome to introduce object-oriented knowledge, honestly say that I have never seen it carefully, but I remember a word, all things are all objects. OK, I think the understanding of the class is so good. Thus, I think of the class as a box, then put some thinking in a class of things, I will think that you think is the same class (or implement a function of a function in the program) All in this box, then the domain (variable) is as private, and the method of reading and writing through the public method (function), this is ok. For a simple story about classes, let's introduce the initialization process of classes.
If a class does not have a parent class, then the order in which the load should be first static (static initialization), because static initialization When a class first new object, first allocate memory, then immediately initialize static things, we should know STATIC has only one copy in all objects in the same class (I don't know if this is not suitable, ^ _ ^, I want to explain that static is only one place in memory, not every object. Different memory spaces exist, that is, all objects use the same Static domain, inevitably, we need to initialize Static, because non-Static Domains After the NEW object, use different memory spaces To assign it), next is the class (non-static) initialization statement, and finally the constructor is constructed. If you don't understand it, you can remember it, because the Java compiler is doing this. I briefly explain, because the constructor is sometimes called to the class itself (member function), and the method may directly use the initialization value of these non-static domains, then if the constructor is in the initialization statement, it will inevitably lead to operation results. Incorrect. Here is an example: Class INSECT {// 1) INT I = 9; INT J; // 2) INSECT () {PRT ("i =" i ", j =" j); // class Insect's constructor INSECT () uses I, J; the result of the output here should be 9; j = 39;} // 3) static int x1 = PRT ("static insect.x1 initialized"; static int INT PRT (String s) {system.out.println (s); return 47;}} // beetle is an Insect subclass
Public class beetle extends INSECT {// 4) INT K = PRT ("Beetle.k Initialized"); // 5) Beetle () {PRT ("k =" K); PRT ("j =" j) } // 6) static int x2 = prt ("static beetle.x2 initialized"); static int prin (String s) {system.out.println (s); return 63;} public static void main (String [] Args) {PRT ("Beetle Constructor"); beetle b = new beetle ();}} The above example also includes a subclass of a parent class, a little bit troublesome when the initialization is loaded, but the overall principle is the same . According to my number, the order in which the load is: 3, 6, 1, 2, 4, 5. When loading the Beetle class, let him load in Beetle.class and load him in Beetle.class, finding that he has a parent insect.class, then you need to put the inct.class to load, And first initialize him, then (3) is first initialized, then after the parent class is loaded, the subclass is then loaded, so that (6) is then initialized. When new beetle (), the memory is allocated, then the non-static initialization of the parent class, then the parent class constructor, so (1) (2) is initialized, then the subclass (4) (5). Basically, the order of initialization is the first parent class, first static, and then non-static, and finally the constructor. Question of parent classes, subclasses: Do not compile, you predict output. Then verify it again.