[英]Java: Create a Child Object that will inherit the Parent properties from a parent Object
我是繼承屬性的新手。
我們創建了一個子對象“obnewChild”從父對象,“obParent”繼承父屬性。
主程序將使用Child對象“obnewChild”並操作從 Parent 繼承的字段以及它自己的字段。
最后 - 輸出將是父對象,返回 Json 格式的“obParent” ......以及程序運行中字段的更新值。
(從程序的角度來看 - 子對象具有瞬態字段 - 為每個父對象創建 - 在主程序運行期間在主程序中使用。期望對子對象的任何更改也將顯示在父對象中..它是從那里繼承的)
測試程序如下所示。 它不適用於當前格式。
以上可以優雅地完成嗎? 在 Java 中執行此操作的好方法是什么。
謝謝你的幫助。
public class Inheritance {
public static void main(String[] args) {
Parent obParent = new Parent();
Child obChild = new Child();
//================
obParent.setVariables();
obParent.parentMethod();
//================
obChild.parentMethod();
obChild.childMethod();
obChild.childParentMethod();
//================
Child obnewChild = (Child) obParent; //Error
}
}
public class Parent {
String varA = "A";
String varB = "B";
public void parentMethod(){
System.out.println(varA + " " + varB);
}
public void setVariables(){
this.varA = "X";
this.varB = "Y";
}
}
public class Child extends Parent {
String varC = "C";
String varD = "D";
public void childMethod(){
System.out.println( varC + " " + varD);
}
public void childParentMethod(){
System.out.println(super.varA + " " + super.varB + " " + this.varC + " " + this.varD);
}
}
//************************************ // 新程序
public class Inheritance {
public static void main(String[] args) {
//================
Parent obParent = new Parent();
obParent.setVariables();
obParent.printParent();
//================
Child obnewChild = new Child(obParent);
obnewChild.printChildAndParent();
obnewChild.setVariables();
obnewChild.printChildAndParent();
//================
obParent.printParent();
//================
}
}
public class Parent {
String varA = "A";
String varB = "B";
public void printParent(){
System.out.println(varA + " " + varB);
}
public void setVariables(){
this.varA = "X";
this.varB = "Y";
}
}
public class Child {
Parent obParent = new Parent();
String varC = "C";
String varD = "D";
public Child(){
}
public Child(Parent obParent){
System.out.println( obParent.varA + " " + obParent.varB);
this.obParent = obParent;
}
public void setVariables(){
this.obParent.varA = "XYZ";
this.obParent.varB = "ABC";
}
public void printChild(){
System.out.println( varC + " " + varD);
}
public void printChildAndParent(){
System.out.println(obParent.varA + " " + obParent.varB + " " + this.varC + " " + this.varD);
}
}
輸出:
X Y
X Y
X Y C D
XYZ ABC C D
XYZ ABC
在秒的情況下......我更改了子對象字段,當我打印父對象時會顯示相同的更改。
您會收到 ClassCastException,因為您嘗試轉換為 Child 的對象實際上並不是一個孩子。 但是,如果在后面添加一些行,您應該能夠看到子級可以操作它從父級繼承的字段
//================
obChild.parentMethod();
obChild.childMethod();
obChild.childParentMethod();
//================
所以它讀
//================
obChild.parentMethod();
obChild.childMethod();
obChild.childParentMethod();
obChild.setVariables(); // set the values of fields inherited from the parent
obChild.childParentMethod(); // print and observe the values have changed
//================
如果您想在這種情況下進行強制轉換,您可以將子對象強制轉換為——或者實際上只是將——子對象分配給父對象。
如果您想避免在出現錯誤的行出現錯誤,您還可以更改 obParent 的構造,使其實際上是一個 Child 對象。 如果你寫過
Parent obParent = new Child();
您遇到錯誤的行會成功。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.