栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

接口interface

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

接口interface

接口interface

接口基本格式
【修饰符】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();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835840.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号