abstract class A{ //抽象方法只有声明而没有方法体 abstract void f(); }
包含抽象方法的类称为抽象类,一个类包含一个或多个abstract方法,该类必须限定为abstract的。
抽象类的子类可以不是抽象类,但要为父类的所有抽象方法提供方法定义,否则这个子类也必须是抽象类。
package Nine; abstract class A{ abstract void print(); A(){ print(); } } //抽象类的子类可以不是抽象类 class B extends A{ int i = 5; @Override void print() { // TODO Auto-generated method stub System.out.println(i); } } public class AbstractPrint { public static void main(String[] args){ B b = new B();//0 这是由于在new B()时先调用了构造器A(),此时将i初始化为0了 b.print();//5 } }/* 0 5 */
无法为抽象类创建任何实例,只能作为其他类继承的父类。
package Nine; abstract class Rodent{ abstract void animal(); abstract String name(String name); abstract void eat(); } abstract class Mouse1{} class Mouse extends Rodent{ @Override void animal() { // TODO Auto-generated method stub System.out.println("a type of Rodent"); } @Override String name(String name) { // TODO Auto-generated method stub return name.toString(); } @Override void eat() { // TODO Auto-generated method stub System.out.println("eat anything"); } } public class ShuTest { public static void main(String[] args){ //Rodent r = new Rodent();//cannot instantiate the type Rodent // Mouse1 m1 = new Mouse1();cannot instantiate不能为抽象类创建任何实例 Mouse m = new Mouse(); m.animal(); m.eat(); System.out.println(m.name("Mouse")) ; } }/*a type of Rodent eat anything Mouse*/
接口 interface
接口使抽象的概念更进一步,产生一个完全抽象的类,不提供任何具体实现。
一个接口表示:所有实现了该接口的类看起来都像这样。类似一种模式”,一种“协议”。
interface中的方法自动是public的。
接口包含的域隐式的为public&&static&&final的,接口中定义的域不能是空final必须要初始化
:
package Nine; //interface中的域隐式是public&&static&&final interface AX{ int I = 0;//接口中定义的域不能是空final必须要初始化 String COLOR = "red"; } class BX implements AX{ public void B(){ //COLOR = "blue";//the final field COLOR cannot be assigned } } public class InterfaceStaticFinal { public static void main(String[] args){ System.out.println(AX.COLOR + AX.I); } }/*red0*/
策略设计模式:能够创建一个能够根据所传递的参数对象的不同而具有不同行为的方法。
适配器设计模式:适配器中的代码将接受你所拥有的接口并产生你需要的接口。
//ProcessorD.java package Nine; public interface ProcessorD { String name(); Object process(Object input); }
//Apply.java package Nine; public class Apply { public static void process(ProcessorD p,Object s){ System.out.println("Using Processor: " + p.name() ); System.out.println(p.process(s)); } }
策略设计模式:Apply.process( )方法可以接受任何类型的Processor,并将其应用到一个对象上。
package Nine; class Waveform{ private static long counter; private final long id = counter++; public String toString(){return "waveform "+id;} } class Filter{ public String name(){ return getClass().getSimpleName(); } public Waveform process(Waveform input){ return input;} } class LowPass extends Filter{ double cutoff; public LowPass(double cutoff){this.cutoff = cutoff;} public Waveform process(Waveform input){return input;} } class HighPass extends Filter{ double cutoff; public HighPass(double cutoff){this.cutoff = cutoff;} public Waveform process(Waveform input){return input;} } class BandPass extends Filter{ double lowCutoff,highCutoff; public BandPass(double lowCut,double highCut){ lowCutoff = lowCut; highCutoff = highCut; } public Waveform process(Waveform input){return input;} } //适配器设计模式:FilterAdapter接受所拥有的接口Filter,然后产生所需要的Processor接口对象。 //FilterAdapter实现了ProcessorD class FilterAdapter implements ProcessorD{ Filter filter; public FilterAdapter( Filter filter){ this.filter = filter; } public String name() { // TODO Auto-generated method stub return filter.name(); } public Object process(Object input) { // TODO Auto-generated method stub return filter.process((Waveform)input); } } public class WaveFormFilter { public static void main(String[] args){ Waveform w = new Waveform(); Apply.process(new FilterAdapter( new LowPass(1.0)), w); Apply.process(new FilterAdapter(new HighPass(2.0)), w); Apply.process(new FilterAdapter(new BandPass(3.0,5.0)), w); } }/* Using Processor: LowPass waveform 0 Using Processor: HighPass waveform 0 Using Processor: BandPass waveform 0 */
将三种不同类型的策略(三种不同的Filter:LowPass/HighPass/BandPass)应用到w上。
接口与类的多重继承
接口继承接口遵循类继承类的语法。
类既继承类又实现接口时,extends 类必须放implements接口的前面。implements多个接口时,接口之间用“,”隔开。
嵌套在一个接口中的接口自动是public的。
package Nine; interface AA{ void A1(); void A2(); } interface BB{ void B1(); void B2(); } interface CC{ void C1(); void C2(); } interface DD extends AA,BB,CC{ void A1(); void A2(); void B1(); void B2(); void C1(); void C2(); void D(); } abstract class ABC{ ABC(){ System.out.println("ABC"); } } //extends 类放前面implements 接口放后面 class D1 extends ABC implements DD{ public void A1() { // TODO Auto-generated method stub System.out.println("A1"); } public void A2() { // TODO Auto-generated method stub System.out.println("A2"); } public void B1() { // TODO Auto-generated method stub System.out.println("B1"); } public void B2() { // TODO Auto-generated method stub System.out.println("B2"); } public void C1() { // TODO Auto-generated method stub System.out.println("C1"); } public void C2() { // TODO Auto-generated method stub System.out.println("C2"); } public void D() { // TODO Auto-generated method stub System.out.println("D"); } } public class InterfaceD { static void A3(AA a){a.A1();a.A2();} static void B3(BB b){b.B1();b.B2();} static void C3(CC c){c.C1();c.C2();} static void D(DD d){d.A1();d.A2();} public static void main(String[] args){ D1 d = new D1();//ABC A3(d);//A1 A2 B3(d); C3(d); D(d); } }/* ABC A1 A2 B1 B2 C1 C2 A1 A2*/
工厂方法:接口是实现多重继承的途径,生成遵循某个接口的对象的典型方式就是工厂方法设计模式。与调用构造器不同,在工厂对象上调用的是工厂接口中创建的方法,该工厂对象将生成接口的某个实现的对象。通过这种方式,代码与接口实现分离。
package Nine; //工厂方法,使用工厂方法的常见原因是想要创建框架 interface Cycle{ void name(); void go(); } interface CycleFactory{ Cycle getCycle(); } class Unicycle implements Cycle{ public void name() { // TODO Auto-generated method stub System.out.println("Unicycle()"); } public void go() { // TODO Auto-generated method stub System.out.println("Unicyclego()"); } } class UniFactory implements CycleFactory{ public Cycle getCycle() { // TODO Auto-generated method stub return new Unicycle(); } } class Bike implements Cycle{ public void name() { // TODO Auto-generated method stub System.out.println("Bike"); } public void go() { // TODO Auto-generated method stub System.out.println("Bikego()"); } } class BikeFactory implements CycleFactory{ public Cycle getCycle() { // TODO Auto-generated method stub return new Bike(); } } public class FactoryInterface { public static void Play(CycleFactory cy){ Cycle cycle = cy.getCycle(); System.out.println(cycle); } public static void main(String[] args){ Play(new UniFactory()); Play(new BikeFactory()); } }/* Nine.Unicycle@19e0bfd
package Nine; //用工厂方法创建一个框架,它可以执行抛硬币、掷骰子功能 interface Toss{boolean event();} interface TossFactory{ Toss getToss(); } class CoinToss implements Toss{ private int event; private static int EVENTS = 2; public boolean event() { // TODO Auto-generated method stub System.out.println("Coin Toss "+ event); return ++event != EVENTS; }} class CoinFactory implements TossFactory{ public Toss getToss() { // TODO Auto-generated method stub return new CoinToss(); } } class DiceToss implements Toss{ private int event = 1; private static int EVENTS = 7; public boolean event() { // TODO Auto-generated method stub System.out.println("Dice Toss "+ event); return ++event != EVENTS; } } class DiceFactory implements TossFactory{ public Toss getToss() { // TODO Auto-generated method stub return new DiceToss(); }} public class FactoryCoin { public static void TossPlay(TossFactory tf){ Toss ts = tf.getToss(); while(ts.event()) ; } public static void main(String[] args){ TossPlay(new CoinFactory()); TossPlay(new DiceFactory()); } }/* Coin Toss 0 Coin Toss 1 Dice Toss 1 Dice Toss 2 Dice Toss 3 Dice Toss 4 Dice Toss 5 Dice Toss 6*/