如何将const参数传递给C#中的抽象类父类?

ami*_*s80 1 .net c# parameters class abstract

我写了以下多态代码:

public abstract class  A  
{  
    readonly int x;  
    A(int i_MyInt)  
    {  
        x = i_MyInt;  
    }  
}  

public abstract class B : A  
{  
    //holds few integers and some methods      
}  


// concrete  object class  
public class C : B   
{  
   // holds some variables and mathods  
    C(int MyInt)  
    {   
      // here i would like to initialize A's x  
    }  
}  
Run Code Online (Sandbox Code Playgroud)

我如何从C初始化A的x我尝试将参数传递给A的C'tor - 但是没有工作..

请帮忙,在此先感谢Amitos80

Mar*_*ers 5

您需要向B添加一个构造函数,它接受一个整数并将其传递给A的构造函数.然后,您可以从C调用此构造函数.

public abstract class B : A
{  
    public B(int myInt) : base(myInt)
    {
        // other initialization here...
    }  
}  

public class C : B
{
    // holds some variables and mathods  
    public C(int myInt) : base(myInt)
    {
        // other initialization here...
    }
}  
Run Code Online (Sandbox Code Playgroud)

A的构造函数也必须不是私有的.