java OOP -接口
package com.s2b.ch3;
//接口定义一个方法
public interface Introduceable {
public String detail();
}
---------------end^-^-------------------
package com.s2b.ch3;
//学生类继承接口定义方法
public class AccpSchool implements Introduceable {
public String detail() {
return "这里是ACCP中心";
}
//Printer p=new Printer();
Printable printer;
//实现接口打印机
public void setPrinter(Printable p){
printer=p;
}
//运行接口中的方法
public void print(Introduceable ins)
{
printer.print(ins.detail());
}
}
---------------end^-^-------------------
package com.s2b.ch3;
//学生类有接口 ,定义一个方法
public class AccpStudent implements Introduceable {
public String detail() {
return "我是一个ACCP学员";
}
}
---------------end^-^-------------------
package com.s2b.ch3;
//教师类,继承接口
public class AccpTeacher implements Introduceable {
public String detail() {
return "本人是ACCP教员";
}
}
---------------end^-^-------------------
package com.s2b.ch3;
//一个接口打印机
public interface Printable {
public void print(String content);
}
---------------end^-^-------------------
package com.s2b.ch3;
//继承接口打印机 黑白打印
public class BlackPrinter implements Printable {
public void print(String content) {
System.out.println("黑白打印:"+content);
}
}
---------------end^-^-------------------
package com.s2b.ch3;
//继承接口打印机 彩色打印
public class ColorPrinter implements Printable {
public void print(String content) {
System.out.println("彩色打印:"+content);
}
}
---------------end^-^-------------------
package com.s2b.ch3;
//打印机
public class Printer {
public void print(String content)
{
System.out.println(content);
}
}
---------------end^-^-------------------
package com.s2b.ch3;
public class Test {
//主方法
public static void main(String[] args) {
AccpSchool as=new AccpSchool();
AccpTeacher at=new AccpTeacher();
as.setPrinter(new BlackPrinter());
//黑白打印:这里是ACCP中心
as.print(as);
//黑白打印:本人是ACCP教员
as.print(at);
//黑白打印:我是一个ACCP学员
AccpStudent student=new AccpStudent();
as.print(student);
}
}