匿名对象

package projict05;

public class test12 {
    public static void main(String[] args) {
        PhoneFactory pf=new PhoneFactory();
        //匿名对象的创建和调用
        pf.usePhone(new Phone());
    }

}


class PhoneFactory{
    public void usePhone(Phone phone) {  //对象当作参数
        phone.call(); 
        phone.send();
    }
}

class Phone{
    String name="zhang";
    int price;
    public void call() {
        System.out.println("calling");
    }
    
    public void send() {
        System.out.println(name+price);
    }
}

 

posted on 2020-05-20 11:04  happygril3  阅读(70)  评论(0)    收藏  举报

导航