JavaSE-12.1.3【接口名作为形参和返回值】
package day3.yub4by01.p3; /* 1.3 接口名作为形参和返回值 方法的形参是接口名,其实需要的是该接口的实现类对象 方法的返回值是接口名,其实返回的是该接口的实现类对象 */ public class JumppingDemo { public static void main(String[] args) { JumppingOperator jo = new JumppingOperator(); Jumpping j = new Cat(); jo.useJumpping(j); Jumpping j2 = jo.getJumpping(); //new Cat(); j2.jump(); } }
package day3.yub4by01.p3; public class JumppingOperator { public void useJumpping(Jumpping j){ //多态Jumpping j = new Cat(); j.jump(); } public Jumpping getJumpping(){ Jumpping j = new Cat(); return j; } }
package day3.yub4by01.p3; public interface Jumpping { void jump(); }
package day3.yub4by01.p3; public class Cat implements Jumpping{ @Override public void jump() { System.out.println("猫猫会跳高吗"); } }