package com.dyz.test; interface HelloWorld{ void sayWhat(); }package com.dyz.test; interface HiWorld{ void sayHi(); }package com.dyz.test; public abstract class AbstractSuper{ protected abstract void sayGood(); }package com.dyz.test; public class Child extends AbstractSuper implements HelloWorld,HiWorld{ public void sayWhat(){ System.out.println("good good study,day day up"); } public void sayHi(){ System.out.println("hi"); } public void sayGood(){ System.out.println("good"); } }package com.dyz.test; public class ChildTest{ public static void main(String[] args){ HelloWorld hello = new Child(); hello.sayWhat(); HiWorld hi = new Child(); hi.sayHi(); AbstractSuper as = new Child(); as.sayGood(); } }package com.dyz.test; public class ChildTest{ public static void main(String[] args){ HelloWorld hello = new Child(); hello.sayWhat(); HiWorld hi = new Child(); hi.sayHi(); AbstractSuper as = new Child(); as.sayGood(); ((Child) hello).sayHi(); } }package com.dyz.test; public class ChildTest{ public static void main(String[] args){ HelloWorld hello = new Child(); hello.sayWhat(); HiWorld hi = new Child(); hi.sayHi(); AbstractSuper as = new Child(); as.sayGood(); if(hello instanceof Child){ ((Child) hello).sayHi(); ((Child) hello).sayGood(); } } }为什么Java会具有多态这个特性?答案已经呼之欲出了,继承、重载和重写、以及我们允许父类(接口类)的引用指向子类(实现类)对象。
package com.dyz.test; public class SuperClass{ public void print(){ System.out.println("父类的print方法"); } }package com.dyz.test; public class SonClass extends SuperClass{ public void print(){ System.out.println("子类的print方法"); } public static void main(String[] args){ SuperClass sc = new SonClass(); sc.print(); } } 请注意,我们在做强制类型转换时,一定要使用instanceof来判断这个转换是不是能成功,能成功才能做强制类型转换,否则程序运行时会抛出异常!
package com.dyz.test; public class Animal{ public void eat(){ System.out.println("进食"); } }package com.dyz.test; public class Cat extends Animal{ public void eat(){ System.out.println("我吃鱼"); } }package com.dyz.test; public class Cow extends Animal{ public void eat(){ System.out.println("我吃草"); } }package com.dyz.test; public class AnimalUse{ public void eat(Animal x){ x.eat(); } public static void main(String[] args){ Animal a = new Cat(); Animal b = new Cow(); Animal c = new Animal(); a.eat(); b.eat(); c.eat(); AnimalUse user = new AnimalUse(); user.eat(a); user.eat(b); user.eat(c); } }package com.dyz.test; public interface Animal{ public void eat(); }package com.dyz.test; public class Cat implements Animal{ public void eat(){ System.out.println("我吃鱼"); } }package com.dyz.test; public class Cow implements Animal{ public void eat(){ System.out.println("我吃草"); } }package com.dyz.test; public class AnimalUse{ public void eat(Animal x){ x.eat(); } public static void main(String[] args){ Animal a = new Cat(); Animal b = new Cow(); a.eat(); b.eat(); AnimalUse user = new AnimalUse(); user.eat(a); user.eat(b); } }class A{ public String show(D obj){ return ("A and D"); } public String show(A obj){ return("A and A"); } }class B extends A{ public String show(B obj){ return ("B and B"); } public String show(A obj){ return ("B and A"); } }class C extends B{ }class D extends B{ }public class E{ public static void main(String[] args){ A a1 = new A(); A a2 = new B(); B b = new B(); C c = new C(); D d = new D(); System.out.println("1.---"+a1.show(b)); System.out.println("2.---"+a1.show(c)); System.out.println("3.---"+a1.show(d)); System.out.println("4.---"+a2.show(b)); System.out.println("5.---"+a2.show(c)); System.out.println("6.---"+a2.show(d)); System.out.println("7.---"+b.show(b)); System.out.println("8.---"+b.show(c)); System.out.println("9.---"+b.show(d)); } }封装:将对象的信息隐藏起来,只向外部提供必要的接口来提供服务。它让我们可以在类的内部修改代码,而不必改外部调用方的代码。
文章来源: 初学Java-15-多态