接口的使用场景

2023-01-14  本文已影响0人  山猪打不过家猪

1.

class Company
    public Company(IKanMen IKanMen)
    {
        this.ObjKanMen = IKanMen;
    }
    public IKanMen ObjKanMen{get;set;}
Company  company = new Company(new Dog());
company.ObjKanMen.KanMen();
Company  company = new Company(new BaoAn());
company.ObjKanMen.KanMen();

不使用接口的话,每次有新的具有看门功能类型添加时候,都要添加新的构造函数

class Company
    public Company(BaoAn BaoAn)
    {
        this.BaoAan = BaoAn;
    }
    public BaoAn BaoAn{get;set;}

    public Company(Dog dog)
    {
        this.Dog = dog;
    }
    public Dog Dog{get;set;}

2.

上一篇下一篇

猜你喜欢

热点阅读