典型的从另一个构造函数调用构造函数,但给了我一个错误

发布于 2024-08-15 21:06:13 字数 873 浏览 3 评论 0原文

在尝试在同一个类中对构造函数调用进行常规构造函数时:

    public Equation(OrdinaryEquations equation, 
                    double xTranslation,
                    double yTranslation, 
                    double rotationAngle)
      : this( (equation == OrdinaryEquations.SecondOrder) ? new EquationFunction(SecondOrderEquation) : new EquationFunction(ThirdOrderEquation), 
        xTranslation, yTranslation, rotationAngle){}

OrdinaryEquations 是枚举

EquationFunction 是委托

我收到此错误: “非静态字段、方法或属性需要对象引用” foreach:“新EquationFunction(SecondOrderEquation)”和“新EquationFunction(ThirdOrderEquation)” 这听起来像是当我尝试从静态方法访问对象数据时

另一个构造函数 sig。 :

public Equation(EquationFunction equation, double xTranslation, double yTranslation, double rotationAngle){}

我这里有什么遗漏的吗?我不知道该怎么办!

PS我删除了条件运算符并将其设为正常的对象初始化,但给出了相同的错误

While trying to make a regular constructor to constructor call in the same class :

    public Equation(OrdinaryEquations equation, 
                    double xTranslation,
                    double yTranslation, 
                    double rotationAngle)
      : this( (equation == OrdinaryEquations.SecondOrder) ? new EquationFunction(SecondOrderEquation) : new EquationFunction(ThirdOrderEquation), 
        xTranslation, yTranslation, rotationAngle){}

OrdinaryEquations is enumeration

EquationFunction is a delegate

I get this error :
" An object reference is required for the non-static field, method, or property"
foreach : " new EquationFunction(SecondOrderEquation)" and "new EquationFunction(ThirdOrderEquation)"
which sounds like when I try to access object data from static method

the other constructor sig. :

public Equation(EquationFunction equation, double xTranslation, double yTranslation, double rotationAngle){}

Is there something I'm missing here ? I can't figure out what to do !!

P.S I removed the conditional operator and made it a normal object initialization but gave the same error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走过海棠暮 2024-08-22 21:06:13

我假设“SecondOrderEquation”是您的类“Equation”中的方法名称。
“SecondOrderEquation”的用法指的是 this.SecondOrderEquation ,它需要一个“Equation”类型的实例作为上下文。

要解决此问题,您可以尝试将“SecondOrderEquation”和“ThirdOrderEquation”设为静态方法。

I assume "SecondOrderEquation" is a method name in your class "Equation".
The usage of "SecondOrderEquation" refers to this.SecondOrderEquation which needs a instance of type "Equation" to be a context.

To fix it, you can try to make "SecondOrderEquation" and "ThirdOrderEquation" to be static methods.

递刀给你 2024-08-22 21:06:13

问题是构造函数调用中的内联 if 语句。

一般来说,这种类型的对象构造有点代码味道。

也许您应该考虑创建一个工厂方法?

The problem is your inline if statement in the constructor call.

Generally speaking this type of object construction is a bit of a code smell.

Perhaps you ought to consider creating a factory method instead?

感受沵的脚步 2024-08-22 21:06:13

新方程函数(二阶方程)

我不知道 SecondOrderEquation 是什么,但它似乎是一种类型..您的意思是 typeof(SecondOrderEquation) 吗?

new EquationFunction(SecondOrderEquation)

I do not know what SecondOrderEquation, but it seems to be a type.. Did you mean typeof(SecondOrderEquation)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文