JAVA基础之接口

来源:互联网 发布:java se6 for mac下载 编辑:程序博客网 时间:2024/05/09 14:56

JAVA基础之接口

目录(?)[+]

  1. 抽象类和抽象方法
  2. 接口
  3. 完全解耦
  4. Java的多重继承
  5. 通过继承来扩展接口
  6. 接口中的域
  7. 嵌套接口
  8. 接口与工厂

抽象类和抽象方法

  • 抽象方法
仅有声明没有方法体
  • 抽象类
包含抽象方法的类
如果一个类包含一个或多个抽象方法,那么该类必定是抽象类
如果继承一个抽象类,必须重写所有抽象方法,否则该子类也必须是抽象类
抽象类中可以包含非抽象方法
可以创建不包含任何抽象方法的抽象类
[java] view plaincopyprint?
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         Super mSuper = new Sub();  
  4.         mSuper.print(mSuper.getString());  
  5.         mSuper.show();  
  6.   
  7.         Base mBase = new Child();  
  8.         ((Child) mBase).function();  
  9.     }  
  10. }  
  11.   
  12. // 抽象类可以不包含抽象方法   
  13. // 包含抽象方法一定是抽象类   
  14. abstract class Base {}  
  15.   
  16. class Child extends Base {  
  17.     void function() {  
  18.         System.out.println("Child function");  
  19.     }  
  20. }  
  21.   
  22. abstract class Super {  
  23.     abstract void print(String string);  
  24.     abstract String getString();  
  25.   
  26.     void show() {  
  27.         System.out.println("Super show");  
  28.     }  
  29. }  
  30.   
  31. // 继承抽象类需要重新所有抽象方法   
  32. // 否则该类也必须是抽象类   
  33. abstract class ASuper extends Super {}  
  34.   
  35. class Sub extends ASuper {  
  36.     @Override  
  37.     void print(String string) {  
  38.         System.out.println(string + " print");  
  39.     }  
  40.   
  41.     @Override  
  42.     String getString() {  
  43.         return "Sub";  
  44.     }  
  45. }  
  46.   
  47. /** 
  48. 输出如下:  
  49. Sub print 
  50. Super show 
  51. Child function 
  52. */  

接口

接口和内部类为我们提供了一种将接口和实现分离的更加结构化的方法
  • interface关键字
interface这个关键字产生一个完全抽象的类,它根本就没有提供任何具体实现
它允许创建者确定方法名、参数列表和返回类型,但是没有任何方法体
接口可以包含域,但这些域隐式地是static和final的
  • implements关键字
实现接口使用implement关键字
一个类可以实现多个接口,只能继承一个类(单继承,多实现)
[java] view plaincopyprint?
  1. public class Test {  
  2.     // 策略模式   
  3.     public static void ploy(Super s,int n) {  
  4.         s.print(n);  
  5.     }  
  6.   
  7.     public static void main(String[] args) {  
  8.         Base mBase = new Sub();  
  9.         mBase.function();  
  10.   
  11.         // Super对象就是一种策略   
  12.         // Sub/Sub1 两种不同的策略应用到了int类型的num上   
  13.         int num = 2;  
  14.         ploy(new Sub(),num);  
  15.         ploy(new Sub1(), num);  
  16.     }  
  17. }  
  18.   
  19. interface Base {  
  20.     String string = "Base";  
  21.   
  22.     // 接口中方法默认是public的,即使不声明也是public的   
  23.     public void function();   
  24. }  
  25.   
  26. abstract class Super {  
  27.     abstract void print(int num);  
  28.     void show() {  
  29.         System.out.println(getClass().getSimpleName() + " show");  
  30.     }  
  31. }  
  32.   
  33. class Sub extends Super implements Base {  
  34.     @Override  
  35.     void print(int num) {  
  36.         //string = "Child"; 隐式是final的,不能修改   
  37.         System.out.println(getClass().getSimpleName() + " " + num*100);  
  38.     }  
  39.   
  40.     @Override  
  41.     public void function() {  
  42.         System.out.println("Sub function " + string);  
  43.     }  
  44. }  
  45.   
  46. class Sub1 extends Super {  
  47.     @Override  
  48.     void print(int num) {  
  49.         System.out.println(getClass().getSimpleName() + " " + (num+100));  
  50.     }  
  51. }  
  52.   
  53. /** 
  54. 输出如下:  
  55. Sub function Base 
  56. Sub 200 
  57. Sub1 102 
  58. */  

完全解耦

将接口从具体实现中解耦使得接口可以应用于多种不同的具体实现
[java] view plaincopyprint?
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         String input = "Test";  
  4.         // 这里使用了适配器模式   
  5.         // 将LowPass,HighPass,BandPass为Apply.process方法做适配   
  6.         Apply.process(new FilterAdapter(new LowPass(1.0)), input);  
  7.         Apply.process(new FilterAdapter(new HighPass(2.0)), input);  
  8.         Apply.process(new FilterAdapter(new BandPass(1.02.0)), input);  
  9.     }  
  10. }  
  11.   
  12. interface Processor {  
  13.     String name();  
  14.     Object process(Object input);  
  15. }  
  16.   
  17. class Apply {  
  18.     public static void process(Processor s, Object o) {  
  19.         System.out.println(s.process(o));  
  20.     }  
  21. }  
  22.   
  23. // FilterAdapter使用了代理   
  24. // 实际上是Filter的代理   
  25. class FilterAdapter implements Processor {  
  26.     Filter mFilter;  
  27.   
  28.     public FilterAdapter(Filter mFilter) {  
  29.         this.mFilter = mFilter;  
  30.     }  
  31.   
  32.     public String name() {  
  33.         return mFilter.name();  
  34.     }  
  35.   
  36.     public Object process(Object input) {  
  37.         return mFilter.process((String) input);  
  38.     }  
  39. }  
  40.   
  41. class Filter {  
  42.     String name() {  
  43.         return getClass().getSimpleName();  
  44.     }  
  45.   
  46.     public String process(String input) {  
  47.         return input;  
  48.     }  
  49. }  
  50.   
  51. class LowPass extends Filter {  
  52.     double cutoff;  
  53.   
  54.     public LowPass(double cutoff) {  
  55.         this.cutoff = cutoff;  
  56.     }  
  57.   
  58.     public String process(String input) {  
  59.         return input + " " + cutoff;  
  60.     }  
  61. }  
  62.   
  63. class HighPass extends Filter {  
  64.     double cutoff;  
  65.   
  66.     public HighPass(double cutoff) {  
  67.         this.cutoff = cutoff;  
  68.     }  
  69.   
  70.     public String process(String input) {  
  71.         return input + " " + cutoff;  
  72.     }  
  73. }  
  74.   
  75. class BandPass extends Filter {  
  76.     double lowCutoff, highCutoff;  
  77.   
  78.     public BandPass(double lowCutoff, double highCutoff) {  
  79.         this.lowCutoff = lowCutoff;  
  80.         this.highCutoff = highCutoff;  
  81.     }  
  82.   
  83.     public String process(String input) {  
  84.         return input + " " + lowCutoff + " " + highCutoff;  
  85.     }  
  86. }  

Java的多重继承

在导出类中,只能继承一个类,可以实现多个接口
接口可以继承接口,并且可以同时继承多个接口
  • 使用接口的原因
1.为了能够向上转型为多个基本类型2.防止客户端程序员创建该类的对象
[java] view plaincopyprint?
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         CanFly Hero1 = new Hero();  
  4.         Hero1.fly();  
  5.   
  6.         CanSwim hero2 = new Hero();  
  7.         hero2.swim();  
  8.   
  9.         CanFight hero3 = new Hero();  
  10.         hero3.fight();  
  11.   
  12.         Super hero4 = new Hero();  
  13.         hero4.superSkill();  
  14.         hero4.swim();  
  15.         hero4.fight();  
  16.   
  17.         Action hero5 = new Hero();  
  18.         hero5.action();  
  19.     }  
  20. }  
  21.   
  22. interface CanFly {  
  23.     void fly();  
  24. }  
  25.   
  26. interface CanSwim {  
  27.     void swim();  
  28. }  
  29.   
  30. interface CanFight {  
  31.     void fight();  
  32. }  
  33.   
  34. //接口可以多继承   
  35. interface Super extends CanFight, CanSwim {  
  36.     void superSkill();  
  37. }  
  38.   
  39. abstract class Action {  
  40.     abstract void action();  
  41. }  
  42.   
  43. class NewAction extends Action {  
  44.     @Override  
  45.     void action() {}  
  46. }  
  47.   
  48. // 只能继承一个类,可以实现多个接口   
  49. class Hero extends NewAction implements CanFly, Super {  
  50.     public void fight() {}  
  51.     public void swim() {}  
  52.     public void fly() {}  
  53.     public void superSkill() {}  
  54.   
  55.     @Override  
  56.     void action() {}  
  57. }  

通过继承来扩展接口

  • 组合接口时的名字冲突
[java] view plaincopyprint?
  1. interface CanFly {  
  2.     void fly();  
  3. }  
  4.   
  5. class SuperMan {  
  6.     int fly() {return 1;}  
  7. }  
  8.   
  9. // 编译报错   
  10. // The return types are incompatible for the inherited methods CanFly.fly(), SuperMan.fly()   
  11. class Hero extends SuperMan implements CanFly {}  
[java] view plaincopyprint?
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         Hero hero = new Hero();  
  4.         hero.fly();  
  5.     }  
  6. }  
  7.   
  8. interface CanFly {  
  9.     void fly();  
  10. }  
  11.   
  12. class SuperMan {  
  13.     public void fly() {  
  14.         System.out.print("SuperMan");  
  15.     }  
  16. }  
  17.   
  18. // 编译不会报错,因为SuperMan的fly()会被作为CanFly.fly()的实现   
  19. class Hero extends SuperMan implements CanFly {}  

接口中的域

接口中的任何域都自动是static和final的
  • 初始化接口中的域
接口中的域需要显示的初始化
接口中定义的域不能是“空final”,但是可以被非常量表达式初始化
[java] view plaincopyprint?
  1. interface Processor {  
  2.     // 必须显式初始化   
  3.     int CORE = 8;  
  4.     // 可以以非常量表达式初始化   
  5.     int THREAD = new Random().nextInt();  
  6. }  

嵌套接口

接口可以嵌套在类和其他接口中
接口也可以被实现为private
[java] view plaincopyprint?
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         Skill skill = new Skill();  
  4.         // The type Skill.IFight is not visible   
  5.         //Skill.IFight fight = skill.getFight();   
  6.   
  7.         // The type Skill.IFight is not visible   
  8.         //skill.getFight().fight();   
  9.   
  10.         skill.setFight(new Skill().getFight());  
  11.     }  
  12. }  
  13.   
  14. class Skill {  
  15.     interface IRun {  
  16.         void run();  
  17.     }  
  18.   
  19.     public interface IEat {  
  20.         void eat();  
  21.     }  
  22.   
  23.     // private接口不能在定义它的类之外被实现   
  24.     private interface IFight {  
  25.         void fight();  
  26.     }  
  27.   
  28.     public class Run implements IRun {  
  29.         @Override  
  30.         public void run() {}  
  31.     }  
  32.   
  33.     // 接口也可以被实现为private   
  34.     private class Eat implements IEat {  
  35.         @Override  
  36.         public void eat() {}  
  37.     }  
  38.   
  39.     // private 接口可以被实现为public   
  40.     public class Fight implements IFight {  
  41.         @Override  
  42.         public void fight() {  
  43.             System.out.print("fighting!");  
  44.         }  
  45.     }  
  46.   
  47.     public IFight getFight() {  
  48.         return new Fight();  
  49.     }  
  50.   
  51.     private IFight fighter;  
  52.   
  53.     public void setFight(IFight fight) {  
  54.         fighter = fight;  
  55.         fighter.fight();  
  56.     }  
  57. }  
  58.   
  59. interface ISuperMan {  
  60.     // 接口中可以嵌套接口   
  61.     // 自动为public,添加public是多余的   
  62.     interface IFly {  
  63.         void fly();  
  64.     }  
  65.   
  66.     void savePerson();  
  67. }  
  68.   
  69. class Boss {  
  70.     class Run implements Skill.IRun {  
  71.         @Override  
  72.         public void run() {}  
  73.     }  
  74.   
  75.     class Eat implements Skill.IEat {  
  76.         @Override  
  77.         public void eat() {}  
  78.     }  
  79.   
  80.     class SuperMan implements ISuperMan {  
  81.         @Override  
  82.         public void savePerson() {}  
  83.   
  84.         class Fly implements ISuperMan.IFly {  
  85.             @Override  
  86.             public void fly() {}  
  87.         }  
  88.     }  
  89.   
  90.     class FlyImpl implements ISuperMan.IFly {  
  91.         @Override  
  92.         public void fly() {}  
  93.     }  
  94. }  
  95. /** 
  96. 输出如下:  
  97. fighting! 
  98. */  

接口与工厂

接口是实现多重继承的途径
[java] view plaincopyprint?
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         Games.GameConsumer(new CheckersFactory());  
  4.         Games.GameConsumer(new ChessFactory());  
  5.     }  
  6. }  
  7.   
  8. // 使用了工厂模式   
  9. interface IGame {  
  10.     boolean move();  
  11. }  
  12.   
  13. interface IGameFactory {  
  14.     IGame getGame();  
  15. }  
  16.   
  17. class Checkers implements IGame {  
  18.     @Override  
  19.     public boolean move() {  
  20.         System.out.println("Checkers move");  
  21.         return true;  
  22.     }  
  23. }  
  24.   
  25. class Chess implements IGame {  
  26.     @Override  
  27.     public boolean move() {  
  28.         System.out.println("Chess move");  
  29.         return true;  
  30.     }  
  31. }  
  32.   
  33. class CheckersFactory implements IGameFactory {  
  34.     @Override  
  35.     public IGame getGame() {  
  36.         return new Checkers();  
  37.     }  
  38. }  
  39.   
  40. class ChessFactory implements IGameFactory {  
  41.     @Override  
  42.     public IGame getGame() {  
  43.         return new Chess();  
  44.     }  
  45. }  
  46.   
  47. // 不同类型的游戏都可以复用这段代码   
  48. class Games {  
  49.     public static void GameConsumer(IGameFactory factory) {  
  50.         IGame game = factory.getGame();  
  51.         game.move();  
  52.     }  
  53. }  
  54. /** 
  55. 输出如下:  
  56. Checkers move 
  57. Chess move 
  58. */  
0 0
原创粉丝点击