接口与抽象类的使用上的区别和选择:
首先,对于抽象类它的作用主要用于继承和重写,里面可以有普通方法,但在设计上面,通常把其派生类的共有属性和方法抽象出来,其中所有派生类中不变的方法,在抽象类中用普通方法写出来,而所有派生类中变化的方法用抽象方法写出来。
另外,对于接口主要作用是实现多继承,它也包含抽象方法,但不允许有普通方法,也不允许有构造器,因为它不是类,在设计的时候派生类必须实现接口中所有的所有的抽象方法,否则这个时候就要用抽象类了,当然这个类也可以继承其它的类并实现该接口。
-----------------------------------------------------------------------------------
抽象类的使用案例:
比如飞机大战中FlyingObject这个抽象类,其中包含了step() 、getImage()这两个方法都是不一样的实现方式,所以这里就要用抽象方法,另外,loadImage() 、paintObject()等方法实现上都类似,所以用普通方法。然后派生类再去用super()去调用和重写相关方法。
说明:派生类必须要完全覆盖超类的抽象方法,否则,该派生类也必须声明为抽象类,这一点要注意。
接口的使用案例:
1 /*能起飞的功能的接口*/ 2 public interface Flyer{ 3 /*三个抽象方法:起飞、飞行、降落*/ 4 public void takeoff(); 5 public void fly(); 6 public void land(); 7 } 8 /*飞机类实现飞行接口*/ 9 class Airplane implements Flyer{ 10 11 @Override 12 public void takeoff(){ 13 System.out.println("飞机起飞"); 14 } 15 public void fly(){ 16 System.out.println("飞机飞行中"); 17 } 18 public void land(){ 19 System.out.println("飞机着陆"); 20 } 21 } 22 /*鸟类继承动物类并是实现飞行接口*/ 23 class Bird extends Animal implements Flyer{ 24 public void eat(){ 25 System.out.println("鸟吃虫"); 26 } 27 28 @Override 29 public void takeoff(){ 30 System.out.println("鸟起飞"); 31 } 32 public void fly(){ 33 System.out.println("鸟起飞"); 34 } 35 public void land(){ 36 System.out.println("鸟着陆"); 37 } 38 39 } 40 41 /*测试方法1:*/ 42 public class InterfaceTest{ 43 public static void main(String[] args) { 44 Flyer f=new Airplane();//一种实现 45 f.takeoff(); 46 f.land(); 47 48 Flyer f1=new Bird();//一种实现 49 f1.takeoff(); 50 f.fly(); 51 } 52 } 53 54 /*测试方法2:*/ 55 public class InterfaceTest{ 56 public static void main(String[] args) { 57 Flyer f=new Airplane();//一种实现 58 InterfaceTest test=new InterfaceTest(); 59 test.oip(f);//传入Flyer接口的具体实现类的Airplane对象 60 test.oip(new Bird()); 61 } 62 public void oip(Flyer flyer){//方法参数类型为接口类型 63 System.out.println("飞得更高!"); 64 flyer.fly(); 65 } 66 }
1 /*多接口案例*/ 2 interface G4Phone{ 3 void online();//上网 4 void videoCall();//视频电话 5 } 6 interface PocketPC{ 7 void installProgram();//安装程序 8 void runProgram(); 9 } 10 class XiaoMiPC implements G4Phone,PocketPC{ 11 //... 12 } 13 public class MultipleInterfaceTest{ 14 public static void main(String[] args) { 15 16 } 17 } 18 19 /*当然,还有其它例子,如人、老师、丈夫、儿子,老师可以同时 20 实现丈夫和儿子这两个接口*/
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
说明:接口可以多实现,即一个产品实现多个接口,必须实现所有接口中的声明方法。
package interfaceE; //定义一个接口和抽象方法 public interface Define { int a=45; int b=55; void abc(); } package interfaceE; //重写父接口方法 public class Test1 implements Define { @Override public void abc() { System.out.println("Connection of test1!!"); } } package interfaceE; //重写父接口方法,定义子类并在main中调用两个派生类 public class Test2 implements Define { @Override public void abc() { System.out.println("Connection of test2!!"); } public static void main(String[] args) { Test1 c=new Test1(); Test2 d=new Test2(); c.abc(); d.abc(); System.out.println(a); System.out.println(b); System.out.println(c); } }
//
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
接口和抽象类的相同点:
都不能在里面创建对象,都可以包含抽象方法,但抽象类也可以不包含抽象方法,但一般都包含。