Person继承student的Java
时间: 2024-03-01 10:50:36 浏览: 84
Java类继承代码示例,定义Person类,Student类继承自Person类,并添加其他信息
代码如下:
```
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void study() {
System.out.println("I'm studying.");
}
}
public class Person extends Student {
private String gender;
public Person(String name, int age, String gender) {
super(name, age);
this.gender = gender;
}
public void introduce() {
System.out.println("My name is " + name + ", I'm " + age + " years old, and I'm " + gender + ".");
}
}
```
在这个例子中,Person类继承了Student类,Person类拥有了Student类中的属性和方法,同时又增加了自己的属性和方法。在Person类的构造方法中,使用了super关键字调用了父类的构造方法。
阅读全文