public interface Person { void introduce(); }
public class Student implements Person { public Student() { // TODO Auto-generated constructor stub } public void introduce() { System.out.println("我要去聽課"); } }
public class Teacher implements Person { public Teacher() { // TODO Auto-generated constructor stub } @Override public void introduce() { // TODO Auto-generated method stub System.out.println("我要去講課"); } }
實作方法
public class Introduce implements Person { Person person; public Introduce() { // TODO Auto-generated constructor stub } public Introduce(Person person) { this.person = person; } @Override public void introduce() { // TODO Auto-generated method stub System.out.println("大家好"); person.introduce(); } }
主函式
public class Test { public Test() { // TODO Auto-generated constructor stub } public static void main(String[] args) { // TODO Auto-generated method stub Student student = new Student(); Introduce sIntroduce = new Introduce(student); sIntroduce.introduce(); Teacher teacher = new Teacher(); Introduce tIntroduce = new Introduce(teacher); tIntroduce.introduce(); } }
運行結果
大家好
我要去聽課
大家好
我要去講課
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/6573.html
標籤:Java
下一篇:Swoole 協程簡介
