我做了一些搜索,但我要么没有问正确的问题,要么没有正确记住.在任何情况下,在Java中,我想知道是否可以将超类对象作为参数传递给子类,以及将该对象的数据提供给类的超类的最有效方法.
代码示例:
public class superclass {
String myparm1;
String myParm2;
int myParmN;
public superclass(String p1, String p2, int pn)
{
this.myparm1 = p1;
this.myparm2 = p2;
this.myParmN = pn;
}
// other methods here
}
public class subclass extends superclass {
double b1;
double b2;
public subclass(superclass sc, double b1, double b2) {
// easy way to make sc data available to this class?
// Do I create a copy or clone method, or some other way?
// calling super(sc); wouldn't exactly work
this.b1 = b1;
this.b2 = b2;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我有一个超类的构造函数,public superclass(superclass sc) { // assign sc properties to this properties, correct? }那么我可以简单地使用super(sc);
传递对构造函数中对象的超类的引用是没有意义的.您的子类已经是超类的实例.
即使您无法直接查看超类的私有组件,但它们仍然存在并且对公共访问器方法的调用仍将产生正常行为.
在回答第二个问题时,访问父类内部数据的最有效方法是使用该父类的访问器方法.如果它有get/set属性方法来填充一些充满属性的数据结构,那么只需从子类中调用这些方法,它们的工作方式与它们对父类完全相同.现在,如果这些内部数据结构由父类的构造函数填充,则在创建需要它们的子构造函数的实例时,您必须使用正确的方法调用该构造函数 - 通常通过调用相应的super()在孩子的构造函数的开头.
如果你试图绕过限制,你无法看到超类的私有部分,java故意不允许你这样做.你可以通过反射解决这个问题,除非你被困在一个不允许这样做的执行环境中,但我通常不会认为这是一种安全或优雅的方法.
从下面的评论中,我理解OP正在尝试做什么,这应该有效,但显然这取决于你对超类进行更改的能力:
public class Super
{
public Super (Super other)
{
//copy stuff from other to this
}
}
public class Child extends Super
{
public Child (Super other)
{
super(other);
//continue constructor
}
}
Run Code Online (Sandbox Code Playgroud)