简体   繁体   English

获取从父类到List.Count的引用在子类中

[英]Get reference from parent class to List.Count in child class

I have two classes A and B, B inheriting from A and additionally having a member of type List<OtherObject> . 我有两个类A和B,B,它们继承自A,并且具有类型List<OtherObject>的成员。

Now I would need a reference to the List.Count property in class A reflecting changes that happen to the List in class B (removing/adding items). 现在,我需要引用class AList.Count属性, class A反映class B的List发生的更改(删除/添加项)。 What's the best way to accomplish that, without changing the int member in class A by myself? 在不自己更改class A int成员的情况下,实现此目标的最佳方法是什么?

EDIT: OK, it is a little bit more complicate and I have some simplified code here for explanation: The int member myCount should hold the reference I was talking about. 编辑:好的,这有点复杂,在这里我有一些简化的代码来解释:int成员myCount应该保存我正在谈论的引用。

public abstract class A{
protected int myCount;

    public void Process()
    {

        ProcessSpecificRequest();

        if(myCount == 0){
            //Do something
        }
    }

    protected abstract void ProcessSpecificRequest();

    }

public class B: A {
private List<Object> myList;

    protected override void ProcessSpecificRequest()
    {
    //Do something with the List

    }

} }

class A {
    public abstract int ListCount { get; }
}

class B : A {
    protected List<object> BList = new List<object>();
    public override int ListCount {
        get {
            return BList.Count;
        }
    }
}
public abstract class A, ICollection
{
    public abstract int Count { get; }
    //todo realize ICollection
}

public class B<T> : A
{
    protected List<T> OtherObject = new List<T>();
    public override int Count 
    {
        get
        {
            return OtherObject.Count;
        }
    }
    //todo realize ICollection
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM