Java面向对象-接口

Printer.java

package com;

public interface Printer {
	void printer();
}

SchoolPrinter.java

package com;

public class SchoolPrinter {
	public void AddPrinter(Printer p){
		p.printer();
	}
}

addPrinter.java

package com;

public class addPrinter {

	public static void main(String[] args) {
		SchoolPrinter sp = new SchoolPrinter();
		ZhenPrinter zp = new ZhenPrinter();
		ColorPrinter cp=new ColorPrinter();
		ThreePrinter tp=new ThreePrinter();
		FourPrinter fp=new FourPrinter();
		sp.AddPrinter(zp);
		sp.AddPrinter(cp);
		sp.AddPrinter(tp);
		sp.AddPrinter(fp);
	}

}

ColorPrinter.java

package com;

public class ColorPrinter implements Printer{
	public void printer(){
		System.out.println("添加彩色打印机");
	}
}

FourPrinter.java

package com;

public class FourPrinter implements Printer{
	public void printer(){
		System.out.println("添加4D打印机");
	} 
}

ThreePrinter.java

package com;

public class ThreePrinter implements Printer{
	public void printer(){
		System.out.println("添加3D打印机");
	}
}

ZhenPrinter.java

package com;

public class ZhenPrinter implements Printer{
	public void printer() {
		System.out.println("添加针孔打印机");
		
	}
}

结果:

Java面向对象-接口_第1张图片

你可能感兴趣的:(Java学习)