需求二:
已知学生类和老师类如下:
属性:姓名,年龄
行为:吃饭
老师有特有的方法:讲课
学生有特有的方法:学习
public class Demo1 {
public static void main(String[] args) {
Teacher teacher=new Teacher("ssss",25);//创建老师对象
teacher.eat();//调用吃的方法
teacher.SpeakClass();//调用讲课
Student student=new Student("555",58);//创建学生类对象
student.eat();//调用吃的方法
student.study();//调用学习的方法
}
}
class Person{
private String name;//姓名
private int age;//年龄
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
//吃饭
public void eat(){
System.out.println(this.name+"吃饭");
}
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;
}
}
class Student extends Person{
public Student() {
super();
}
public Student(String name, int age) {
super(name,age);
}
//学习
public void study() {
System.out.println(this.getName()+"学习");
}
}
class Teacher extends Person{
public Teacher() {
super();
}
public Teacher(String name, int age) {
super(name,age);
}
//讲课
public void SpeakClass() {
System.out.println(this.getName()+"讲课");
}
} |
|