java接口的妙用

    技术2022-05-11  168

    接口的妙用

                                                彭建雄

    关键字

    java,interface

    说明

    一气之作,文字功夫太差请大家谅解,希望能对研究COM的朋友带点帮助

    接口功能介绍

    1、‘纯’抽象类的实现(参见JAVA编程思想P/153

    // Interface1.java 接口只负责描述自己的样子“对于实现我的所有的类,看起来都应该象我这个样子。我所有的方法,实现类都必须有!”

    public interface Interface1 {

      public void setS(String str);

      public String getS();

      public void ShowMessage(String MSG);

    }

    =====================================================================

    // ClassItf.java  “接口只是一个非常‘纯‘的抽象的东西,你的实现代码都在我这里!”

    public class ClassItf implements Interface1{

      public String S = "";

      public ClassItf() {

      }

      public void ShowMessage(String MSG)

      {

        System.out.print(this.getClass().getName()+"====="+MSG+"====by Interface1n/");  }

      public void setS(String str)

      {

        S = str;

      }

      public String getS()

      {

        return S;

      }

    }

    2、多重继承的实现(参见JAVA编程思想P/155

    //Interfase2.java  接口的样子

    public interface Interface2 {

      public void ShowMessage2(String MSG);

    }

     

    // ClassItf.java 加入多重继承后的ClassItf 灰底为加入更改、插入行

    public class ClassItf implements Interface1,Interface2{

      public String S = "";

      public ClassItf() {

      }

      public void ShowMessage(String MSG)

      {

        System.out.print(this.getClass().getName()+"====="+MSG+"====by Interface1n/");

      }

      public void setS(String str)

      {

        S = str;

      }

      public String getS()

      {

        return S;

      }

      public void ShowMessage2(String MSG)

      {

        System.out.print(this.getClass().getName()+"====="+MSG+"====by Interface2n/");

      }

    }

    呵呵!我的多重继承功能在JAVA中有着大量的应用如:

    public class JFrame  extends Frame implements WindowConstants, Accessible, RootPaneContainer

    3、外观与实现分离

    // ClassItf.java  “接口只是一个非常‘纯‘的抽象的东西,你的实现代码都在我这里!”

    public class ClassItf implements Interface1,Interface2{

     

      public String S = "";

      public ClassItf() {

      }

      public void ShowMessage(String MSG)// 实现了接口‘Interface1ShowMessage

      {

        System.out.print(this.getClass().getName()+"====="+MSG+"====by Interface1n/");

     
      }

      public void setS(String str) //实现了接口‘Interface1setS

     

      {

        S = str;

      }

      public String getS()//实现了接口‘Interface1getS

     

     

      {

        return S;

      }

      public void ShowMessage2(String MSG)// 实现了接口‘Interface2 ShowMessage2

      {

        System.out.print(this.getClass().getName()+"====="+MSG+"====by Interface2n/");

      }

    }

     

    4、提供调用的影子

      public void ShowMessage(Interface1 req)

      {

        req.ShowMessage("111");

      }

      public void ShowMessage2(Interface2 req)

      {

        req.ShowMessage2("111");

      }

     

      void jButton1_actionPerformed(ActionEvent e) {

        private ClassItf C1 = new ClassItf();

        ShowMessage(C1);

        ShowMessage2(C1);

      }

    同样呀,我的应用也非常的广呀!

    比如在Servlet

    public void doPost(HttpServletRequest request,HttpServletResponse response)

    public void doGet(HttpServletRequest request,HttpServletResponse response)

    HttpServletRequest ,HttpServletResponse我的应用!

    其它如:

    事件Listen呀:

    public void addActionListener(ActionListener l)

    Observer模式呀!

     

    5、通过接口实现引用传递

      public void SetS1(Interface1 req)

      {

        req.setS("New Value with C1");

      }

      public void SetS2(ClassNoitf req)

      {

        req.setS("New Value with C2");

      }

     

      void jButton3_actionPerformed(ActionEvent e) {

        ClassItf C1 = new ClassItf();

        ClassNoitf C2 = new ClassNoitf();

        //Set C1

        SetS1(C1);

        //Set C2;

        SetS2(C2);

        //Get Value

        System.out.print(C1.getS());

        System.out.print(C2.getS());

      }

     


    最新回复(0)