class Father{ public void func1() { func2();} public void func2() { System.out.println("AAA");} } class Child extends Father { public void func1(int i){System.out.println("BBB");} public void func2(){System.out.println("CCC");} } public class PolymorphismTest{ public static void main (String []args){ Father child =new Child(); Father father =new Father(); child.func1(); child.func2(); father.func1(); father.func2(); } }打印结果
时间: 2024-03-31 22:37:18 浏览: 23
打印结果为:
CCC
AAA
AAA
AAA
解析:
1. 首先创建了一个 Child 对象 child 和一个 Father 对象 father。
2. 在调用 child.func1() 时,因为 Child 类没有 func1() 方法,所以会调用 Father 类的 func1() 方法,而 Father 类的 func1() 方法中又调用了 func2() 方法,所以打印出 "CCC"。
3. 在调用 child.func2() 时,因为 child 是以 Father 类的身份被创建的,所以调用的是 Father 类的 func2() 方法,打印出 "AAA"。
4. 在调用 father.func1() 时,因为 Father 类中有 func1() 方法,所以直接调用 Father 类的 func1() 方法,而 Father 类的 func1() 方法中又调用了 func2() 方法,所以打印出 "AAA"。
5. 在调用 father.func2() 时,因为 father 是 Father 类的对象,所以直接调用 Father 类的 func2() 方法,打印出 "AAA"。
相关问题
class Father{ public void func1() { func2();} class Father { public void func1() { func2(); } public void func2() { System.out.println("AAA"); }}class Child extends Father { public void func1(int i) { System.out.println("BBB"); } public void func2() { System.out.println("CCC"); }}public class PolymorphismTest { public static void main(String[] args) { Child child = new Child(); Father father = new Father(); child.func1(); child.func2(); father.func1(); father.func2(); }}打印结果
这段代码存在语法错误,无法编译通过。我做了一些修改,修改后的代码如下:
```
class Father {
public void func1() {
func2();
}
public void func2() {
System.out.println("AAA");
}
}
class Child extends Father {
public void func1(int i) {
System.out.println("BBB");
}
public void func2() {
System.out.println("CCC");
}
}
public class PolymorphismTest {
public static void main(String[] args) {
Child child = new Child();
Father father = new Father();
child.func1(1);
child.func2();
father.func1();
father.func2();
}
}
```
运行这段代码会输出以下结果:
```
BBB
CCC
AAA
AAA
```
解析:
1. 首先创建一个 Child 对象 child 和一个 Father 对象 father。
2. 调用 child.func1(1) 时,因为 Child 类有 func1(int i) 方法,所以直接调用 Child 类的 func1(int i) 方法,打印 "BBB"。
3. 调用 child.func2() 时,因为 Child 类有 func2() 方法,所以直接调用 Child 类的 func2() 方法,打印 "CCC"。
4. 调用 father.func1() 时,因为 Father 类有 func1() 方法,所以直接调用 Father 类的 func1() 方法,而 Father 类的 func1() 方法中又调用了 func2() 方法,所以打印 "AAA"。
5. 调用 father.func2() 时,因为 father 是 Father 类的对象,所以直接调用 Father 类的 func2() 方法,打印 "AAA"。
public void func2() { System.out.println("AAA";}} class Child extends Father { public void func1(int i){System.out,println("BBB");} public void func2(){System.out,println("CCC");}} public class PolymorphismTest{ public static void main (String []args){ Child child =new Child(); Father father =new Father(); child.func1(); child.func2(); father.func1(); father.func2();}}打印结果
代码存在一些语法错误,我做了一些修改,修改后的代码如下:
```
class Father {
public void func1() {
func2();
}
public void func2() {
System.out.println("AAA");
}
}
class Child extends Father {
public void func1(int i) {
System.out.println("BBB");
}
public void func2() {
System.out.println("CCC");
}
}
public class PolymorphismTest {
public static void main(String[] args) {
Child child = new Child();
Father father = new Father();
child.func1(1);
child.func2();
father.func1();
father.func2();
}
}
```
打印结果为:
```
BBB
CCC
AAA
AAA
```
解析:
1. 首先创建了一个 Child 对象 child 和一个 Father 对象 father。
2. 在调用 child.func1(1) 时,因为 Child 类有 func1(int i) 方法,所以直接调用 Child 类的 func1(int i) 方法,打印出 "BBB"。
3. 在调用 child.func2() 时,因为 Child 类有 func2() 方法,所以直接调用 Child 类的 func2() 方法,打印出 "CCC"。
4. 在调用 father.func1() 时,因为 Father 类中有 func1() 方法,所以直接调用 Father 类的 func1() 方法,而 Father 类的 func1() 方法中又调用了 func2() 方法,所以打印出 "AAA"。
5. 在调用 father.func2() 时,因为 father 是 Father 类的对象,所以直接调用 Father 类的 func2() 方法,打印出 "AAA"。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)