接口:初期理解,能够觉得是一个特殊的抽象类
当抽象类中的方法都是抽象的,那么该类能够通过接口的形式来表示。
class用于定义类
interface 用于定义接口.
接口定义是,格式特点:
1:接口中常见定义:常量、抽象方法。
2:接口中的成员都有固定修饰符。
常量:public static final
方法:public abstract
记住:
接口中的成员都是public
接口是不能够创基对象的,由于有抽象方法。
须要被子类实现。子类对接口中的抽象方法全都覆盖后。子类才干够实例化。否则子类是一个抽象类。
接口能够被类多实现。也是对多继承不支持的转换形式。
子类在继承的同一时候也能够多实现;
interface Inter
{
public static final int NUM=3;
public abstract void show();
}
interface Inter1
{
public abstract void method();
}
class demo
{
public void function()
{
System.out.println("lxlxl");
}
}
class Test extends demo implements Inter,Inter1
{
public void show(){}
public void method(){}
}
class InterfaceDemo
{
public static void main(String[] args)
{
Test t=new Test();
t.function();
System.out.println(t.NUM);
System.out.println(Test.NUM);
System.out.println(Inter.NUM);
}
}