Class Head { Head (){ System. out. println ("head "); } } Class Body { Body (){ System. out. println ("body "); } } Class Person () { Head h = null; Body B = null; Person () // a Person is composed of the Head and Body. The object of the Head and Body is part of the Person. { H = new Head (); B = new Body (); } } Inheritance, as one of the three important features of object-oriented, plays an important role in the Object-Oriented field. It seems that I have not heard of any object-oriented language that does not support inheritance. Class Person { Private String name = null; Private int age = 0; Public Person (String n, int) { Name = n; Age =; } Int getAge () { Return age; } String getName () { Return name; } Void getDescription () { System. out. println ("name:" + name + "/t" + "age:" + age ); } } Class Student extends Person { Private String studno = null; Public Student (String n, String no, int) { Super (n, ); Studno = no; } } Note: The Student class has three member variables: name, age, studno, And the getDescription () method (); Note: The subclass inherits all the variables and functions of the parent class. The subclass cannot access the private variables and functions of the parent class. Both combination and inheritance allow us to place sub-objects in our new class. You may wonder the differences between the two and how to choose them. Car objects are a good example: Class Engine { Public void start (){} Public void rev (){} Public void stop (){} } Class Wheel { Public void inflate (int psi ){} } Class Window { Public void rollup (){} Public void rolldown (){} } Class door { Public window = new window (); Public void open (){} Public void close (){} } Public class car { Public engine = new engine (); Public wheel [] wheel = new wheel [4]; Public door left = new door (), Right = new door (); // 2-door Car (){ For (INT I = 0; I <4; I ++) Wheel [I] = new wheel (); } Public static void main (string [] ARGs ){ Car car = new car (); Car. Left. Window. rollup (); Car. wheel [0]. Inflate (72 ); } }///:~ Because automobile assembly is a factor that needs to be considered in Fault Analysis (not just a simple part of the basic design), it helps the customer programmers understand how to use classes, in addition, the programming complexity of class creators is greatly reduced. If you select inheritance, You need to obtain a ready-made class and create a special version of it. Generally, this means that we are going to use a class for general purposes and customize it according to specific needs. Just imagine that you cannot combine a car with a vehicle object. -- An automobile does not "include" a vehicle. On the contrary, it "belongs to" a type of vehicle. The "belonging" relationship is expressed by inheritance, while the "include" relationship is expressed by combination. Protected Now that we have understood the concept of inheritance, the keyword protected finally makes sense. Under ideal conditions, Private Members are "private" at any time and cannot be accessed by anyone. However, in practical applications, we often want to hide some things deeply, but at the same time allow access to members of the category class. The protected keyword helps us do this. It means "it is private, but it can be accessed by anything inherited from this class or anything in the same package ". That is to say, protected in Java will become "friendly. The best practice we take is to maintain the private status of Members-in any case, the right to modify the basic implementation details should be retained. On this premise, you can use the protected method to allow the class's successors to Perform controlled access: Import java. util .*; Class villain { Private int I; Protected int read () {return I ;} Protected void set (int ii) {I = II ;} Public villain (int ii) {I = II ;} Public int value (INT m ){ Return M * I; } } Public class Orc extends villain { Private Int J; Public ORC (int jj ){ Super (jj ); J = JJ; } Public void change (int x ){ Set (x ); } }///:~ As you can see, change () has the permission to access set () because its attribute is protected (protected ).
Synthesis and inheritance In object-oriented programming, one of the most likely ways to create and use code is to encapsulate data and methods into a class and use the class objects. Sometimes, you need to use the "Combination" technique to construct new classes with ready-made classes. Inheritance is the least common practice. Therefore, although inheritance has been heavily emphasized in the process of learning OOP, it does not mean that it should be used everywhere as much as possible. On the contrary, exercise caution when using it. It can be considered only when it is clear that inheritance is the most effective in all methods. To determine whether a combination or inheritance should be selected, the simplest way is to consider whether the new class needs to be traced back to the basic class. If it must be traced back, it must be inherited. However, if you do not need to roll back the style, you should remind yourself to prevent the abuse of inheritance. However, as long as you remember to always ask yourself "Do I really need to roll back the shape?", the choice of combination or inheritance should not be too big. |