Document A small problem that has recently been encountered in project design.
Premise: There are two Pojo classes that can set their property values in a chained invocation, where one class inherits from another class.
Problem: When a subclass object accesses a parent class method through a chained call, how can the returned object still be a subclass object, and still be able to continue the chaining method of calling subclasses?
Conclusion: Subclasses override the method that needs to be called in the parent class. In the method overridden by subclasses, the parent class method is first called through the Super keyword.
The child class object is then returned by the return this statement.
For a more specific, more descriptive description of the problem and workaround, on the example code.
Baseoption, Appearanceoption are two Pojo classes that implement chained calls, where Appearanceoption inherits from Baseoption.
1 Packagecom.practice.option;2 3 Public classbaseoption {4 5 PrivateString ID;6 7 PrivateString name;8 9 PublicString getId () {Ten returnID; One } A - PublicString GetName () { - returnname; the } - - Publicbaseoption setId (String id) { - This. ID =ID; + return This; - } + A Publicbaseoption setName (String name) { at This. Name =name; - return This; - } - -}View Code
1 Packagecom.practice.option;2 3 Public classAppearanceoptionextendsbaseoption {4 5 PrivateString color;6 7 PrivateString shape;8 9 PrivateString size;Ten One PublicString GetColor () { A returncolor; - } - the PublicString Getshape () { - returnshape; - } - + PublicString GetSize () { - returnsize; + } A at Publicappearanceoption setcolor (String color) { - This. color =color; - return This; - } - - Publicappearanceoption setshape (String shape) { in This. Shape =shape; - return This; to } + - Publicappearanceoption setSize (String size) { the This. Size =size; * return This; $ }Panax Notoginseng -}View Code
At this point, when the object of the Appearanceoption class calls the method of the parent class, the parent class object is returned.
For example, the SetId () method returns a Baseoption object, and Eclipse automatically prompts you not to see the methods of the subclass.
Modify the code of the subclass Appearanceoption, overriding the parent class method.
1 Packagecom.practice.option;2 3 Public classAppearanceoptionextendsbaseoption {4 5 PrivateString color;6 7 PrivateString shape;8 9 PrivateString size;Ten One PublicString GetColor () { A returncolor; - } - the PublicString Getshape () { - returnshape; - } - + PublicString GetSize () { - returnsize; + } A at Publicappearanceoption setcolor (String color) { - This. color =color; - return This; - } - - Publicappearanceoption setshape (String shape) { in This. Shape =shape; - return This; to } + - Publicappearanceoption setSize (String size) { the This. Size =size; * return This; $ }Panax Notoginseng - @Override the Publicappearanceoption setId (String id) { + Super. SetId (ID); A return This; the } + - @Override $ Publicappearanceoption setName (String name) { $ Super. SetName (name); - return This; - } the -}View Code
The SetID () method now returns the Appearanceoption object, and the method of the subclass can be seen in the Eclipse auto prompt.
From the conclusion, there is no use of how advanced technology, mainly on the object-oriented features of understanding and application. However, in the actual design code structure when Leng half a day did not think.
The main solution is inspired by Java source code. In Java, the most common chained invocation is the Append () method in the StringBuffer, StringBuilder class. We can complete the concatenation of strings by continuous. Append (). Append () method. If you are a little familiar with the source code, you may know that the StringBuffer, StringBuilder two classes inherit from the abstract class Abstractstringbuilder, and the abstract class also has a append () method. The answer is obvious, check the source of StringBuffer or StringBuilder to know.
Chained invocation of the "Java" subclass