[public] interface interfacename [extends SuperInterfaceList]{//可继承多个接口
[public static final] int i = 20;
[public abstract] void f();
...... //常量定义和方法定义
}
类可以实现接口,并可重写方法,重写必须加public
interface It{
int i = 20;
void f();
}
abstract class A implements It{ //把接口中的成员包含到类A中
public void f(){//重写必须加public
System.out.printf("i = %d\n",i);
}
}
继承放前面,实现接口放后面,例如:
class T extends A implements It4,It3{
}
class A{
int i;
public void show(){
System.out.printf("show-> %d\n",i); //i是属性i,此时的i等价于this.i
}
public void f(){
int i; //这里的i和属性i没有冲突
System.out.printf("f-> %d\n",i); //error 因为i是局部变量,使用时必须初始化
}
public void g(int i){ //i是形参i,形参i也是局部变量
this .i = i;
System.out.printf("g-> %d\n",i);
}
}
interface It{
void f();
}
class A implements It{
public void f(){
System.out.printf("AAAA\n");
}
}
class D{
public static void main(String[] args) {
It it;
it = new A();
it.f();//输出:AAAA
it.g();//error
}
}
new A()
),把对象的地址发送给接口的引用,通过接口的引用可以调用这个接口指向的那个真正的对象(A
)中的那些成员。A
)从父类( It
)继承过来的成员( f()
),不能调用子类所特有的( g()
)。java.lang.Colneable
接口,但该接口却是空的,没有任何内容,目的只是为了起个标志作用。原文:https://www.cnblogs.com/aabyss/p/12317460.html