简体   繁体   English

Java:创建一个将从父对象继承父属性的子对象

[英]Java: Create a Child Object that will inherit the Parent properties from a parent Object

I am new to Inheritance properties.我是继承属性的新手。

We created a Child object "obnewChild" which inherits the Parent properties from the Parent Object, "obParent" .我们创建了一个对象“obnewChild”从父对象,“obParent”继承属性。

The main program will use the Child object "obnewChild" and manipulate the fields inherited from the Parent as well as its own fields.主程序将使用Child对象“obnewChild”并操作从 Parent 继承的字段以及它自己的字段。

At the end - the Output would be the Parent Object, "obParent" back in Json format.. with the updated values of the fields from the program run.最后 - 输出将是父对象,返回 Json 格式的“obParent” ......以及程序运行中字段的更新值。

(From a Program perspective - The Child object has transient fields - that are created for each Parent Object - used in the main program during its run. The expectation is that any changes to the Child object will also show up in the Parent Object .. from which it was inherited) (从程序的角度来看 - 子对象具有瞬态字段 - 为每个父对象创建 - 在主程序运行期间在主程序中使用。期望对子对象的任何更改也将显示在父对象中..它是从那里继承的)

The test program is shown below.测试程序如下所示。 It is not working in the current format.它不适用于当前格式。

Can the above be done elegantly?以上可以优雅地完成吗? What would be a good way to execute this in Java.在 Java 中执行此操作的好方法是什么。

Thanks for your help.谢谢你的帮助。

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);
    }
}

//********************************** // New Program //************************************ // 新程序

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);

      }

}

Output:输出:

X Y
X Y
X Y C D
XYZ ABC C D
XYZ ABC

In the seconds case.. I change the Child Object fields, the same changes show up when I print the Parent Object.在秒的情况下......我更改了子对象字段,当我打印父对象时会显示相同的更改。

You get a ClassCastException because the object that you are trying to cast to a Child is not actually a child.您会收到 ClassCastException,因为您尝试转换为 Child 的对象实际上并不是一个孩子。 However, you should be able to see that the child can manipulate the fields it inherits from the parent if you add some lines after但是,如果在后面添加一些行,您应该能够看到子级可以操作它从父级继承的字段

    //================
    obChild.parentMethod();
    obChild.childMethod();
    obChild.childParentMethod();
    //================

so that it reads所以它读

    //================
    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
    //================

If you want to cast in this situation, you can cast -- or actually just assign -- the Child object to a Parent object.如果您想在这种情况下进行强制转换,您可以将子对象强制转换为——或者实际上只是将——子对象分配给父对象。

If you want to avoid an error at the line where you are getting an error, you could also change the construction of obParent so that it actually is a Child object.如果您想避免在出现错误的行出现错误,您还可以更改 obParent 的构造,使其实际上是一个 Child 对象。 If you had written如果你写过

Parent obParent = new Child();

the line where you are getting an error would succeed.您遇到错误的行会成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM