public class Student extends Person { Student() { System.out.println("I am a student."); } Student(String name) { super(name); System.out.println("I am a student."); } public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student("张三"); } } class Person { private String name; Person() { System.out.println("I am a person."); } Person(String name) { System.out.println("I am a person, and my name is: " + name); } }
时间: 2024-03-07 08:54:24 浏览: 21
这段代码中定义了一个名为 Student 的类,它继承自另一个名为 Person 的类。Student 类有两个构造方法,一个无参构造方法和一个带有一个字符串参数的构造方法。在无参构造方法中,会先调用父类的无参构造方法,再输出 "I am a student.";在有参构造方法中,会先调用父类的带有一个字符串参数的构造方法,再输出 "I am a student."。在 main 方法中,分别创建了一个无参构造方法和一个带有一个字符串参数的构造方法的 Student 对象。运行这段代码将输出以下内容:
I am a person.
I am a student.
I am a person, and my name is: 张三
I am a student.
相关问题
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public void eat() { System.out.println("Person is eating."); } public void sleep() { System.out.println("Person is sleeping."); }}public class Student extends Person { private String major; public Student(String name, int age, String major) { super(name, age); this.major = major; } @Override public void eat() { System.out.println("Student is eating."); } @Override public void sleep() { System.out.println("Student is sleeping."); }}
这段代码展示了一个父类 Person 和一个子类 Student。Person 类有一个构造方法和三个方法:getName()、getAge()、eat() 和 sleep()。Student 类继承自 Person 类,有一个构造方法和两个方法:eat() 和 sleep()。在 Student 类中,通过使用 @Override 注解重写了父类中的 eat() 和 sleep() 方法,实现了自己的行为。这个例子展示了面向对象的继承和多态特性,子类 Student 继承了父类 Person 的属性和方法,并且可以在此基础上添加自己的行为,实现了代码的复用和灵活性。同时,通过重写父类中的方法,我们实现了多态,可以在运行时根据具体对象的类型来调用相应的方法。
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"。现在您可以运行该代码并得到预期的输出。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)