1

我正在使用一个公共实体框架, ParentClassfunction通过搜索配置和数据库条目来做很多重要的事情,并且无法编辑此代码,因为它来自第三方。但是,我想functionParentClass无法访问的白名单信息上添加额外的“搜索”。

下面的代码是正在发生的事情的模型。我只是想知道,有没有更好的方法来扩展ParentClass和访问它function

public ParentClass : ClassInterface
{
    foo bar;
    public Parent(foo _bar) 
    {
        this.bar = _bar
    }

    function(int) 
    {
        // do things with this.bar
    }
}

public ChildClass : ParentClass, ClassInterface 
{
    public ChildClass(foo _bar) : base(_bar) { }


    public virtual function(int) 
    {
        bool x = base.function(int)
        if (!x) 
        {
            ... stuff
        }

        ... otherstuff
    }
}
4

1 回答 1

1

调用base.function()是正确的做法。所以你的代码正在做正确的事情。话虽如此,当你认为你需要这样的东西时,你应该考虑一种更加基于组合的方法。

于 2015-09-17T19:06:07.660 回答