如何在公共类中以只读方式访问私有抽象的成员?

时间:2011-04-26 08:28:08

标签: c# class abstract

我有一个名为TDSeq的私有抽象类,其中有一些抽象成员和非抽象成员。有两个派生类,它从以下数据获取数据: - 私有类TDSeqBuy:TDSeq和私有类TDSeqSell:TDSeq。

我尝试访问的私有抽象类的成员是private / public bools / double / integers。

数据从派生类流向受保护的抽象名称{get;}的私有抽象类。之后,数据被“移动”到上面提到的私人/公共布尔/双打/整数。

我想从抽象类到公共类访问数据以进行只读,但不知道如何执行此操作。有人可以帮忙吗?

private abstract class TDSeq
{
    public event SetupCompletedEventHandler SetupCompleted;

    protected abstract double TDSTHigh { get; }
    protected abstract double TDSTLow { get; }
    protected abstract double SetupStopLevel { get; }
    public double highesthigh = 0;
    public double lowestlow = 0;
    public double truerange = 0;
    public double setupstoplevel = 0;

    // ...

    case TDSTStateSetup.Completed:
        if( ValidSetup )
        {
            Print = "ValidExtSetup";
            setupCount++;
            SetupDrawText();
            //Print = NameIndex;
        }
        else
        {
            Print = "ExtSetup Finalised";
            tdsetupiscompleted = true;
            if (tdsetupiscompleted)
            {
                Print = "tdsetupiscompleted";
            }
            if (tdsetupdirection == 1) 
            {
                Print = "tdsellsetupiscompleted";
            }
            if (tdsetupdirection == -1) 
            {
                Print = "tdbuysetupiscompleted";
            }
            highesthigh = TDSTHigh;
            lowestlow = TDSTLow;
            truerange = (highesthigh - lowestlow);
            setupstoplevel = SetupStopLevel;
            stateSetup = TDSTStateSetup.Finished;
        }
// ...
}

我正试图公开访问最后5行......

5 个答案:

答案 0 :(得分:3)

使用受保护的,而非私人的。还要考虑组合而不是继承。

嵌套类不是一个好主意。它只限制范围。受保护不会拯救你。

答案 1 :(得分:2)

您也可以使用自动属性来实现相同而不使用私有字段。 e。

private abstract class A
{
    protected int Number { get; private set; }
}

private class B : A
{
    public int GetNumber()
    {
        return Number;
    }
}

答案 2 :(得分:0)

如果您想要访问属性并且它们只是只读,请将值存储在私有字段中 - 并提供受保护的get属性以提供对私有字段的只读访问权限,如下所示:

    private abstract class A
    {
        private int _number = 5;

        protected int Number { get { return _number; } }
    }

    private class B : A
    {
        public int GetNumber()
        {
            return Number;
        }
    }

    private class C : A
    {
        public int GetNumber()
        {
            return Number;
        }
    }

答案 3 :(得分:0)

如果要通过单独的公共类X的方法中的抽象类A的对象访问数据,则抽象类必须对X可见,因此它必须是公共的(或者至少是内部的,当A和X是同一组件的一部分时):

public class Program
{
    static void Main(string[] args)
    {

        B b = new B();
        X.Test(b);
    }

    // private does not work here if you want to have a parameter of type A in X
    public abstract class A
    {
        private int _number = 5;
        public int Number { get { return _number; } }
    }

    private class B : A
    {
    }
}

public class X
{
    public static void Test(Program.A a)
    {
        Console.WriteLine(a.Number);
    }
}

答案 4 :(得分:0)

程序集中的顶级类在可访问性方面只能是公共的或内部的,因此我假设您的私有抽象类,并且它的派生类都嵌套在某些公共类中,对于初学者而言。正确的吗?

如果是这样,只需通过首先通过公共属性实例化该父类中的私有派生类来访问非抽象和公共的嵌套私有抽象类的成员,然后只需从中调用公共字段:

public class TopClass
{

  DerivedClass MyDerivedClass;

  public int GetDerivedClassPublicField
  {
    get
      {
        DerivedClass MyDerivedClass = new DerivedClass();
        return DerivedClass.myfield;//here is access to your abstract class field from outside
      }
   }

  // Private classes must be nested
  private abstract class AbstractClass
  {
    public int myfield = 1;
  }

  private class DerivedClass : AbstractClass
  {
    ... (derived classes inherit the non-abstract field from the abstract parent by default here) ...
  }
}


// now call the public top level class property to get the field in the abstract class
TopClass MyTopClass = new TopClass();
int myInt = MyTopClass.GetDerivedClassPublicField;