In fact, the two methods of implementation are often heard in Java--combination and inheritance.
(1) Combination
The role of Has-a.
1 Public class TV {2 Show Show; 3 Public String toString () {4 return "Showgirl"; 5 }6}7 8class show{9 }
View Code
To mention the ToString method, when you need a string and you are an object, the compiler invokes the object's ToString method.
TV has show, now show is not initialized, is NULL, cannot call show method.
A combination of powerful, object-oriented look, if you are building a car class, then you can use the combination of glass,light,engine and so on the car these parts together.
(2) Inheritance
Is-a
1 PackageCom.myown.iaiti;2 3 Public classFather {4 Public inti;5 voidget () {6System.out.println ("Father");7 }8 }9 Ten PackageSon; One Importcom.myown.iaiti.*; A - Public classSonextendsfather{ -Father f =NewFather (); theintj =f.i; -Son son =NewSon (); - son.get (); - } + - Public voidget () { +Super. get (); ASystem.out.println ("Son"); at } - } -View Code
There is a packet access problem, if there is no public, the default is within the package member access, different package access, that is, son of the father member access to the Get method is not visible. And the public words are visible, so I access to get.
The private part is not inherited, belongs to the parent class private, and the public part, will inherit, need to modify the method, can be rewritten. The attributes to be added can be added separately.
and inherited methods, if the original Father public method is not added after the public, there will be cannot reduce the visibility of the inherited method from Father, That is, you cannot reduce the visibility of inherited methods in the parent class. Super refers to the parent class, which is father.
Also, in fact, all classes in Java implicitly inherit the object class. Object is a parent class, other classes are subclasses
Foreigners like to speak as a base class. Subclasses are also called export classes or derived classes.
(3) Agent
There is a more difficult to understand in the design mode-proxy mode, the author is very interesting, the agent is the combination and inheritance of the mean.
1 PackageSon;2 classfather{3 Public voidget () {4System.out.println ("Father");5 }6 }7 Public classSonextendsfather{8 Public Static voidMain (string[] args) {9Father f =NewFather ();Ten f.get (); One } A } - - classfatherproxy{ the PrivateFather f =NewFather (); - Public voidget () { - f.get (); - } +}View Code
Like direct father as a member, then the Father method is exposed to this class, then we can use fatherproxy such proxy class, I myself define how get method is to take, I know is to call Father Get method, But the person who uses my agent does not know, I only tell him that you need to use the proxy get method on it. Encapsulation is the embodiment of the. The above is just a simple example of knocking at random.
(4) Comparison
| Combinatorial Relationships |
Inheritance Relationship |
| Pros: Do not break the package, the whole class and the local class loosely coupled, relatively independent of each other |
Cons: Break encapsulation, the child class is tightly coupled with the parent class, the subclass relies on the implementation of the parent class, the subclass lacks the independence |
| Advantages: Good Scalability |
Cons: Supports scaling, but often at the expense of increasing the complexity of the system structure |
| Pros: Support dynamic combination. At run time, the whole object can select different types of local objects |
Disadvantage: Dynamic inheritance is not supported. Subclasses cannot select different parent classes at run time |
| Advantage: The whole class can wrap the local class, encapsulate the interface of the local class, and provide the new interface |
Disadvantage: Subclasses cannot change the interface of the parent class |
| Disadvantage: The whole class cannot automatically get the same interface as the local class |
Advantage: Subclasses can automatically inherit the interface of the parent class |
| Disadvantage: When you create an object of a whole class, you need to create an object of all local classes |
Advantage: You do not need to create an object of the parent class when you create an object of the subclass |
Reference documents:
38438403
53883966
The Java Foundation Reuse class