The grammar rules define JAVA interface

Interface ( role of interface is very rich, and design patterns often combined together ):

  • Concept: the interface can be considered a "complete" abstract class, the interface is a specification extracted from a plurality of similar class, the interface is embodied specification (communication specification), such as various types of the motherboard " slot ", regardless of which vendor, regardless of which type of motherboard, they can perform data communication with the CPU, graphics card, memory - as a set of international public " standard "(specifications), is reflected by the interface.
  • Defined syntax:
    【修饰符】 interface 接口名
    {
        //0 ~ N个Field定义
        //0 ~ N个抽象方法
        //0 ~ N个内部类、内部接口、内部枚举定义
    }

    note:

  1. The interface can not have a constructor, initialization block;

  2. Modifiers can be: public or omitted; can not be modified to use static, not (does not allow subclasses) modified with a final, not abstract;

  3. Interface name: put together from more than one word, the first letter of each word capitalized, recommended the interface with the "adjective" in some places (c #), the recommended interface for I the beginning, and later some Java programmers in this way, [ Interface in the composition, are public, because the public interface to reflect the standards];

  4. Interface in the Field, by default there are three modifiers: public, static, final, whether written or not written, anyway, the interface has [in the Field, you must specify an initial value statement, because the final class variables can only be modified in a statement when the static initialization specified initial value, and since the interface does not contain the initialization block, the initial value can be specified only when the declaration];

  5. Interface methods in the default there are two modifiers: public, abstract, whether written or not written, anyway, there [in the interface method can not be static, because the interface where the default methods have abstract modification];

  6. Interface in the internal class, the internal interface, internal enumeration, there are two default modifiers, public, static, whether written or not written, anyway there;

  7. abstract and static methods can not be modified, abstract and private at the same time not allow modification methods, abstract can not occur simultaneously with the final, the two always mutually exclusive;

  8. N interface can have a direct parent interface. With After the interface, the interface can be used to define a variable, can not create an instance of the interface, the interface is the biggest use for other classes to achieve their own;

  9. implements a realization or N interfaces, when a class implements an interface, implementation class or provide an implementation for all abstract methods abstract parent class, subclass or you can only be an abstract class;

  10. Interface is mainly in the "programming to an interface" time to provide a more flexible mechanism
interface A
{
}
interface B
{
}
interface C
{
}
//一个接口,可以拥有N个直接的父接口
public interface D extends A,B,C
{
}
interface Eatable
{
    void eat();
}
interface Flyable
{
    void fly();
}

public interface Walkable extends Eatable
{
    int AGE = 20;
    //对于执行“宏替换”,变量名应该是多个单词连缀而成,并且所有字母全部大写,单词与单词之间以下划线隔开
    final String MY_NAME = "张三";
    //接口里的成分只能用public修饰,可省略
    double weight = 34.4;

    //方法前面默认有public abstract修饰。
    void info();
    //方法前面默认有abstract修饰,所以方法后面坚决不能有花括号
   // void test(){}             //错误
}

//一个类可以同时“实现”N个接口
public class Sparrow implements Flyable, Walkable
{
    @Override
    public void eat()
    {
        System.out.println("麻雀一口一口地吃麦子");
    }
    public void fly()
    {
        System.out.println("麻雀在天空飞翔");
    }
    public void info()
    {
        System.out.println("我是一只小麻雀");
    }

    public static void main(String[] args)
    {
    	//向上转型
        Walkable w = new Sparrow();
        //w引用变量,在编译时只是Walkable类型
        //因此,编译器只能允许调用Walkable里的方法
        w.eat();          //麻雀一口一口地吃麦子
        w.info();         //我是一只小麻雀
        
        //强转,向下转型
        Sparrow sp = (Sparrow) w;
        sp.eat();         //麻雀一口一口地吃麦子
        sp.fly();         //麻雀在天空飞翔
        sp.info();        //我是一只小麻雀
        
        Sparrow fy = new Sparrow();
        fy.fly();         //麻雀在天空飞翔
    }
}
  • Description:
  1. Similarities between interfaces and abstract classes: it can contain abstract methods; can not create an instance; subclass inherits an abstract class that implements the interface, are required to implement all of the abstract methods, or subclass can only be an abstract class
  2. Interface and the difference between abstract classes at: the interface, there are only abstract methods, but an abstract class without abstract methods can only contain ordinary methods; can not define static methods in an interface, but the abstract class can contain static methods; interfaces in the Field there is always public, static, final, but the abstract class can Field Field is the most common; interfaces can not contain constructors, but can have an abstract class constructor; interfaces can not contain initialization block, but can abstract class initialization block; interface can have a plurality of direct interface to the parent, but the abstract class only one direct parent.

Published 111 original articles · won praise 57 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_38358499/article/details/100600212