package interfaceDemo;
interface Greepy{//创建一个接口,趴行动物
void run();//不能有方法体,跑的行为
}
interface Aquatic{//水生动物
void swim();//游泳的行为public static
//String name="www";//同等于:public static final String name="www";
}
/*interface Amphibian extends Greepy,Appendable{
//两栖动物,具备上面两种行为,继承接口,只有接口才可以实现多继承,类只能实现单继承。
//接口不能实例化
}*/
class Cat implements Greepy{//必须重父类中的方法,因为是接口是比抽象类更抽象
@Override
public void run() {//必须重写父类中的方法
// TODO 自动生成的方法存根
System.out.println("我会跑!");
}
}
class Fish implements Aquatic{
@Override
public void swim() {
// TODO 自动生成的方法存根
System.out.println("我会游泳!");
}
}
class Frog implements Greepy,Aquatic {//创建一个青蛙类,有两种行为,继承两个接口
@Override
public void swim() {
// TODO 自动生成的方法存根
System.out.println("我会蛙泳。。。。。。");
}
@Override
public void run() {
// TODO 自动生成的方法存根
System.out.println("我会蹦蹦跳跳!");
}
}
public class InterfaceDemo {
public static void main(String[] args) {
Greepy c=new Cat();//实现了多态的作用
c.run();
Aquatic f=new Fish();//实现了多态的作用
f.swim();
Frog frog =new Frog();//两种行为都使用,现在就没有多态,一般很少这么使用。
frog.run();
frog.swim();
Greepy dance=new Frog();//单个实现跳的行为,同时使用了多态
dance.run();
}
}