如何在C#中将常量参数传递给抽象类父级?

如何在C#中将常量参数传递给抽象类父级?,c#,.net,class,parameters,abstract,C#,.net,Class,Parameters,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 publ

我编写了以下多态代码:

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  
    }  
}  
如何从C初始化A的x 我尝试将参数传递给A的C'tor,但没有成功

请帮忙, 提前谢谢
Amitos80

您需要向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...
    }
}  

A的构造函数也不能是私有的。

+1您无论如何都需要它,因为A类没有默认构造函数。或者,您也可以将A的x设置为受保护的或公共的,然后从C的构造函数设置它。非常感谢,这很有帮助。