C# 私有抽象方法的一些问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11925948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:43:37  来源:igfitidea点击:

Some trouble with private abstract methods

c#.netabstract-class

提问by miri

Let's say I make a major class Car- and I want this class to be abstract. Abstract because this is my major class, nobody should make an object of this class. This class should be only there as "backbone".

假设我开设了一个主要课程Car- 我希望这个课程是抽象的。Abstract 因为这是我的主要课程,没有人应该创建这个课程的对象。这个类应该只作为“骨干”存在。

I want that classes can be created only from subclasses of Car(lets say Mercedes, Ferrari...). Because every car should have methods like StartEngineI put it into the major class. Let's say I have this:

我希望这些类只能从Car(比如梅赛德斯、法拉利......)的子类中创建。因为每辆车都应该有像StartEngine我放在主要类中的方法。假设我有这个:

abstract class Car
{
  public string Name { get; set; }

  public abstract void StartEngine();
  private abstract bool CheckGasoline();
  //and so on...
}

class Mercedes : Car
{
  private override bool CheckGasoline()
  {
  //somehow check gasoline and return whatever...
  }

  public override void StartEngine()
  {
    if (CheckGasoline())
      //start engine...
  }
}

Well this is not gonna work. Because of private abstract:

嗯,这是行不通的。由于私人摘要:

virtual or abstract members cannot be private

So ill make every private method to protected:

所以生病使每个私有方法都受到保护:

abstract class Car
{
  public string Name { get; set; }

  public abstract void StartEngine();
  protected abstract bool CheckGasoline();
  //and so on...
}

class Mercedes : Car
{
  protected override bool CheckGasoline()
  {
  //somehow check gasoline and return whatever...
  }

  public override void StartEngine()
  {
    if (CheckGasoline())
      //start engine...
  }
}

Is this alright? I mean it's working, but is that how it should be? Using protected when I just need a method in the same class (like here: CheckGasoline()is only needed for StartEngine()). Somehow private would look better.

这可以吗?我的意思是它正在工作,但它应该是这样吗?当我只需要同一个类中的一个方法时使用protected(就像这里:CheckGasoline()只需要StartEngine())。不知何故,私人会更好看。

Any suggestions? Thank you.

有什么建议?谢谢你。

采纳答案by Marc Gravell

Yes that is fine. A sub-type cannot seeprivate methods, therefore cannot overridethem: they mustbe protected(or public etc). There is no such thing as "private to method X" in c#, so it'll have to suffice as-is.

是的,那很好。子类型不能看到私有方法,因此不能看到override它们:它们必须protected(或公共等)。在 c# 中没有“方法 X 私有”这样的东西,所以它必须按原样就足够了。

回答by Daniel

Private methodsare inaccessible to any class other than the classes that they are in - this includes derived classes.

除了它们所在的类之外的任何类都无法访问私有方法- 这包括派生类。

Protected methods, on the other hand, are accessible both to the classes that they are in AND their derived classes.

另一方面,受保护的方法对于它们所在的类及其派生类都可以访问。

Your usage of protected is correct.

您对 protected 的使用是正确的。

You may find this article to be of help: http://msdn.microsoft.com/en-us/library/ba0a1yw2(v=vs.80).aspx.

您可能会发现这篇文章有帮助:http: //msdn.microsoft.com/en-us/library/ba0a1yw2(v=vs.80).aspx

回答by m-y

You can build in override hooks. Microsoft does this with FrameworkElement.ArrangeCore(...)(which utilizes ArrangeOverride) and FrameworkElement.MeasureCore(...)(which utilizes MeasureOverride).

您可以构建覆盖钩子。Microsoft 使用FrameworkElement.ArrangeCore(...)(利用ArrangeOverride)和FrameworkElement.MeasureCore(...)(利用MeasureOverride)来实现这一点。

For example:

例如:

abstract class Car
{
  public string Name { get; set; }

  public void StartEngine()
  {
    if (CheckGasoline())
        StartEngineOverride();
  }

  protected abstract void StartEngineOverride();
  protected abstract bool CheckGasoline();

  // ...
}

class Mercedes : Car
{
  protected override bool CheckGasoline()
  {
     //somehow check gasoline and return whatever...
  }

  protected override void StartEngineOverride()
  {
     // CheckGasoline was already checked, so just do the override portion.
  }
}

回答by SuperS7

I reccomend the following:

我推荐以下内容:

Try using protectedfor your member variable declarations in the abstract class. Then use publicfor your abstract methods, as well.

尝试protected在抽象类中用于成员变量声明。然后也public用于您的抽象方法。

Next, within your derived class, you should use public and override for your methods in that derived class. However, this is another option that you have, and that is that you could use virtual as well. Therefore, instead of using a single abstract class, nest all of your potential Cartype class within one public CarClassclass, or whatever you want to call it.

接下来,在您的派生类中,您应该对派生类中的方法使用 public 和 override 。但是,这是您拥有的另一种选择,即您也可以使用虚拟。因此,不要使用单个抽象类,而是将所有潜在Car类型类嵌套在一个公共CarClass类中,或者您想调用的任何类中。

Use the virtual method for a shared method in the first declared class which is an Abstract Carclass. Then use public override for the remainder of the declared derived classes that are nested also within the CarClassclass which can override the virtual method that you declared in your asbstract Carclass. This is another option that you have, although it may limit you to only whatever you declare in the CarClassthan a distinct and separate abstract class.

将虚方法用于第一个声明的类中的共享方法,该类是一个Abstract Car类。然后对声明的派生类的其余部分使用公共覆盖,这些派生类也嵌套在CarClass类中,可以覆盖您在 asbstractCar类中声明的虚拟方法。这是您拥有的另一种选择,尽管它可能会限制您只能使用您在CarClass不同且独立的抽象类中声明的任何内容。