方法的形式参数如果是抽象类,调用方法实际参数需要抽象类的子类对象
abstractclassPerson{publicabstractvoidwork();}classPersonDemo{publicvoidmethod(Person p){//需要的是抽象类的子类对象
p.work();}}//需要提供子类classWorkerextendsPerson{publicvoidwork(){System.out.println("工人都需要工作...");}}//测试类中:调用PersonDemo类中的method方法//创建PersonDemo对象PersonDemo pd =newPersonDemo();//抽象类多态:Person p =newWorker();
pd.method(p);
3.方法的形式参数如果是接口,调用方法实际参数应该传递什么?
方法形式参数如果是接口,调用方法,实际参数需要传递该接口的子实现类对象
interfaceMary{voidmary();}classMaryDemo{publicvoidshow(Mary mary){//需要接口的子实现类对象
mary.mary();}}//提供接口的实现类--具体类classMaryImplimplementsMary{publicvoidmary(){System.out.println("结婚了...");}}//测试类中测试:调用MaryDemo类中show方法MaryDemo md =newMaryDemo();//接口多态Mary mary =newMaryImpl();
md.show(mary );
4,方法的返回值如果是接口,该方法结束需要返回什么?(具体代码实现)
方法的返回值如果是接口类型,该方法结束需要返回该接口的子实现类对象
interfaceMary{voidmary();}classMaryDemo{publicMaryfunction(){// 返回的Mary的子实现类对象Mary mary =newMaryImpl();return mary ;}}//提供接口的实现类--具体类classMaryImplimplementsMary{publicvoidmary(){System.out.println("结婚了...");}}
4,方法的返回值如果是具体类,该方法结束需要返回什么?
方法的返回值是具体类,需要返回的是该具体类的对象
classStudent{publicvoidplayGame(){System.out.println("玩游戏...");}}classStudentDemo{publicStudentmethod(){//需要Student类的对象Student s =newStudent();return s ;}}//测试类//调用StudentDemo类中的method方法StudentDemo sd =newStudentDemo();Student student = sd.method();//本质就是创建了学生对象
student.playGame();//(sd.method()).playGame() ; 这样可以的,不建议这样写
5,方法的返回值如果是抽象类,该方法结束需要返回什么?(具体代码实现)
方法的返回值如果是抽象类类型,该方法结束需要返回该抽象类的具体子类对象
abstractclassMary{publicabstractvoidmary();}classMaryDemo{publicMaryfunction(){// 返回的Mary的子类对象Mary mary =newMaryTest();return mary ;}}//提供抽象类的子类--具体类classMaryTestextendsMary{publicvoidmary(){System.out.println("结婚了...");}}