• 接口


    接口:是抽象方法和全局常量组成

    接口定义:

          interface 接口名

          {

            全局常量;public  static  final  全局常量名

            抽象方法;public  abstract  void  方法名(){}

          }

    接口的实现:

          class  类名  implement  接口名称{}

    接口中只有public权限,成员变量只能是final或静态

    一个类只能继承一个类,但能实现多个接口

      class  类名  implement  ab{}

    如果一个类即要实现接口又要继承抽象类的话,例如:

      class  类名 extends c implement  ab{}

    一个接口不能继承一个抽象类,但是一个接口能够继承多个接口。

    接口也能用来声明对象。

     

    (1)接口实例

    接口也可以同过多态性实例化。

    interface A{

           public void fun() ;

    }

    class B implements A{

           public void fun(){

                  System.out.println("Hello") ;

           }

    };

    public class InterPolDemo01{

           public static void main(String args[]){

                  A a = new B() ;      // 为接口实例化

                  a.fun() ;

           }

    };

    (2)图形问题

    Powerdesigner软件,尖括号表示两个类的继承关系,如果是抽象类则在类名旁边写上abstract关键字;如果是一个虚线的尖括号,然后在类旁边有“()”则表示的接口。

    二十四,接口和抽象类

    如果想选择性的实现接口中的方法,则设置一个抽象类去实现这个接口,此抽象类中对接口中的方法设置为空实现,则就可以在其他继承了该抽象类的子类中,可以选择性的实现接口中的方法

    interface Window{

           public void open() ;       // 打开窗口

           public void close() ;       // 关闭窗口

           public void icon() ; // 最小化

           public void unicon() ;     // 最大化

    }

    abstract class WindowAdapter implements Window{

           public void open(){}

           public void close(){}

           public void icon(){}

           public void unicon(){}

    };

    class MyWindow extends WindowAdapter{

           public void open(){

                  System.out.println("打开窗口!") ;

           }

    };

    public class AdpaterDemo{

           public static void main(String args[]){

                  Window win = new MyWindow() ;

                  win.open() ;

           }

    }

    主方法实际上就是一个客户端。

    所有接口的实例化对象都是通过工厂类取得的,那么客户端调用的时候根据传入的名称不同,完成不同的功能。

    如果碰到字符串对象与常量的比较,则要把常量放到外面,即:常量.equals(字符串)

    (3)代理模式

    interface Give{

           public void giveMoney() ;

    }

    class RealGive implements Give{

           public void giveMoney(){

                  System.out.println("把钱还给我。。。。。") ;

           }

    };

    class ProxyGive implements Give{       // 代理公司

           private Give give = null ;

           public ProxyGive(Give give){

                  this.give = give ;

           }

           public void before(){

                  System.out.println("准备:") ;

           }

           public void giveMoney(){

                  this.before() ;

                  this.give.giveMoney() ;  // 代表真正的讨债者完成讨债的操作

                  this.after() ;

           }

           public void after(){

                  System.out.println("销毁所有罪证") ;

           }

    };

    public class ProxyDemo{

           public static void main(String args[]){

                  Give give = new ProxyGive(new RealGive()) ;

                  give.giveMoney() ;

           }

    };

  • 相关阅读:
    5个排序算法
    原生侧边栏sidebar
    静态方法、实例方法、继承
    函数作用域之闭包与this!
    OOP面向对象编程(下)
    数组方法篇二
    对象
    nginx windows负载均衡入门
    NVelocity
    python3简单爬虫
  • 原文地址:https://www.cnblogs.com/jinzhengquan/p/1941535.html
Copyright © 2020-2023  润新知