Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java将变量从超类传输到子类_Java_Constructor_This_Extends - Fatal编程技术网

Java将变量从超类传输到子类

Java将变量从超类传输到子类,java,constructor,this,extends,Java,Constructor,This,Extends,在java中,我有一个扩展了类B的类a 我想把B类的所有内容都分配给A类 问题是我想从A类内部做这件事,现在这似乎是合理的,很容易做到,只需转移所有变量 import java.lang.reflect.Field; class Super { public int a ; public String name; Super(){} Super(int a, String name) { this.a = a; this.n

在java中,我有一个扩展了类B的类a

我想把B类的所有内容都分配给A类 问题是我想从A类内部做这件事,现在这似乎是合理的,很容易做到,只需转移所有变量

import java.lang.reflect.Field;
class Super
{
    public int a ;
    public String name;
    Super(){}
    Super(int a, String name)
    {
        this.a = a;
        this.name = name;
    }
}
class Child extends Super 
{
    public Child(Super other)
    {
        try{
        Class clazz = Super.class;
        Field[] fields = clazz.getFields();//Gives all declared public fields and inherited public fields of Super class
        for ( Field field : fields )
        {
            Class type = field.getType();
            Object obj = field.get(other);
            this.getClass().getField(field.getName()).set(this,obj);
        }
        }catch(Exception ex){ex.printStackTrace();}
    }
    public static void main(String st[])
    {
        Super ss = new Super(19,"Michael");
        Child ch = new Child(ss);
        System.out.println("ch.a="+ch.a+" , ch.name="+ch.name);
    }
}
这是最难的部分。我没有进入B类,它是android.widget的一部分 在C++中,你只需在B类中,然后分配给这个并将其转换。 我将如何在java中实现这一点

为了进一步说明它是relativelayout,我需要将relativelayout的所有内容复制到扩展relativelayout的类中

class something extends other
{
public something(other a){
 //transfer all of the other class into something
 this=(something)a;  // obviously doesn't work
 //*this doesn't exist?
 //too many variables to transfer manually
}
}

非常感谢你的帮助。真的很感激

父类(非私有)的所有变量和函数在子类中都是直接访问的。您不需要在子类中分配任何东西。您可以直接访问。

父类(非私有)的所有变量和函数是子类中的直接访问。您不需要在子类中分配任何内容。您可以直接访问。

这将不起作用:

Something=(Something)other.clone()

如果other的true运行时类型是
other


相反,您必须创建一个复制构造函数,或者将
其他
实例化为
某物
的实例,然后克隆它。

这将不起作用:

Something=(Something)other.clone()

如果other的true运行时类型是
other


相反,您必须创建一个复制构造函数,或者将
other
实例化为
Something
的实例,然后克隆它。

请参阅下面给出的代码。它使用
java.lang.reflect
包从超类中提取所有字段,并将获得的值分配给子类变量

import java.lang.reflect.Field;
class Super
{
    public int a ;
    public String name;
    Super(){}
    Super(int a, String name)
    {
        this.a = a;
        this.name = name;
    }
}
class Child extends Super 
{
    public Child(Super other)
    {
        try{
        Class clazz = Super.class;
        Field[] fields = clazz.getFields();//Gives all declared public fields and inherited public fields of Super class
        for ( Field field : fields )
        {
            Class type = field.getType();
            Object obj = field.get(other);
            this.getClass().getField(field.getName()).set(this,obj);
        }
        }catch(Exception ex){ex.printStackTrace();}
    }
    public static void main(String st[])
    {
        Super ss = new Super(19,"Michael");
        Child ch = new Child(ss);
        System.out.println("ch.a="+ch.a+" , ch.name="+ch.name);
    }
}

请参阅下面给出的代码。它使用
java.lang.reflect
包从超类中提取所有字段,并将获得的值分配给子类变量

import java.lang.reflect.Field;
class Super
{
    public int a ;
    public String name;
    Super(){}
    Super(int a, String name)
    {
        this.a = a;
        this.name = name;
    }
}
class Child extends Super 
{
    public Child(Super other)
    {
        try{
        Class clazz = Super.class;
        Field[] fields = clazz.getFields();//Gives all declared public fields and inherited public fields of Super class
        for ( Field field : fields )
        {
            Class type = field.getType();
            Object obj = field.get(other);
            this.getClass().getField(field.getName()).set(this,obj);
        }
        }catch(Exception ex){ex.printStackTrace();}
    }
    public static void main(String st[])
    {
        Super ss = new Super(19,"Michael");
        Child ch = new Child(ss);
        System.out.println("ch.a="+ch.a+" , ch.name="+ch.name);
    }
}


当你扩展一个特定的类时,你会自动继承该类的所有属性。不是吗?我认为他有一个类a的对象和一个类B的对象,并且想将类B的字段值复制到类a的对象。其他a
所指的对象的确切类型是什么?如果它是类
某物
,那么你的代码肯定会工作。这正是我想要Jean Waghetti的。如果你知道所有这些变量的名称,那么你可以使用反射API复制这些值。当你扩展一个特定的类时,你会自动继承该类的所有属性。不是吗?我想他有一个对象类和类B的对象,并希望将类B的字段值复制到类A的对象。其他A
所指的对象的确切类型是什么?如果它是类
something
,那么你的代码肯定会工作..这正是我想要Jean Waghetti的东西如果你知道所有这些变量的名称,那么你可以使用反射API复制这些值..正如我在问题中所评论的,我想他有一个A类的对象和一个B类的对象,想把B类的字段值复制到A类的对象。是的,这就是我想要的。我需要将对象b字段复制到对象A,因为对象A扩展了我在问题中评论的BAs,我认为他有一个类A的对象和一个类b的对象,并且希望将类b的字段值复制到类A的对象。是的,这就是我想要的。我需要对象b字段来复制到对象A,因为对象A扩展了b对象是一个android.widget.RelativeLayout我无法访问它的所有内容手动复制它对象是一个android.widget.RelativeLayout我无法访问它的所有内容手动复制它我必须坐下来看看它是否有效。我做了一个在我的案例中有效的工作,但实际上没有回答这个问题。但我以后还要看这个。谢谢似乎是真正的交易tho:t这不会在私人领域,对吗?可能会成为一个问题。如果android.widget.RelativeLayout有任何真正重要的私有字段而不是受保护的字段?有人知道吗?@Lpc_dark:子类永远无法访问超级类的私有字段。让超级类来处理其私有字段。这是符合最重要的规则的1.非常感谢。现实生活中的英雄就是你,我必须坐下来看看它是否有效。我做了一个在我的案例中有效的工作,但实际上没有回答这个问题。但我以后还要看这个。谢谢似乎是真正的交易tho:t这不会在私人领域,对吗?可能会成为一个问题。如果android.widget.RelativeLayout有任何真正重要的私有字段而不是受保护的字段?有人知道吗?@Lpc_dark:子类永远无法访问超级类的私有字段。让超级类来处理其私有字段。这是符合最重要的规则的1.非常感谢。现实生活中的英雄就是你