已知学生类和老师类如下:
属性:姓名,年龄
行为:吃饭
老师有特有的方法:讲课
学生有特有的方法:学习
class Text2 {
public static void main(String[] args) {
Teacher t = new Teacher("王大锤",25);
System.out.println(t.getName()+" "+t.getAge());
t.eat();
t.teach();
Student s = new Student("王小锤",18);
System.out.println(s.getName()+" "+s.getAge());
s.eat();
s.study();
}
}
class Person{
private String name;
private int age;
public Person(){}
public Person(String name,int age){
this.name=name;
this.age=age;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public void eat(){
System.out.println("吃饭");
}
}
class Teacher extends Person{
public Teacher(){}
public Teacher(String name,int age){
super(name,age);
}
public void teach(){
System.out.println("讲课");
}
}
class Student extends Person{
public Student(){}
public Student(String name,int age){
super(name,age);
}
public void study(){
System.out.println("学生");
}
}
|
|