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
最新推荐文章于 2024-11-04 22:26:34 发布
本文通过Java代码展示了如何创建一个抽象类`Person`,以及其子类`Teacher`和`Student`的实现,介绍了抽象方法、构造方法和继承等面向对象编程概念的应用。
摘要由CSDN通过智能技术生成