C# - 当每个继承类都需要基类的 getter 方法,但只需要一个继承类的 setter 方法时,我该怎么办

发布于 2024-09-04 01:28:20 字数 671 浏览 9 评论 0原文

我有一个名为 WizardViewModelBase 的抽象类。

我的所有 WizardXXXViewModel 类都继承自抽象基类。

底座具有带有吸气剂的属性。每个子类都需要并重写该字符串

属性作为 ViewModel 的 DisplayName。

只有一个名为 WizardTimeTableWeekViewModel 的 ViewModel 需要一个 setter,因为我必须设置

ViewModel 是 A 周还是 B 周的时间表。使用 2 个 ViewModel(例如

WizardTimeTableWeekAViewModel 和 WizardTimeTableWeekBViewModel)将是多余的。

我不想覆盖所有其他类中的设置器,因为它们不需要设置器。

我可以以某种方式告诉子类它不需要覆盖设置器吗?

或者还有其他建议吗?

通过接口,我可以自由地使用 getter 或 setter,但拥有许多空的 setter

属性对我来说不是一个选择。

有趣..我只是想如果我真的需要设置 WizardPages 的所有 DisplayNames 与我首先所说的相反,会发生什么。也许由于本地化,我不应该对 getter 中的字符串进行硬编码并将字符串放入资源文件中,然后我需要在每个子类中的任何位置设置一个 setter XD

I have a abstract class called WizardViewModelBase.

All my WizardXXXViewModel classes inherit from the base abstract class.

The base has a property with a getter. Every sub class needs and overrides that string

property as its the DisplayName of the ViewModel.

Only ONE ViewModel called WizardTimeTableWeekViewModel needs a setter because I have to set

wether the ViewModel is a timetable for week A or week B. Using 2 ViewModels like

WizardTimeTableWeekAViewModel and WizardTimeTableWeekBViewModel would be redundant.

I do not want to override the setter in all other classes as they do not need a setter.

Can I somehow tell the sub class it needs not to override the setter?

Or any other suggestion?

With interfaces I would be free to use getter or setter but having many empty setter

properties is not an option for me.

Funny.. I have just thought what would happen if I really would need to SET all DisplayNames of the WizardPages contrary what I said firstly. Maybe I should not hardcode the strings in the getter and put the strings in a reesource file because of localization, then I need a setter anywhere in every sub class XD

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

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

发布评论

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

评论(4

我是有多爱你 2024-09-11 01:28:20

不要将 setter 方法声明为 virtual。

如果出于某种原因(我想不出一个!),您需要它在继承层次结构的顶部是虚拟的,然后在覆盖它时使用密封:

http://msdn.microsoft.com/en-us/library/aa645769(VS.71).aspx

Don't declare the setter method as virtual.

If for some reason (I can't think of one!) you need for it to be virtual at the top of your inheritance hierarchy then use sealed when you override it:

http://msdn.microsoft.com/en-us/library/aa645769(VS.71).aspx

左秋 2024-09-11 01:28:20

如果属性不是抽象的,则任何基类都可以选择仅重写 setter、getter 或两者。

如果您希望子类无法访问您的 setter(仅指定子类除外),则可以仅对 getter 使用内部访问修饰符,并实现不应访问另一个程序集中的 setter 的类。

If the property is not abstract, then any base class may choose to only override the setter, the getter, or both.

If you want your subclasses not to have access to your setter, except for only a given subclass, you can use the internal access modifier only to the getter, and implement classes that shouldn't have access to the setter in another assembly.

江挽川 2024-09-11 01:28:20

您应该引入一个新的抽象类,它将包含在 WizardViewModelBase 类中。该类应该使用 get 和 set 访问器重写属性,但会保留属性抽象,如下所示:

    public abstract string DisplayName 
    {
        get;
        set;
    }

然后您可以使用此类作为 WizardTimeTableWeekViewModel 类的基类,并且您将能够重写 get 和 set 访问器。

You should introduce a new abstract class, which will inhere WizardViewModelBase class. That class should override a property using both get and set accessors, but will leave a property abstract, like this:

    public abstract string DisplayName 
    {
        get;
        set;
    }

Then you can use this class as a base class for WizardTimeTableWeekViewModel class and you wil be able to override both get and set accessors.

笑脸一如从前 2024-09-11 01:28:20

我将使用受保护的设置器并创建一个单独的函数来设置该值。毕竟,该类没有与其他类相同的接口,因此将其与其他类明显区分开来应该有助于可读性。

class Base
{
  public String Value { get; protected set; }
}

class SpecialChild : Base
{
  public void SetValue(String newValue) { this.Value = newValue; }
}

// Somewhere else
SpecialChild special = foo as SpecialChild;
if (special != null)
{
  special.SetValue('newFoo');
}
else
{
  foo.DoSomeStuff();
}

I'd use a protected setter and create a seperate function to set the value. After all the class does not have the same interface as the others so distinguishing it visibly from the others should help readability.

class Base
{
  public String Value { get; protected set; }
}

class SpecialChild : Base
{
  public void SetValue(String newValue) { this.Value = newValue; }
}

// Somewhere else
SpecialChild special = foo as SpecialChild;
if (special != null)
{
  special.SetValue('newFoo');
}
else
{
  foo.DoSomeStuff();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文