Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从基构造函数中获取值的可能性,其变量与派生构造函数中的变量不同?_C#_Constructor - Fatal编程技术网

C# 从基构造函数中获取值的可能性,其变量与派生构造函数中的变量不同?

C# 从基构造函数中获取值的可能性,其变量与派生构造函数中的变量不同?,c#,constructor,C#,Constructor,解释我想要什么有点不同,但我希望我能以你能理解的方式来解释 我有一个a班(主班) B类:A(继承) C类:A A类有我自己编写的构造函数,如下所示: public A(int a, int b, int c, string d) { } public B(int a, int b, int c, string d, int e) { } public B(int a, int b, int c, string d, int e) : base(a, b, c, d) {

解释我想要什么有点不同,但我希望我能以你能理解的方式来解释

我有一个a班(主班)

B类:A(继承)

C类:A

A类有我自己编写的构造函数,如下所示:

public A(int a, int b, int c, string d)
{
}
public B(int a, int b, int c, string d, int e)
{
}
 public B(int a, int b, int c, string d, int e) : base(a, b, c, d)
    {
       /* It's working because I've added the variable "e" only.
       The other variables haven't been changed. */
    }
 public C(int w, int x, string y, string z) : base(a, b, c, d)
    {
         /* Class C does have completely different values than class A.
            But I'd like to have all the variables the way I've written above.
            I'd like to use variable "w" for example. But "a" should be valid 
            to use as well. How is it possible to make the variables
            a, b, c and d valid to class C? */
}
让我们看看B类: B类继承自A类。 在这里,我想写另一个单独的构造函数,它比A中的其他构造函数有更多的可能性

看起来是这样的:

public A(int a, int b, int c, string d)
{
}
public B(int a, int b, int c, string d, int e)
{
}
 public B(int a, int b, int c, string d, int e) : base(a, b, c, d)
    {
       /* It's working because I've added the variable "e" only.
       The other variables haven't been changed. */
    }
 public C(int w, int x, string y, string z) : base(a, b, c, d)
    {
         /* Class C does have completely different values than class A.
            But I'd like to have all the variables the way I've written above.
            I'd like to use variable "w" for example. But "a" should be valid 
            to use as well. How is it possible to make the variables
            a, b, c and d valid to class C? */
}
现在编译器可能会返回错误,因为类A中的标准构造函数已被删除。所以,我必须这样写:

public A(int a, int b, int c, string d)
{
}
public B(int a, int b, int c, string d, int e)
{
}
 public B(int a, int b, int c, string d, int e) : base(a, b, c, d)
    {
       /* It's working because I've added the variable "e" only.
       The other variables haven't been changed. */
    }
 public C(int w, int x, string y, string z) : base(a, b, c, d)
    {
         /* Class C does have completely different values than class A.
            But I'd like to have all the variables the way I've written above.
            I'd like to use variable "w" for example. But "a" should be valid 
            to use as well. How is it possible to make the variables
            a, b, c and d valid to class C? */
}
但是这种情况呢? 不必与类A做任何事情的类C也希望有自己的构造函数。看起来是这样的:

public A(int a, int b, int c, string d)
{
}
public B(int a, int b, int c, string d, int e)
{
}
 public B(int a, int b, int c, string d, int e) : base(a, b, c, d)
    {
       /* It's working because I've added the variable "e" only.
       The other variables haven't been changed. */
    }
 public C(int w, int x, string y, string z) : base(a, b, c, d)
    {
         /* Class C does have completely different values than class A.
            But I'd like to have all the variables the way I've written above.
            I'd like to use variable "w" for example. But "a" should be valid 
            to use as well. How is it possible to make the variables
            a, b, c and d valid to class C? */
}

我希望您理解我想要解决的问题。

将值从外部传递到基类构造函数真的没有什么神奇之处-派生类必须以某种方式将所有参数指定给基类构造函数,要么直接传递,要么以其他方式计算。通常,您会根据参数和一些常量值计算基本构造函数的参数:

 public C(int w, int x, string y, string z) : 
       base(w+x, x*3, 42, "Test:" + y+z) {...}

以下是对问题的代码谜题解释的答案:

如何使
公共C(intw,intx,string y,string z):base(a,b,C,d)
编译而不考虑结果”

要能够编译未作为参数传递的
base(a、b、c、d)
,您需要它们是派生类中可见的静态字段/属性或
const
值(即,在具有任何可访问性的派生类中定义或在具有非私有可访问性的基类中定义)。根据C#规范,不能将实例字段/属性用作基本构造函数的参数(推理:在这一点上实际上没有“实例”)

允许代码编译的示例(您还可以使用
public
/
internal
/
protected
访问修饰符将a、b、c、d中的任何一个移动到基类):


我希望这个代码示例可以帮助您帮助我;-)

首先,我跳转到main方法,然后使用以下代码创建一个类B的对象:

new B(global::Test.Properties.Resources.MyPlane, 100, 100, 200, 100, 10, 10);
然后,我将使用以下参数来讨论B的构造函数(尚未执行):

然后是一个具有以下参数的构造函数:

 private PictureBox picBoxImage;     
    protected Bitmap path;
    protected int x, y, width, height;
如果我像这样将实例变量更改为static,那么它可以工作,但我不能再创建两个类B的实例:

protected static Bitmap path;
protected static int x, y, width, height;
如果我再次将静态变量更改为实例变量,则会出现此错误报告的问题:

非静态字段、方法或属性“A.path”需要对象引用

    public A(Bitmap pathOfMovingObject, int xPositionObject, int yPositionObject, int widthOfObject, int heightOfObject)
        {
            path = pathOfMovingObject;
            x = xPositionObject;
            y = yPositionObject;
            width = widthOfObject;
            height = heightOfObject;

picBoxImage = new PictureBox();
            picBoxImage.Image = pathOfMovingObject;
            picBoxImage.BackColor = Color.Transparent;
            picBoxImage.SetBounds(xPositionObject, yPositionObject, widthOfObject, heightOfObject);
            this.Controls.Add(picBoxImage);
        }
然后调用B的构造函数并执行块中的代码

然后我用main方法跳回main类,创建了类C的对象

new C(1000, 650, 5, "stackoverflow", global::Test.Properties.Resources.sky);
然后我来讨论C构造函数(还没有执行)

然后我跳转到类A的构造函数,执行块

然后我再次来到C构造函数:

public C(int width, int height, int speed, string title, Bitmap path)

    : base(path, x, y, width, height)  -- There you can see the error...
非静态字段、方法或属性“A.path”需要对象引用

    public A(Bitmap pathOfMovingObject, int xPositionObject, int yPositionObject, int widthOfObject, int heightOfObject)
        {
            path = pathOfMovingObject;
            x = xPositionObject;
            y = yPositionObject;
            width = widthOfObject;
            height = heightOfObject;

picBoxImage = new PictureBox();
            picBoxImage.Image = pathOfMovingObject;
            picBoxImage.BackColor = Color.Transparent;
            picBoxImage.SetBounds(xPositionObject, yPositionObject, widthOfObject, heightOfObject);
            this.Controls.Add(picBoxImage);
        }

那么,我的问题是……如何创建两个显示在屏幕上的B类实例,并在C类中设置有效值,以及禁用上面的错误报告?

您需要告诉我们您希望解决的问题。您的目标是什么,您尝试了什么,您遇到了什么错误你的C示例没有任何意义。如果C在构造函数参数中没有定义a、b、C或d,你希望编译器从哪里获得这些值?是的,这就是问题所在。我在Main类中创建了一个对象,在那里传输值并调用类a的构造函数。这些值a=13,b=14,C=15,d=”我不知道如何更好地解释!“应该以某种方式转移到C类。然后它看起来有点像Alexei Levenkov的代码,但他在C类中进行了初始化。它应该是变量而不是值。您是否希望
新的C(…)
以某种方式从与
新的a(…)完全无关的单独调用中选择参数
?您希望看到的示例代码可能很有用……好的,这很好。让我们看一下:我的值在类A中。在类Main中,主方法是我希望传递值,如42、43、44和“为什么?”。然后这些值是这样的,例如:public A(42、43、44,”为什么?){}我不喜欢在C类中写值,因为它不再灵活了。无论我创建一个对象并将值传递给构造函数,所有的值都是在Main类中写的。@Luckybugy您的问题感觉像是“如何使
公共C(int w,int x,string y,string z):base(a,b,C,d)
compile nothing result”-我不知道您的实际目标是什么。@slugster还注意到,静态字段/属性可以使用默认值隐式初始化(您会收到警告,但不会因此出错)。