class Father { int a =100; public void print() { System.out.println(a); } } class Child1 extends Father { int a =200; public void print() { System.out.println(a); } } class Child2 extends Father { int a =300; public void print() { System.out.println(a); } } public class Main { public static void main(String[] a) { Father obj1; obj1=new Child1(); System.out.println(obj1.a); obj1.print(); obj1=new Child2(); System.out.println(obj1.a); obj1.print(); } }
时间: 2024-03-13 18:48:09 浏览: 48
执行代码的输出结果为:
```
200
200
300
300
```
首先定义了一个 `Father` 类型的引用变量 `obj1`,然后将其赋值为 `Child1` 类型的对象。因为 `Child1` 类是 `Father` 类的子类,所以可以将 `Child1` 类型的对象赋值给 `Father` 类型的引用变量。此时,`obj1` 引用的对象是 `Child1` 类型的,所以访问 `obj1.a` 时,会输出 `200`,而调用 `obj1.print()` 方法时,会调用 `Child1` 类中重写的 `print()` 方法,输出的结果也是 `200`。
接着,将 `obj1` 赋值为 `Child2` 类型的对象。此时,`obj1` 引用的对象变成了 `Child2` 类型的,所以访问 `obj1.a` 时,会输出 `300`,而调用 `obj1.print()` 方法时,会调用 `Child2` 类中重写的 `print()` 方法,输出的结果也是 `300`。
因此,最终输出的结果为 `200 200 300 300`。
相关问题
class Father { int a; public Father(int a) { this.a=a; } public void print() { System.out.println(a); } } class Child extends Father { int a; public Child(int a) {
super(a);
this.a = a * 2;
}
public void print() {
System.out.println(a);
super.print();
}
}
以上是一个父类Father和一个子类Child的代码。当执行下面的代码时,会输出什么?
Father f = new Child(10);
f.print();
请问输出的内容是什么?
class Father { int a = 100; int b = 200; public void print() { System.out.println(a + " " + b); } } class Child extends Father { int b = 300; int c = 400; public void print() { System.out.println(a + " " + b + " " + c); } public void printExtend() { System.out.println(c); } } public class Main { public static void main(String[] a) { Father obj = new Child(); System.out.println(obj.a); / AA语句 System.out.println(obj.b); // BB语句 obj.print(); // CC语句 obj.printExtend(); // DD语句 } }
这段代码会输出什么?
答案:
AA语句会输出 100,因为 Father 类的 a 变量被继承并赋值为 100,而子类 Child 中没有对 a 进行重写或者隐藏。
BB语句会输出 200,因为在 Child 类中定义了一个与父类同名的 b 变量,这个变量会隐藏父类的 b 变量,所以 obj.b 会访问到父类的 b 变量。
CC语句会输出 100 300 400,因为 obj 是一个 Father 类型的引用,但是实际引用的是 Child 类的一个对象,所以调用 print 方法时会调用 Child 类中的 print 方法,从而输出 Child 类中的 a、b、c 变量。
DD语句会编译错误,因为 Father 类中没有 printExtend 方法,而 obj 引用的类型是 Father 类型,无法调用子类中的方法。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)