package demo;
abstract class Person {
//私有数据成员
private String name;
public Person() {}
public Person(String name) {
this.name = name;
}
//getter和setter方法
public String getName() {
return name;
}
public void eating(String food) {
System.out.println(this.name + "正在吃" + food);
}
abstract public void workOn();//抽象方法
}
class Teacher extends Person {
//私有数据成员
private String department;
public Teacher() {}
public Teacher(String name,String department) {
super(name);
this.department = department;
}
//getter和setter方法
public void workOn() {
System.out.println(this.getName() + "在" + this.department + "工作");
System.out.println(this.getName() + "在讲授国学");
}
}
class Student extends Person {
//私有数据成员
private int id;
//构造方法
public Student() {}
public Student(String name, int id) {
super(name);
this.id = id;
}
//getter和setter方法
public int getId() {
return id;
}
//功能方法
public void workOn() {
System.out.println("学号" + this.id + ",姓名:" + this.getName());
}
}
public class Chapter0702 {
public static void main(String[] args) {
Teacher tch = new Teacher("孔子", "国学院");
tch.workOn();
Student std = new Student("子路",10001);
std.workOn();
}
}
标签:java,String,workOn,public,Student,抽象类,id,name
From: https://blog.csdn.net/dl20050314/article/details/137091800