匿名内部类在开发中当做参数传递

xiaoxiao2021-02-27  88

匿名内部类在开发中当做参数传递

class Test { public static void main(String[] args) { //如何调用PersonDemo中的method方法呢? PersonDemo pd = new PersonDemo (); //pd.method(new Student()); pd.method(new Person() {//匿名内部类在开发中当做参数传递 public void show() { System.out.println("show"); } }); } } //这里写抽象类,接口都行 abstract class Person { public abstract void show(); } class PersonDemo { //public void method(Person p) { /* Person p = new Person(){ public void show() { System.out.println("show"); } }; */ public void method(Person p) {//Person p = new Student(); //父类引用指向子类对象 p.show(); } } class Student extends Person { public void show() { System.out.println("students show"); } }

转载请注明原文地址: https://www.6miu.com/read-6048.html

最新回复(0)