C# 不确定何时使用抽象属性,何时不使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12254438/
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
Not sure when to use an abstract property and when not
提问by miri
I'm not really sure what looks better or when do I really use in abstract classes and properties, or when to use non abstract properties. I'll try to make a simple example. Let's say I have this:
我不确定什么看起来更好,或者我什么时候真正在抽象类和属性中使用,或者什么时候使用非抽象属性。我会尝试做一个简单的例子。假设我有这个:
abstract class Human
{
public GenderType Gender { get; set; }
public string Name { get; set; }
public Date Born { get; set; }
public bool IsNerd { get; set; }
abstract public void Speak();
abstract public void Sleep();
abstract public void AnoyingPeopleOnStackOverflow();
//... so on
}
class Peter : Human
{
//Peter is special, he got a second name
//But thats all, everything else is the same as like on other humans
public string SecondName { get; set; }
//...override abstract stuff
}
Is this alright? As I understood, I don't have to use an abstract property if I dont want to override it. And in this situation it would be ok, just the methods like Speak, Sleepand so on should be abstract.
这可以吗?据我了解,如果我不想覆盖它,我不必使用抽象属性。在这种情况下就可以了,只是像Speak,之类的方法Sleep应该是抽象的。
Now, if this is ok, when would or should I use an abstract property?
现在,如果这没问题,我什么时候会或应该使用抽象属性?
回答by Tim Medora
Use an abstract property when you have no default implementation and when derived classes must implement it.
当您没有默认实现并且派生类必须实现它时,请使用抽象属性。
Use a virtual property when you have an implementation in the base class but want to allow overriding.
当您在基类中有实现但希望允许覆盖时,请使用虚拟属性。
Use the overridekeyword to override a member. Mark the member as sealed overrideif it should not be overridden again.
使用override关键字覆盖成员。将该成员标记为sealed override不应再次覆盖。
Don't mark the property as abstractor virtualif you don't want it to be overridden.
不要将属性标记为abstract或者virtual如果您不希望它被覆盖。
Use the newkeyword to hide a non-abstract, non-virtual member (this is rarely a good idea).
使用new关键字隐藏非抽象、非虚拟成员(这很少是一个好主意)。
How to: Define Abstract Properties
I find that abstract properties often occur in a design which implies that they will have type-specific logic and/or side effects. You are basically saying, "here is a data point that all subclasses must have, but I don't know how to implement it". However, properties which contain a large amount of logic and/or cause side effects may not be desirable. This is an important consideration, though there is no fixed right/wrong way to do it.
我发现抽象属性经常出现在设计中,这意味着它们将具有特定于类型的逻辑和/或副作用。您基本上是在说,“这是所有子类都必须具有的数据点,但我不知道如何实现它”。然而,包含大量逻辑和/或导致副作用的属性可能并不理想。这是一个重要的考虑因素,尽管没有固定的正确/错误方法来做到这一点。
See:
看:
Personally, I find that I use abstract methods frequently but abstract properties rarely.
就个人而言,我发现我经常使用抽象方法,但很少使用抽象属性。
回答by Daniel
Use abstract when all sub-classes haveto implement the method/property. If there's no need for each and every sub-class to implement it, then don't use it.
当所有子类都必须实现方法/属性时使用抽象。如果不需要每个子类都实现它,那么就不要使用它。
As for your example, if SecondNameis not required for each person, then there's no need to make an abstract property in the base class. If on the other hand, every person does need a second name, then make it an abstract property.
至于你的例子,如果SecondName不是每个人都需要,那么就没有必要在基类中创建一个抽象属性。另一方面,如果每个人都需要第二个名字,那么将其设为抽象属性。
Example of correct usage of an abstract property:
正确使用抽象属性的示例:
public class Car
{
public abstract string Manufacturer { get; }
}
public class Odyssey : Car
{
public override string Manufacturer
{
get
{
return "Honda";
}
}
}
public class Camry : Car
{
public override string Manufacturer
{
get
{
return "Toyota";
}
}
}
Making Makerabstract is correct because everycar has a manufacturer and needs to be able to tell the user who that maker is.
使Maker抽象的是正确的,因为每个车有一个制造商和需要能够分辨出谁是制造者是用户。
回答by Jon Egerton
An abstract property would be used where you want the class to always expose the property, but where you can't pin down the implemetation of that property - leaving it up to/forcing the inheriting class to do so.
抽象属性将用于您希望类始终公开该属性的地方,但您无法确定该属性的实现 - 将其留给/强制继承类这样做。
There's an example here, where the abstract class is named Shape, and it exposes an abstract Areaproperty. You can't implement the Areaproperty in the base class, as the formula for area will change for each type of shape. All shapes have an area (of some sort), so all shapes should expose the property.
这里有一个例子在这里,这里的抽象类被命名Shape,并将它暴露一个抽象的Area财产。您不能Area在基类中实现该属性,因为面积的公式会因每种形状类型而变化。所有形状都有一个面积(某种形式),因此所有形状都应该公开该属性。
Your implementation itself looks just fine. Was trying to think of a sensible example of an abstract property for a Human, but couldn't think of anything reasonable.
您的实现本身看起来很好。试图为 a 想一个抽象属性的合理例子Human,但想不出任何合理的例子。
回答by Guffa
Abstract members are simply virtual members that you have to override. You use this for something that has to be implemented, but can't be implemented in the base class.
抽象成员只是您必须覆盖的虚拟成员。您可以将它用于必须实现但不能在基类中实现的东西。
If you want to make a virtual property, and want that it has to be overridden in the class that inherits your class, then you would make it an abstract property.
如果您想创建一个虚拟属性,并希望它必须在继承您的类的类中被覆盖,那么您可以将其设为抽象属性。
If you for example have an animal class, its ability to breathe would not be possible to detemine just from the information that it's an animal, but it's something that is pretty crucial:
例如,如果你有一个动物类,它的呼吸能力不可能仅根据它是动物的信息来确定,但这是非常关键的:
public abstract class Animal {
public abstract bool CanBreathe { get; }
}
For a fish and a dog the implementation would be different:
对于鱼和狗,实现会有所不同:
public class Dog : Animal {
public override bool CanBreathe { get { return !IsUnderWater; } }
}
public class Fish : Animal {
public override bool CanBreathe { get { return IsUnderWater; } }
}
回答by Jon Hanna
I know what I want them to do, I don't care how they do it: Interface.
我知道我想让他们做什么,我不在乎他们是怎么做的:界面。
I know what I want them to do, I don't care how they do some of it, but I've firm ideas on how they'll (or at least most of them) do other bits: Abstract class.
我知道我想让他们做什么,我不在乎他们如何做一些事情,但我对他们(或至少大多数人)如何做其他方面有坚定的想法:抽象类。
I know what I want them to do, and how most of them will do it: Concrete class with virtual members.
我知道我想让他们做什么,以及他们中的大多数人会怎么做:具有虚拟成员的具体类。
You can have other cases such as e.g. an abstract class with no abstract members (you can't have an instance of one, but what functionality it offers, it offers completely), but they're rarer and normally come about because a particular hierarchy offers itself cleanly and blatantly to a given problem.
你可以有其他情况,例如一个没有抽象成员的抽象类(你不能有一个实例,但它提供什么功能,它完全提供),但它们很少见,通常是因为特定的层次结构干净而公然地解决给定的问题。
(Incidentally, I wouldn't think of a Peter as a type of Human, but of each peter as an instance of human who happens to be called Peter. It's not really fair to pick on example code in this way, but when you're thinking about this sort of issue it's more pertinent than usual).
(顺便说一句,我不会将 Peter 视为一种人类,而是将每个 peter 视为碰巧被称为 Peter 的人类的一个实例。以这种方式选择示例代码并不公平,但是当您“重新考虑这类问题,它比平时更相关)。

