一接口实现的多态
在上一篇博文:JavaSE入门学习20:Java面向对象之接口(interface)(一)中提到了接口的实现存在多态性,那么
这一篇主要就要分析接口实现的多态。
实例一
Test.java源文件代码:
public class Test{ public static void main(String[] args){ //实现接口Singer Singer s1 = new Student("Amy"); s1.sing(); s1.sleep(); s1.study();//编译出错 //实现接口Singer Singer s2 = new Teacher("Jack"); s2.sing(); s2.sleep(); s2.paint();//编译出错 s2.eat();//编译出错 s2.teach();//编译出错 //实现接口Painter Painter p1 = (Painter)s2; p1.paint(); p1.eat(); p1.teach();//编译出错 p1.sing();//编译出错 p1.sleep();//编译出错 } } //接口Singer interface Singer{ //抽象方法 public void sing(); public void sleep(); } //接口Painter interface Painter { //抽象方法 public void paint(); public void eat(); } //学生类Student。继承一个Singer接口 class Student implements Singer{ //私有成员变量 private String name; //构造方法 Student(String name){ this.name = name; } public String getName(){ return name; } //学生类独有的study()方法 public void study(){ System.out.println("student is studying"); } //重写接口Singer中的sing()方法 public void sing(){ System.out.println("student is singing"); } //重写接口Singer中的sleep()方法 public void sleep(){ System.out.println("student is sleeping"); } } //教师类Teacher,继承两个接口Singer和接口Painter class Teacher implements Singer,Painter{ //私有成员变量 private String name; //构造函数 Teacher(String name){ this.name = name; } public String getName(){ return name; } //Teacher类独有的teach()方法 public void teach(){ System.out.println("teacher is teaching"); } //重写接口Singer的sing()方法 public void sing(){ System.out.println("teacher is singing"); } //重写接口Singer的sleep()方法 public void sleep(){ System.out.println("teacher is sleeping"); } //重写接口Painter的paint()方法 public void paint(){ System.out.println("teacher is painting"); } //重写接口Painter的eat()方法 public void eat(){ System.out.println("teacher is eating"); } }
编译过程报错:
从上面报错的信息能够看出:在前面的叙述面向对象多态的时候就说过,引用多态和方法多态,以及多态中的引
用类型转换,这些相同在接口中都能实现,可是有差别的是,接口不能实例化。可是能够接口的引用能够指向继承它
的子类的对象,当然调用的方法也是子类的重写接口中的抽象方法的方法。详细的对象的多态性能够參考:JavaSE
上面的编译过程报错的行数是7、13、14、15、21、22、23。这些报错的类型都是找不到符号。这是由于引用类
型不一样的原因,比方Singer接口的引用指向子类Student的对象s1。这个对象引用仅仅能包括子类Student中重写接口
Singer的两个抽象方法,不能包括其他重写Painter接口中的抽象方法以及子类Student本身独有的方法。其他的类
似。为了避免这样的编译出错,我们能够创建子类Student的对象和子类Teacher的对象。
实例二
改写Test类中的代码:
public class Test{ public static void main(String[] args){ //创建Student类的对象,实现接口Singer Student s1 = new Student("Amy"); s1.sing(); s1.sleep(); s1.study(); //创建Teacher类的对象,实现接口Singer和接口Painter Teacher t1 = new Teacher("Jack"); t1.sing(); t1.sleep(); t1.paint(); t1.eat(); t1.teach(); } }
编译执行结果:
二接口的使用方法(实际參考)
(1)精简程序结构,免除反复定义
比方,有两个及上的的类拥有同样的方法,可是实现功能不一样,就能够定义一个接口。将这种方法提炼出来,
在须要使用该方法的类中去实现,就免除了多个类定义系统方法的麻烦。
举例:鸟类和昆虫类都具有飞行的功能,这个功能是同样的,可是其他功能是不同的,在程序实现的过程中,就
能够定义一个接口。专门描写叙述飞行。
下图是分别定义鸟类和昆虫类,其都有飞行的方法,类图为:
下图定义了接口。其类图例如以下:
详细的代码实现:
//接口FlyAnimal interface FlyAnimal{ //抽象方法fly() void fly(); } //昆虫类 class Insect{ int legnum = 6; void oviposition(){}; } //鸟类 class Bird { int legnum = 2; void egg(){}; } //蚂蚁类 class Ant extends Insect implements FlyAnimal{ public void fly(){ System.out.println("Ant can fly"); } public void oviposition(){ System.out.println("Ant can spawn"); } } //鸽子类 class Pigeon extends Bird implements FlyAnimal{ public void fly(){ System.out.println("pigeon can fly"); } public void egg(){ System.out.println("pigeon can lay eggs "); } } public class Test{ public static void main(String args[]){ Ant a=new Ant(); a.fly(); a.oviposition(); System.out.println("Ant's legs are "+ a.legnum); System.out.println("\n"); Pigeon p= new Pigeon(); p.fly(); p.egg(); System.out.println("Pigeon's legs are "+ p.legnum); } }
编译执行结果:
(2)拓展程序功能,应对需求变化
如果一个学校接待方面的程序。招待不同身份的人的食宿问题。其相应规则例如以下:
理论上。当然能够对每一个不同身份的人各定义一个相应的类。并实现各自的方法,可是观察这写类。能够归纳出
其有一个共同的模板。即“人”的“食、宿”问题。这时候,就能够发挥接口的功能了。
详细实现代码例如以下:
interface Person{ void eat(); void sleep(); } class Student implements Person{ public void eat(){ System.out.println("学生去食堂吃饭!"); } public void sleep(){ System.out.println("学生回寝室睡觉!"); } } class Teacher implements Person{ public void eat(){ System.out.println("教师去教工餐厅吃饭。"); } public void sleep(){ System.out.println("教师回学校公寓睡觉!"); } } class Parents implements Person{ public void eat(){ System.out.println("家长去招待所饭馆吃饭!"); } public void sleep(){ System.out.println("家长回招待所睡觉!
"); } } public class Test{ public static void main(String[] args){ Person p1 = new Student(); p.eat(); p.sleep(); Person p2 = new Teacher(); p.eat(); p.sleep(); Person p3 = new Parents(); p.eat(); p.sleep(); } }
执行结果:
如今须要加入一些功能,即如今须要加入“外宾、上级领导”两类角色,而且以后工具须要还要加入对应的身份角
色的人进来,此时,仅仅须要依据须要加入“外宾”类、“领导”类。而主类仍然能够拿来就用。无需进行很多其它的改动。
此
时就能够显示出接口的作用了。
我们列表如今更新为:
在上面的程序中加入例如以下两个类就可以:
class Foreign implements Person{ public void eat(){ System.out.println("外宾去酒店吃饭!"); } public void sleep(){ System.out.println("外宾回酒店睡觉!"); } } class Leader implements Person{ public void eat(){ System.out.println("领导去宾馆吃饭!"); } public void sleep(){ System.out.println("领导回宾馆睡觉!"); } }
我们在主方法中加入的代码为:
Person p4 = new Foreign(); p4.eat(); p4.sleep(); Person p5 = new Leader(); p5.eat(); p5.sleep();
执行结果:
总结