one , problem

Writing code recently , There are two classes with similar properties , among 80% Properties of ( field ) It's all the same , The rest are different , When designing , The way of inheritance is adopted , Abstract except for a parent class , Roughly as follows ,

have FirstChild and SecondChild Two classes , Because of the properties name,code And so on , A parent class was extracted for this purpose BaseDO, as follows
package com.example.day01; public class BaseDO { private String name; private
String code; private String field1; private String field2; public String
getName() { return name; } public void setName(String name) { this.name = name;
} public String getCode() { return code; } public void setCode(String code) {
this.code = code; } public String getField1() { return field1; } public void
setField1(String field1) { this.field1 = field1; } public String getField2() {
return field2; } public void setField2(String field2) { this.field2 = field2; }
@Override public String toString() { return "BaseDO{" + "name='" + name + '\''
+ ", code='" + code + '\'' + ", field1='" + field1 + '\'' + ", field2='" +
field2 + '\'' + '}'; } }
FirstChild
package com.example.day01; public class FirstChild extends BaseDO{ private
String myField1; private String myFiled2; public String getMyField1() { return
myField1; } public void setMyField1(String myField1) { this.myField1 =
myField1; } public String getMyFiled2() { return myFiled2; } public void
setMyFiled2(String myFiled2) { this.myFiled2 = myFiled2; } @Override public
String toString() { return "FirstChild{" + "myField1='" + myField1 + '\'' + ",
myFiled2='" + myFiled2 + '\'' + "} " + super.toString(); } }
SecondChild
package com.example.day01; public class SecondChild extends BaseDO{ private
String secondField1; private String secondField2; public String
getSecondField1() { return secondField1; } public void setSecondField1(String
secondField1) { this.secondField1 = secondField1; } public String
getSecondField2() { return secondField2; } public void setSecondField2(String
secondField2) { this.secondField2 = secondField2; } @Override public String
toString() { return "SecondChild{" + "secondField1='" + secondField1 + '\'' +
", secondField2='" + secondField2 + '\'' + "} " + super.toString(); } }
From the above, we can see that the two subclasses have their own properties in addition to the properties of the parent class , Now there is a demand like this , To instantiate these two subclasses .

two , How to solve

2.1, Initialize separately

What is separate initialization , The so-called separate initialization is to initialize their own , Each subclass implements methods to initialize its properties separately , as follows
FirstChild fillFirstField(FirstChild firstChild){ firstChild.setName("apple");
firstChild.setCode("apple"); firstChild.setMyField1("first Child"); return
firstChild; } SecondChild fillSecondField(SecondChild secondChild){
secondChild.setName("apple"); secondChild.setCode("apple");
secondChild.setSecondField1("second Child"); return secondChild; }
As a demonstration, not all attributes are assigned , If two subclasses have the same attributes, there are more , Then the assignment will be more troublesome , And the code repetition of the two methods will be very high .

2.2, Abstract a public method

since , Common properties have been abstracted for the two subclasses , Then follow this idea , You can also abstract out a public method to assign values to these public attributes , That is, fill in the attributes for the parent class ,
BaseDO fillField(BaseDO baseDO){ baseDO.setName("apple");
baseDO.setCode("apple"); return baseDO; }
okay , When initializing subclasses, there is already a method that can initialize its public properties , So what happens next , Just initialize its own properties ,

Instantiate below FirstChild, Then initialize its public properties
FirstChild firstChild=new FirstChild(); fillField(firstChild);
firstChild.setMyField1("first Child"); System.out.println(firstChild);
Print out firstChild The results are as follows

You can see that the public attribute name,code And intentional attributes myField1 Assign value , Completed the above requirements .

Maybe some students will use the following writing method ,

It can be seen that there are errors in this writing , Tip: what type do we need FirstChild, But what we offer BaseDO, We know fillField Method returns BaseDO type , An instance of a parent type cannot be assigned to a reference of a child type (  Instead, an instance of a subtype can be assigned to a parent type , Typical polymorphism  ), What about that , Down type conversion , you 're right , as follows

A careful partner will ask , Then why fillField The method is acceptable FirstChild Example of that , Ha ha ha , Polymorphism is mentioned in the preceding scarlet letter .

how , Do you have a deeper understanding of polymorphism .

Extend the application a little

from fillField Method point of view , We know that this method can not set the return value , Why not set the return value , Because the reference type , Not pass value , It can be understood as a reference , ha-ha , So it's called reference type , stay fillField The parameter of its reference type has been modified in the method , So after this method is executed , Other references that reference this parameter can also be aware of its modification , The following wording is a good proof ,

Does it deepen the relationship between references and referenced objects , One more quote jvm Which area is in the memory model of , The object that the reference points to ?

three , summary

This paper analyzes the development process , Design idea when encountering multiple entity classes with many public attributes , Extract public parent , The public attribute is assumed by the parent class . And during attribute filling , If the value of the public attribute is the same , Then you can extract public methods for attribute filling , Polymorphism is also mentioned here .

1, Extract public attributes ;

2, Understanding polymorphism ;

3, Down type conversion ;

4, Passing of reference types ;

last , Readers , For similar needs , Do you have a better design idea , Welcome to discuss .

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage Python Study notes ( one )evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possibleRISC-V_GD32VF103-TIMER0 timer interrupt java Array subscript variable _Java Basic grammar : array be based on stm32 Control four-wheel trolley motor drive ( one ) be based on redis Design of liking function Software engineering career planning mysql Query random data by conditions _MySQL Random query of several qualified records centos7 install RabbitMq