C# 抽象变量/属性?C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16247350/
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
Abstract variable/property? C#
提问by sparkzbarca
I've read some on properties and i'm not sure if thats what i want or not. Basically i have an abstract projectile class so that all "bullets" have a common implementation That any "weapon" they are attached to can expect to be able to use.
我已经阅读了一些关于属性的内容,但我不确定这是否是我想要的。基本上我有一个抽象的射弹类,所以所有的“子弹”都有一个共同的实现,它们所附加的任何“武器”都可以期望能够使用。
I know you can declare an abstract class and force the functions you declare inside it to be defined. I'd like the same thing with the variables (i think, obviously this functionality doesn't seem to exist so perhaps i'm thinking about the solution wrong)
我知道您可以声明一个抽象类并强制定义您在其中声明的函数。我想对变量做同样的事情(我认为,显然这个功能似乎不存在,所以也许我正在考虑错误的解决方案)
My issue is that since all "bullets" should have a damage i'd like to be forced to declare in code the damage value of a bullet.
我的问题是,由于所有“子弹”都应该有损坏,我想被迫在代码中声明子弹的损坏值。
There may be a case where the round is from a stun gun and it does no damage but I feel that I should still be made to declare it for 2 reasons.
可能有一种情况是子弹来自电击枪并且没有损坏,但我觉得我仍然应该出于两个原因宣布它。
The explicit declaration in the code of this does zero damage is worth the one line of code. You don't have to go well I guess it does none since it says nothing about damage it's explicitly stated.
Debugging (this is the main reason the other is minor) I want to be forced, so I don't forget. I don't want to mistype the variable name or forget to assign a value altogether and hunt for a half hour trying to figure out why my rocket or missile or bullet or whatever isn't doing any damage or is doing exactly 1 or the amount of the last projectile I used or whatever the default value of the float variable I declared in the abstract class ends up as. I want to be thrown an error right away telling me I can't continue until my new bullet has a damage.
代码中的显式声明零损害值得一行代码。你不必一切顺利,我想它没有任何作用,因为它没有明确说明损坏。
调试(这个是主因,其他次要)我想被强迫,所以我没有忘记。我不想打错变量名或忘记分配一个值并寻找半小时试图弄清楚为什么我的火箭或导弹或子弹或任何东西没有造成任何损害或恰好是 1 或数量我使用的最后一个射弹或我在抽象类中声明的浮点变量的默认值最终是什么。我想立即抛出一个错误,告诉我在新子弹损坏之前我无法继续。
采纳答案by nathan gonzalez
Properties are what you're looking for. on an interface you would just do something like this:
属性就是你要找的。在界面上,您只需执行以下操作:
public interface IProjectile
{
string Name { get; }
int Damage { get; }
void Fire();
}
Only the get method has to be defined on the interface because you only need the consumer of the interface to read from the damage property, and you'd prefer to not allow the consumer to write the damage value.
只需要在接口上定义 get 方法,因为您只需要接口的使用者从损坏属性中读取,并且您不希望让使用者写入损坏值。
The implementation would be something like this:
实现将是这样的:
public class Bullet : IProjectile
{
public string Name { get { return "Bullet"; } }
public string Damage { get { return 5; } }
public void Fire()
{
Console.WriteLine("Did {0} damage.",Damage);
}
}
回答by MAV
I think you should go with the interface approach from Nathan's answer. But it is possible to declare abstract properties.
我认为您应该采用 Nathan 的回答中的界面方法。但是可以声明抽象属性。
public abstract class Bullet
{
public abstract int Damage { get; set; }
}
public class SomeBullet : Bullet
{
public override int Damage { get; set; } //This has to be implemented.
...
}
回答by Pieter Geerkens
Here is a bit more detail on how to implement this:
以下是有关如何实现这一点的更多详细信息:
public abstract class Projectile {
public Name { get{return GetType().Name;} }
public abstract int Damgage { get; }
}
public Bullet202: Projectile {
public override int Damage { get{return 5;} }
}
public Bullet303: Projectile {
public override int Damage { get{return 8;} }
}
Distinction between using an interface or an abstract class as a basis: An abstract class ia a shared implementation, an interface is a shared contract. They share some commonality, but meet different requirements. For instance, you might want to share requirements (a common interface IProjectile) between small arms and heavier weapons, or between lethal and non-lethal weapons, while implementing the three categories on three distinct abstract classes (LethalSmallArms, NonLethalSmallArms, and Howitzers) that all implement the common interface IProjectile.
使用接口或抽象类作为基础的区别:抽象类是共享实现,接口是共享契约。它们有一些共同点,但满足不同的要求。例如,您可能希望在小型武器和重型武器之间或致命武器和非致命武器之间共享需求(一个公共接口IProjectile),同时在三个不同的抽象类(LethalSmallArms、NonLethalSmallArms和Howitzers)上实现三个类别都实现了通用接口IProjectile。

