class FatherA{ int x,y; FatherA(int x,int y){this.x=x;this.y=y;} static void show(){ System.out.println("FatherA静态方法"); } void compare(){ System.out.println(x+y); } } class SonB extends FatherA{ int x,y; SonB(int x,int y) { super(x,y);//代码1 this.x=x; this.y=y; } void compare(){ System.out.println(x+y); } static void show(){ System.out.println("SonB类的静态方法"); } } public class Test { public static void main(String[] args) { FatherA a; SonB b=new SonB(10,20); a=b; b.compare();//代码2 a.compare();//代码3 b.show();//代码4 a.show();//代码5 } }代码2和代码3执行的结果都是一样,输出30.而且两个对象a、b都是调用子类重写之后的方法。 是对的吗
时间: 2023-12-06 21:41:08 浏览: 24
是的,代码2和代码3输出的结果都是30。因为b是SonB类的对象,调用compare()方法时会先在SonB类中查找该方法,如果找到了就直接调用,如果没有找到则向上找父类中是否有该方法,找到FatherA类中的compare()方法,然后执行FatherA类中的compare()方法,输出x+y的值。而a是FatherA类的引用,指向了SonB类的对象b,所以a也是调用SonB类中重写的compare()方法,输出的也是30。
代码4输出的是“SonB类的静态方法”,因为静态方法是根据引用所属的类来决定的,而不是根据引用所指向的对象。所以这里b调用的是SonB类中的静态方法,而不是FatherA类中的静态方法。
代码5输出的也是“SonB类的静态方法”,因为a虽然是FatherA类的引用,但是指向的是SonB类的对象b,所以也是调用SonB类中的静态方法。
相关问题
class FatherA{ int x,y; FatherA(int x,int y){this.x=x;this.y=y;} static void show(){ System.out.println("FatherA静态方法"); } void compare(){ System.out.println(x+y); } } class SonB extends FatherA{ int x,y; Son
B(int x,int y,int a,int b){ super(x,y); this.x=a; this.y=b; } static void show(){ System.out.println("SonB静态方法"); } void compare(){ super.compare(); System.out.println(x+y); } }
以上是一个Java代码,其中FatherA是父类,SonB是子类。
父类和子类都有x和y属性,但是子类中的x和y是新定义的,与父类中的x和y不同。
父类中有一个compare()方法,输出x和y的和,而子类中也有一个compare()方法,先调用父类的compare()方法,再输出子类中的x和y的和。
父类中有一个静态方法show(),输出"FatherA静态方法",而子类中也有一个静态方法show(),输出"SonB静态方法",这种情况下,当通过子类调用静态方法show()时,输出的是"SonB静态方法",而不是"FatherA静态方法"。
解释下面这段代码并输出值class FatherA{ int x,y; FatherA(int x,int y){this.x=x;this.y=y;} static void show(){ System.out.println("FatherA静态方法"); } void compare(){ System.out.println(x+y); } } class SonB extends FatherA{ int x,y; SonB(int x,int y) { super(x,y);//代码1 this.x=x; this.y=y; } void compare(){ System.out.println(x+y); } static void show(){ System.out.println("SonB类的静态方法"); } } public class Test { public static void main(String[] args) { FatherA a; SonB b=new SonB(10,20); a=b; b.compare();//代码2 a.compare();//代码3 b.show();//代码4 a.show();//代码5 } }
以上代码定义了一个父类FatherA和一个子类SonB,SonB是FatherA的子类。在Test类的main方法中,创建了一个SonB对象b,并将其赋值给FatherA类型的变量a。
在代码行1中,FatherA类有一个构造函数,用于初始化父类的成员变量x和y,使用this关键字来指代当前对象。
在代码行3中,FatherA类有一个静态方法show(),输出"FatherA静态方法"。
在代码行4中,FatherA类有一个实例方法compare(),输出x+y的值。
在代码行5中,SonB类继承了FatherA类,并且添加了自己的成员变量x和y。在SonB类的构造函数中,使用super关键字调用父类的构造函数,初始化父类的成员变量x和y,然后使用this关键字初始化子类的成员变量x和y。
在代码行8中,SonB类重写了compare()方法,输出子类自己的x+y的值。
在代码行9中,SonB类有一个静态方法show(),输出"SonB类的静态方法"。
在代码行12中,b.compare()输出30,因为子类的compare()方法被调用,输出子类自己的x+y的值。
在代码行13中,a.compare()输出30,因为a指向的是一个SonB对象,调用的是子类的compare()方法,输出子类自己的x+y的值。
在代码行14中,b.show()输出"SonB类的静态方法",因为调用的是子类的静态方法show()。
在代码行15中,a.show()输出"FatherA静态方法",因为静态方法不具有多态性,调用的仍然是父类的静态方法show()。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)