zoukankan      html  css  js  c++  java
  • 对Java单继承的弥补——接口

    接口主要用来实现多重继承,它是常量和方法的集合,这些方法只有声明没有实现,即接口本身是抽象的,系统默认abstract修饰。

    1.接口的定义:

    1 public interface A{
    2     int A=1;//默认为public static final常量
    3     void displayA();//默认为public abstract抽象方法
    4 }

    带默认方法的接口定义:

    1 interface B
    2 {
    3     int B=2;//常量
    4     public void print();//抽象方法
    5     default public void otherprint()//带方法体的默认方法
    6     {
    7         System.out.println(B);
    8     }
    9 }

    default方法称为虚拟扩展方法,是指:在接口内部包含一些默认的方法实现,使得接口在进行扩展的时候,不会破坏与接口相关的实现类代码。

    2.接口可以实现多重继承:

     1 interface A{
     2     int A=1;
     3     void displayA();
     4 }
     5 interface B{
     6     int B=2;
     7     void displayB();
     8 }
     9 interface C extends A,B{
    10     int C=3;
    11     void displayC();
    12 }
    13 class ABC implements C
    14 {
    15     public void displayA() 
    16     {
    17         System.out.println(A);
    18     }
    19     public void displayB() 
    20     {
    21         System.out.println(B);
    22     }
    23     public void displayC()
    24     {
    25         System.out.println(C);
    26     }
    27 }
    28 public class T04
    29 {
    30     public static void main(String[] args)
    31     {
    32         ABC a = new ABC();
    33         ABC b = new ABC();
    34         ABC c = new ABC();
    35         a.displayA();
    36         b.displayB();
    37         c.displayC();
    38     }
    39 }

    上面的例子中,接口C多继承了接口AB,类ABC通过实现接口C,同时实现了接口AB 中的方法,这就是多继承的一种体现。

    一个类也可以同时实现几个不同的接口:

    class ABC implements A,B,C{}//这行代码和例子中的红色字体效果是一样的

    3.接口和抽象类的区别:

      接口中只有抽象方法,没有普通方法;接口无构造方法。

      接口不能被继承,只能被实现。

      上述:public class ABC implements C{}就是类实现接口的方法。

    4.接口中定义的常量可以直接用类名访问。

    System.out.println(ABC.A);

    5.GUI程序事件处理——接口使用一例。

     1 import java.awt.FlowLayout;
     2 import java.awt.event.*;
     3 import javax.swing.*;
     4 
     5 public class EventDemo extends JFrame implements ActionListener
     6 {    
     7     JButton button1 = new JButton("登录");
     8     JButton button2 = new JButton("取消");
     9     JTextField jtf = new JTextField(20);
    10     public EventDemo()
    11     {
    12         add(jtf);
    13         add(button1);
    14         add(button2);
    15         button1.addActionListener(this);//为按钮注册事件监听器
    16         button2.addActionListener(this);
    17         setSize(600,350);
    18         setVisible(true);
    19         setLayout(new FlowLayout());
    20         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    21     }
    22     public void actionPerformed(ActionEvent e)//实现接口中的抽象方法
    23     {
    24         if((JButton)e.getSource()==button1)
    25             jtf.setText("***");
    26         else
    27             jtf.setText("&&&");
    28     }
    29     public static void main(String[] args)
    30     {
    31        EventDemo frame = new EventDemo();
    32     }
    33 }
    看似简单的知识背后,承载的是收获和成长!
  • 相关阅读:
    javascript模拟枚举
    javascript实现命名空间效果
    js数组去重
    文件上传插件
    script标签的defer属性
    js数组复制
    更改visual studio2010的主题
    关于json格式在.net前后台传值的详细示例
    where T : class泛型类型约束
    JavaScript模仿鼠标拖动选择功能
  • 原文地址:https://www.cnblogs.com/wxywxy/p/6724590.html
Copyright © 2011-2022 走看看