
接口: 初期理解可以认为是一个特殊的抽象类
当抽象类中的方法都是抽象的,那么该类可以通过接口的形式来表示。
class用于定义类
interface 用于定义接口
接口定义时,格式特点:
1,接口中常量见定义:常量,抽象方法。
2,接口中的成员都有固定修饰符号
常量:public static final
方法:public abstracts
记住: 接口中的成员都是public的
接口:是不可以创建对象的,因为有抽象方法。
需要被子类实现,子类对接口中的抽象方法全都覆盖后,子类才可以实例化。
否则子类是一个抽象类。
接口可以被类多实现。也对多继承不支持的转换形式。java支持多实现。
接口的特点:
接口是对外暴露的规则。
接口是程序的功能扩展。
接口可以用来多实现。
类与接口之间是实现关系,而且类可以继承一个类的同时实现多个接口。
接口与接口之间可以有继承关系。
interface Inter
{
public static final int NUM = 3;
public abstract void show(); }
interface InterA
{
public abstract void method;
} class Demo
{
public void function() {}
}
class Test extends Demo implements Inter,INterA
{
public void show() {}
public void method() {} } interface A
{
void methodA();
} interface B extends A
{
void methodA();
}
interface C extends B
{
void methodA();
}
class D implements C
{
void methodA(){}
void methodB(){}
void methodA(){}
}
class InterfaceDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
/*
class Student
{
abstract void study();
void sleep()
{
System.out.println("sleep") }
//void smoke()
//{
// System.out.println("Smoking");
//}
} interface Smokoing
{
void smoke();
} class ZhangSan extends Sudent implements Smoking
{
void stude() {}
public void smoke() {}
} class Lisi extends Student
{
}
*/ class Sporter
{
void play();
} interface Study
{
} class WangWu extends Sports implements
{
} class
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}