典型的从另一个构造函数调用构造函数,但给了我一个错误
在尝试在同一个类中对构造函数调用进行常规构造函数时:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设“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.
问题是构造函数调用中的内联 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?
我不知道 SecondOrderEquation 是什么,但它似乎是一种类型..您的意思是 typeof(SecondOrderEquation) 吗?
I do not know what SecondOrderEquation, but it seems to be a type.. Did you mean typeof(SecondOrderEquation)?