帮我写一段含有两个类的java代码
时间: 2024-05-10 10:19:56 浏览: 7
好的,以下是一段含有两个类的java代码示例:
```
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 class Student extends Person {
private String school;
private double gpa;
public Student(String name, int age, String school, double gpa) {
super(name, age);
this.school = school;
this.gpa = gpa;
}
public String getSchool() {
return school;
}
public double getGpa() {
return gpa;
}
public void setSchool(String school) {
this.school = school;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
}
```
这段代码包含了两个类,一个是 `Person` 类,另一个是 `Student` 类,`Student` 类是 `Person` 类的子类。`Person` 类有两个字段——`name` 和 `age`,一个构造函数和两个访问器方法;`Student` 类有两个额外的字段——`school` 和 `gpa`,并且添加了两个设置器方法,以及一个构造函数用于初始化对象。