如何从两个超类下面的子类中获取实例变量? [英] How do I reach an instance variable from a subclass beneath two superclasses?

查看:51
本文介绍了如何从两个超类下面的子类中获取实例变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类超级{

数字aNumber;

}

类Sub延伸超级{

Float aNumber;

}



类SubSUB扩展子{

长麻木;



公共SubSUB(Numbeer aNumber,浮动aNumber,长麻木){

我如何达到数字aNumber?

}

}



我的尝试:



我试过超级但我无法达到上层类实例变量,我如何解决这个问题?

class Super {
Number aNumber;
}
class Sub extends Super {
Float aNumber;
}

class SubSUB extends sub{
long numb;

public SubSUB(Numbeer aNumber, float aNumber, long numb){
how do i reach number aNumber?
}
}

What I have tried:

I have tried super but i cant reach the upper upper class instancevariable, how do i resolve this?

推荐答案

使用父构造函数来设置祖父项变量:

Use the parent contructor to set the grandparent variable:
class Super
{
  Number aNumber;
  public Super(Number n){ aNumber = n;}

}
class Sub extends Super
{
  Float aFloat;
  public Sub(Number n, Float f) { super(n); aFloat= f; }
}

class SubSub extends Sub
{
  long aLong;

  public SubSub (Number n, Float f, long l)
  {
    super(n, f);
    this.aLong = l;
  }

  public static void main(String arg[])
  {
    SubSub ss = new SubSub(new Double(5.0), new Float(3.2), 10);
  }
}


这篇关于如何从两个超类下面的子类中获取实例变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆