public class Test {
public static void main(String[] args) {
// i.创建老师对象t,并把名称赋值为”王小平”,年龄赋值为30,工资赋值为8000
Teacher t = new Teacher("王小平", 30, "Java");
// iii.调用老师对象t的讲解方法
t.teach();
// iv.创建学生对象 s,并把名称赋值为”李小乐”,年龄赋值为14,成绩赋值为90分.
Student s = new Student("李小乐", 14, 90);
// vi.调用学生对象 s 的考试方法
s.exam();
}
}
class Person {
// 名称(name)
private String name;
// 年龄(age)
private int age;
// 空参构造
public Person() {
}
// 带参构造
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// setXxx和getXxx方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
/*
2.定义老师类(Teacher),继承Person类
*/
class Teacher extends Person {
// course(科目)
private String course;
// 空参构造
public Teacher() {
}
// 带参构造方法
public Teacher(String name,int age, String course) {
super(name,age);
this.course = course;
}
// 提供setXxx和getXxx方法
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public void teach() {
System.out.println(getName() +"老师,讲授"+course +"课");
}
}
/*
3.定义学生类(Student),继承Person类
*/
class Student extends Person {
// score(成绩)
private int score;
// 无参构造
public Student() {
super();
}
// 带参构造
public Student(String name, int age,int score) {
super(name, age);
this.score = score;
}
// 提供setXxx和getXxx方法
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public void exam(){
System.out.println(getName()+"同学,考试得了:"+ score +"分");
}
} |
|