当前位置: 代码迷 >> Java相关 >> 关于方法覆盖的菜菜
  详细解决方案

关于方法覆盖的菜菜

热度:333   发布时间:2004-10-17 01:49:00.0
关于方法覆盖的菜菜

public class test{ public static void main(String args[]){ System.out.println("Hello World!!!") ; A a = new A(6) ; a.printresult() ; B b = new B(7) ; b.printresult() ; } }

class A{ public int x, y ;

A(int i){ x = i ; sendresult() ; }

public void sendresult(){ y = x * x ; }

public void printresult(){ System.out.println(y) ; } }

class B extends A{ }

在B类中我想通过构造函数向类中传递x,并利用方法的覆盖把y = x * x改成y = x + x,但是不知道那个super是怎么用的。~~请大家帮我把B类里面的东西补全~~谢谢

搜索更多相关的解决方案: 菜菜  

----------------解决方案--------------------------------------------------------

class B extends A{

B (int i){ super(i); sendresult(); }

public void sendresult(){ y = x + x ; } }

class B is a subclass of A, in this case, if you have another method called sendresult which has the same method signiture as the one in its parent class, namely A, the one in the subclass, B, will be called first.


----------------解决方案--------------------------------------------------------
  相关解决方案