最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

JAVA使用接口实现类的功能

IT培训 admin 2浏览 0评论

JAVA使用接口实现类的功能

interface Eatable
{public abstract void eat();
}class chinese implements Eatable
{public void eat(){System.out.println("中国人吃");}
}class American implements Eatable
{public void eat(){System.out.println("美国人吃");}
}class Indian implements Eatable
{public void eat(){System.out.println("印度人吃");}
}public class test
{public static void main(String[] args){Eatable[] e = new Eatable[10];for (int i = 0; i < e.length; i++) {if(i % 2 == 0){e[i] = new chinese();}else if(i % 3 == 0){e[i] = new American();}else{e[i] = new Indian();}}for (int i = 0; i < e.length; i++) {e[i].eat();}}
}

JAVA使用接口实现类的功能

interface Eatable
{public abstract void eat();
}class chinese implements Eatable
{public void eat(){System.out.println("中国人吃");}
}class American implements Eatable
{public void eat(){System.out.println("美国人吃");}
}class Indian implements Eatable
{public void eat(){System.out.println("印度人吃");}
}public class test
{public static void main(String[] args){Eatable[] e = new Eatable[10];for (int i = 0; i < e.length; i++) {if(i % 2 == 0){e[i] = new chinese();}else if(i % 3 == 0){e[i] = new American();}else{e[i] = new Indian();}}for (int i = 0; i < e.length; i++) {e[i].eat();}}
}
11136

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论