1.抽象案例,雇员
雇员employee
需求:公司中有:程序员(programmer),姓名(name),工号(id),薪水(pay),工作内容(work)
项目经理(Manager),姓名(name),工号(id),薪水(pay),工作内容(work),奖金(bonus)
package 抽象;
public class AbstractTest1 {
public static void main(String[] args) {
Programmer pg = new Programmer();
pg.work();
Manager mk = new Manager();
mk.work();
}
}
abstract class Employee{
String name;
int id;
int pay;
//工作内容
/*public void work(){
System.out.println(".....");//因为不同的雇员所执行的工作不一样所以得把这个方法抽象
}*/
public abstract void work();
}
//具体的两个雇员类
class Programmer extends Employee{
@Override
public void work() {
System.out.println("程序员写代码");
}
}
class Manager extends Employee{
byte bonus;
@Override
public void work() {
System.out.println("项目经理接项目,监督程序员写代码");
}
}
2.抽象案例(老师)
package 抽象;
public class AbstractTeacher {
public static void main(String[] args) {
BasicTeacher bt = new BasicTeacher();
bt.name="聂小倩";
bt.teach();
JobTeacher jt= new JobTeacher();
jt.name="宁采臣";
jt.teach();
}
}
abstract class Teacher{
String name;
int age;
String gender;
/*public void teach(){
System.out.println("----");//写一个teach方法,讲什么课我们不清楚,所以我们要将这个方法改为抽象,同时类也得抽象
}*/
public abstract void teach();
}
//创建具体的老师类
class BasicTeacher extends Teacher{
@Override
public void teach() {
System.out.println(name+"将基础班的课程");
}
}
class JobTeacher extends Teacher{
public void teach() {
System.out.println(name+"将就业班的课程");
}
}