华恩JAVA班第24天
package com.zjj; public abstract class Printer { private String type; public Printer(String type) { this.type = type; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public abstract void print(); }
package com.zjj; public class LaserPrinter extends Printer{
public LaserPrinter(String type) { super(type); // TODO Auto-generatedconstructor stub }
@Override public void print() { // TODO Auto-generated methodstub System.out.println("打印机型号"+getType()+"打印速度快,噪音小,效果好"); } }
package com.zjj; public class InkpetPrinter extends Printer{
public InkpetPrinter(String type) { super(type); // TODO Auto-generatedconstructor stub }
@Override public void print() { // TODO Auto-generated methodstub System.out.println("打印机型号"+getType()+"打印效果介于针式和激光打印机之间"); } }
package com.zjj; public class DotMatrixPrinter extends Printer{
public DotMatrixPrinter(String type) { super(type); // TODO Auto-generatedconstructor stub }
@Override public void print() { // TODO Auto-generated methodstub System.out.println("打印机型号"+getType()+"打印速度慢,效果差,噪音高"); } }
package com.zjj; public class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } public void usePrinter(Printer[]printer){ for(int i=0;i printer[i].print(); } } }
package com.zjj; public class Test {
public static void main(String[] args) { // TODO Auto-generated methodstub User user = newUser("张三",15); Printer[] printer = newPrinter[3]; printer[0] = newLaserPrinter("\n"+"激光打印机"); printer[1] = newInkpetPrinter("\n"+"喷墨打印机"); printer[2] = newDotMatrixPrinter("\n"+"针式打印机"); user.usePrinter(printer); }
}
