[英]How do I override the setter method of a property in C#?
I have a class with a subclass. 我有一个带有子类的类。 The superclass has a Position
property. 超类具有Position
属性。 The subclass must perform an additional operation when the Position
property is changed, so I am attempting to override the setter method and call the superclass' setter. 当Position
属性发生变化时,子类必须执行一个额外的操作,所以我试图覆盖setter方法并调用超类的setter。
I think I've got the superclass setter calling part down, but I can't figure out how the overriding syntax works here. 我想我已经让超类setter调用了一部分,但是我无法弄清楚覆盖语法是如何工作的。
Here is my best attempt: 这是我最好的尝试:
The getter is there just for proof of concept -- suppose I wanted to override that too? 吸气剂只是为了证明概念 - 假设我也想覆盖它?
The getter and setter give me errors of this form: getter和setter给出了这种形式的错误:
cannot override inherited member 'superClassName.Position.[gs]et' because it is not marked virtual, abstract, or override 不能覆盖继承的成员'superClassName.Position。[gs] et',因为它没有标记为虚拟,抽象或覆盖
Here's a screencap of the errors too for good measure: 这里有一个错误的屏幕提示,以便进行良好的衡量:
I also tried using the override keyword in front of the set
. 我也尝试在set
前面使用override关键字。 Removing the superfluous getter has no effect. 去除多余的吸气剂无效。
What is the correct syntax? 什么是正确的语法?
The override is fine. 覆盖是好的。 However, as the error message states, you need to mark the property in the base class as virtual to be able to override it: 但是,正如错误消息所述,您需要将基类中的属性标记为虚拟,以便能够覆盖它:
public virtual Vector2 Position
Unlike Java, class members are not virtual by default in C#. 与Java不同,默认情况下,类成员在C#中不是虚拟的。 If you can't change the base class, you're out of luck. 如果你不能改变基类,那你就不走运了。
Your Position
property isn't virtual in the base class, so you can't override it. 您的Position
属性在基类中不是虚拟的,因此您无法覆盖它。 If you make it virtual, you should be able to override it. 如果将其设为虚拟,则应该能够覆盖它。
It gets a bit messy overriding one bit without the other - it's not the kind of thing I do very often, so I'd have to play around with it to make sure of what's going on, but the first thing to do would be to change the base class property... 它变得有点凌乱,在没有另一个的情况下压倒一点 - 这不是我经常做的事情,所以我必须玩它以确保发生了什么,但首先要做的是更改基类属性...
This is also exactly what the compiler message is telling you: 这正是编译器消息告诉您的内容:
cannot override inherited member 'superClassName.Position.[gs]et' because it is not marked virtual, abstract, or override 不能覆盖继承的成员'superClassName.Position。[gs] et',因为它没有标记为虚拟,抽象或覆盖
It's telling you something about the base class property - so that's what you need to change in order to override it. 它告诉你一些关于基类属性的东西 - 所以你需要改变它以覆盖它。 Compiler messages are there to help - pay close attention to them! 编译器消息可以提供帮助 - 密切关注它们!
在您的基类中:添加关键字virtual
The problem is pointed out to you in the error message :) 在错误消息中指出了问题:)
You cannot override members that aren't virtual (or abstract) in the first place. 您不能首先覆盖非虚拟(或抽象)的成员。
Your superclass needs to have virtual
in the property definition: 您的超类需要在属性定义中具有virtual
:
public virtual Vector2 Position { ... }
You cannot override members not marked as virtual
or abstract
您不能覆盖未标记为virtual
或abstract
成员
Mark your Vector2
property in a super class as virtual
. 将超类中的Vector2
属性标记为virtual
。
public virtual Vector2 Position
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.