抽象类名以及接口作为形式参数
抽象类名作为形式参数,但是接收的是具体的对象
package com.imooc; abstract class Person{ public abstract void study(); } class PersonDemo{ public void method(Person p){ p.study(); } } class Student extends Person{ public void study(){ System.out.println("学生好好学习"); } } public class InterfaceDemo2 { public static void main(String[] args){ Person p = new Student(); PersonDemo pd = new PersonDemo(); pd.method(p); } }
接口名作为形式参数,(注意接收的仍然是具体的对象)
package com.imooc; abstract class Person{ public abstract void study(); } class PersonDemo{ public void method(Person p){ p.study(); } } class Student extends Person{ public void study(){ System.out.println("学生好好学习"); } } public class InterfaceDemo2 { public static void main(String[] args){ Person p = new Student(); PersonDemo pd = new PersonDemo(); pd.method(p); } }