[Java] chained call of sub-classes and java chained call

Source: Internet
Author: User
Tags getcolor

[Java] chained call of sub-classes and java chained call

Record a small problem recently encountered in the project design.

Premise: There are two POJO classes, both of which can set their property values through chained calls. One Class inherits the other class.

Q: After a subclass object accesses the parent class method through a chained call, how can I make the returned object still be a subclass object and continue the chained call of the subclass method?

Conclusion: subclass overrides the method to be called in the parent class. In the subclass override method, the parent class method is called by using the super keyword,

Then return the subclass object using the return this statement.

 

The sample code above is used to describe the problem and solution in a more specific and vivid way.

BaseOption and AppearanceOption are two POJO classes that implement chained calling. AppearanceOption inherits from BaseOption.

 1 package com.practice.option; 2  3 public class BaseOption { 4  5     private String id; 6  7     private String name; 8  9     public String getId() {10         return id;11     }12 13     public String getName() {14         return name;15     }16 17     public BaseOption setId(String id) {18         this.id = id;19         return this;20     }21 22     public BaseOption setName(String name) {23         this.name = name;24         return this;25     }26 27 }
View Code
 1 package com.practice.option; 2  3 public class AppearanceOption extends BaseOption { 4  5     private String color; 6  7     private String shape; 8  9     private String size;10 11     public String getColor() {12         return color;13     }14 15     public String getShape() {16         return shape;17     }18 19     public String getSize() {20         return size;21     }22 23     public AppearanceOption setColor(String color) {24         this.color = color;25         return this;26     }27 28     public AppearanceOption setShape(String shape) {29         this.shape = shape;30         return this;31     }32 33     public AppearanceOption setSize(String size) {34         this.size = size;35         return this;36     }37 38 }
View Code

In this case, the object of the AppearanceOption class returns the parent class object after calling the method of the parent class.

For example, the setId () method returns a BaseOption object, and the subclass method is not displayed in the eclipse automatic prompt.

 

Modify the Code of the Child class AppearanceOption and override the parent class method.

 1 package com.practice.option; 2  3 public class AppearanceOption extends BaseOption { 4  5     private String color; 6  7     private String shape; 8  9     private String size;10 11     public String getColor() {12         return color;13     }14 15     public String getShape() {16         return shape;17     }18 19     public String getSize() {20         return size;21     }22 23     public AppearanceOption setColor(String color) {24         this.color = color;25         return this;26     }27 28     public AppearanceOption setShape(String shape) {29         this.shape = shape;30         return this;31     }32 33     public AppearanceOption setSize(String size) {34         this.size = size;35         return this;36     }37     38     @Override39     public AppearanceOption setId(String id) {40         super.setId(id);41         return this;42     }43 44     @Override45     public AppearanceOption setName(String name) {46         super.setName(name);47         return this;48     }49 50 }
View Code

Now the setId () method returns the AppearanceOption object. You can see the subclass method in the eclipse automatic prompt.

From the conclusion, there is no advanced technology, mainly understanding and application of object-oriented features. However, when designing the code structure, I did not expect it for half a day.

The main solution is inspired by the Java source code. In Java, the most common chained call is the append () method in the StringBuffer and StringBuilder classes. We can concatenate strings through the continuous. append (). append () method. If you are a little familiar with the source code, you may know that the StringBuffer and StringBuilder classes are inherited from the abstract class AbstractStringBuilder, which also contains the append () method. The answer is obvious. Check the StringBuffer or StringBuilder source code.

 

 

The several questions that come up with this question are left empty first, and I want to understand them and then I want to add ......

1. Under what circumstances is this chained method suitable for calling?

2. Is this inheritance structure reasonable for projects? Over-designed?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.