接口基本格式
【修饰符】interface 接口名 【】{
【public】【static】【final】常量;
【public】【abstract】方法;
}
package com.hg.test.interface01;
//定义接口Interface1
interface Interface1 {
public void display();//定义在接口中的方法,没有实现。和抽象方法差不多。
}
class a implements Interface1 { //在类中用implements实现接口。
public void display() {
System.out.println("display a");
}
}
class b implements Interface1 {
public void display() {
System.out.println("display b");
}
}
public class Test {
public static void main(String[] args) {
Interface1 i;
i=new a();//通过将类a和类b的实例赋给接口引用,从而实现方法运行的动态绑定
i.display();
i=new b();
i.display();
a A=new a();
A.display();
b B=new b();
B.display();
}
}
##在Java中类的多继承是不允许的,但接口允许多继承。
package com.hg.test.interface02;
interface Attack {
public void attack();
}
interface Evolve {
public void evolve();
}
class zombie implements Attack {
public void attack() {
System.out.println("zombie bite plant");
}
}
class plant implements Attack,Evolve { //plant继承了Attack和Evolve两个接口
public void attack() {
System.out.println("plant attack zombie ");
}
public void evolve() {
System.out.println("plant evolve ");
}
}
public class Test {
public static void main(String[] args) {
zombie z=new zombie();
z.attack();
plant p=new plant();
p.attack();
p.evolve();
Attack a;
a=new zombie();
a.attack();
a=new plant();
a.attack();
Evolve e;
e=new plant();
e.evolve();
}
}



