执行以下Java程序后运行的结果是( )。 class Parent{ void printInfo( ){ System.out.println("Parent"); } } class Child extends Parent{ void printInfo ( ){ System.out.println("Child"); } } public class Test { public static void main(String[ ] args) { Child child = new Child( ); child.printInfo( ); } }
时间: 2024-03-14 21:42:43 浏览: 18
运行结果为:
```
Child
```
解析:
在`main`方法中创建了一个`Child`类的对象`child`,并调用了它的`printInfo()`方法。由于`Child`类继承自`Parent`类,因此`Child`类重写了`Parent`类的`printInfo()`方法。因此,在调用`child.printInfo()`方法时,会调用`Child`类的`printInfo()`方法,打印出`Child`。
相关问题
class Person{ private String name; private int age; private char gender; public Person(String name,int age,char gender){ this.name=name; this.age=age; this.gender=gender; } public void printInfo(){ System.out.println("姓名: "+name); System.out.println("年龄: "+age); System.out.println("性别: "+gender); } } class Student extends Person{ private String school; private int score; public Student(String name,int age,char gender,String school,int score){ super(name,age,gender); this.school=school; this.score=score; } public void printInfo(){ super.printInfo(); System.out.println("学校: "+school); System.out.println("成绩: "+score); } } class Teacher extends Person{ private String course; public Teacher(String name,int age,char gender,String course){ super(name,age,gender); this.course=course; } public void printInfo(){ super.printInfo(); System.out.println("课程: "+course); } } //替换下面的学生信息,修改主类的名字 public class Experiment_04_01 { public static void main(String[] args) { Student stud=new Student01("lili",19,'F',"齐鲁工业大学",90); Teacher tech=new Teacher("coco",30,'F',"高级程序设计"); stud.printInfo(); System.out.println(); tech.printInfo(); } }
抱歉,您的代码有一些错误。首先,您在创建学生对象时使用了错误的类名,应该是"Student"而不是"Student01"。其次,由于您的代码中没有定义名为"Student01"的类,因此需要将主类的名字从"Experiment_04_01"修改为"Main"。以下是修改后的代码:
```java
class Person {
private String name;
private int age;
private char gender;
public Person(String name, int age, char gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public void printInfo() {
System.out.println("姓名: " + name);
System.out.println("年龄: " + age);
System.out.println("性别: " + gender);
}
}
class Student extends Person {
private String school;
private int score;
public Student(String name, int age, char gender, String school, int score) {
super(name, age, gender);
this.school = school;
this.score = score;
}
public void printInfo() {
super.printInfo();
System.out.println("学校: " + school);
System.out.println("成绩: " + score);
}
}
class Teacher extends Person {
private String course;
public Teacher(String name, int age, char gender, String course) {
super(name, age, gender);
this.course = course;
}
public void printInfo() {
super.printInfo();
System.out.println("课程: " + course);
}
}
public class Main {
public static void main(String[] args) {
Student stud = new Student("lili", 19, 'F', "齐鲁工业大学", 90);
Teacher tech = new Teacher("coco", 30, 'F', "高级程序设计");
stud.printInfo();
System.out.println();
tech.printInfo();
}
}
```
请注意,我已经将主类名更改为"Main",并将学生对象的类名更正为"Student"。现在您可以运行该代码并得到预期的输出。
2.以下代码中,带下划线的一行代码有没有实现动态多态性?为什么? class A { public void printInfo() { System.out.println("A"); } } class B extends A { @Override public void printInfo() { System.out.println("B"); } } public class Test { public static void main(String[] args) { A a = new A(); a.printInfo(); B b = new B(); b.printInfo(); } }
这段代码中没有实现动态多态性,因为在 main() 方法中分别创建了 A 类和 B 类的对象,并分别调用了它们的 printInfo() 方法,而这两个对象的类型都是确定的,并且在编译时期已经确定了调用的方法,所以属于静态绑定。即使 B 类的 printInfo() 方法覆盖了 A 类的 printInfo() 方法,但由于在代码中直接指定了调用的对象类型,所以不会发生动态绑定。
要实现动态多态性,需要使用向上转型的方式,将子类对象赋值给父类引用,然后通过父类引用调用方法,这样就可以在运行时期确定对象类型,实现动态绑定。比如在 main() 方法中可以将 B 类的对象赋值给 A 类的引用,如下所示:
```java
A a = new B(); // 向上转型
a.printInfo(); // 动态多态性
```
这样就会在运行时期确定对象类型,调用 B 类的 printInfo() 方法,实现动态多态性。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.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)
![](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)