类图,Person为类名,name,age为属性,name是String类型,age是int类型
Student类和Teacher类是Person类的子类
Person类中的move是成员方法,返回值为void,say()方法返回值为void,其他类同理
public class Test {
public static void main(String[] args) {
Student student=new Student("小白",18,"10001");
Teacher teacher=new Teacher("张老师",32,"2001");
System.out.println("学生姓名:"+student.getName());
student.study();
student.move();
student.say();
System.out.println("老师姓名:"+teacher.getName());
teacher.teach();
teacher.move();
teacher.say();
}
}
public class Person {
protected String name;
protected int age;
public void move(){
System.out.println("可以移动。。。");
}
public void say(){
System.out.println("可以说话。。。");
}
}
public class Student extends Person{
private String studentNo;
public Student(){}
public Student(String name,int age,String studentNo){
this.name=name;
this.age=age;
this.studentNo=studentNo;
}
public String getName(){
return name;
}
public void study(){
System.out.println("正在学习。。。");
}
}
public class Teacher extends Person{
private String teacherNo;
public Teacher(){}
public Teacher(String name,int age,String teacherNo){
this.name=name;
this.age=age;
this.teacherNo=teacherNo;
}
public String getName(){
return name;
}
public void teach(){
System.out.println("正在教书。。。");
}
}