Working类:
/*
* 把打工这个功能封装在一个接口中
*/
public interface Working {
void sendAd(); //接口中方法默认使用public abstract修饰
}
Student类:
/*
* 定义学生类实现打工接口
* 一个类实现一个接口,需要重写接口中的抽象方法
* 这个类就具有了接口中指定的功能
*/
public class Student implements Working {
@Override
public void sendAd() {
System.out.println("学生发传单......");
}
}
Teacher类:
public class Teacher implements Working {
@Override
public void sendAd() {
System.out.println("发广告");
}
}
测试Demo类:
/*
* 打工的学生
*/
public class Demo {
public static void main(String[] args) {
Student stu1=new Student();
stu1.sendAd();
Teacher tc1=new Teacher();
tc1.sendAd();
}
}