C# 设计问题-具有相同功能的多个类
我有一些事情要自己澄清,所以请容忍我 假设我想要一个物体,在一段时间后爆炸。如果物体在人还拿着它的时候爆炸,你会被杀死C# 设计问题-具有相同功能的多个类,c#,unity3d,design-patterns,C#,Unity3d,Design Patterns,我有一些事情要自己澄清,所以请容忍我 假设我想要一个物体,在一段时间后爆炸。如果物体在人还拿着它的时候爆炸,你会被杀死 让我们考虑下面的实现: public interface IKillable { void Kill(); } public interface IExplodable : IKillable { float TimeLeft { get; set; } void Timer(float timeToExplode); } public abst
让我们考虑下面的实现:
public interface IKillable
{
void Kill();
}
public interface IExplodable : IKillable
{
float TimeLeft { get; set; }
void Timer(float timeToExplode);
}
public abstract class Bomb: IExplodable
{
public float TimeLeft { get; set; }
public void Kill()
{
//Destroy the object
}
public void Timer(float timeLeft)
{
if (TimeLeft <= 0) {
Kill();
}
}
}
公共接口IKillable
{
无效杀死();
}
公共接口IExplodable:IKillable
{
float TimeLeft{get;set;}
无效计时器(浮动时间到展开);
}
公共抽象类炸弹:IExplodable
{
公共浮点时间限制{get;set;}
公共空杀()
{
//摧毁目标
}
公共无效计时器(浮动时间限制)
{
如果(TimeLeft您可以创建一个抽象ExplodableDevice
类,并从中继承Bomb
和Backpack
。您可以创建一个抽象ExplodableDevice
类,并从中继承Bomb
和Backpack
。拥有一个为其提供公共功能的基类对象在Unity中既常见又推荐
与其调用类Bomb
,不如将其称为更通用的类,如ExplodableDevice
(如@zmbq所回答)但是,我想更明确地说,设备由于计时器而爆炸,因此可能TimerExplodableDevice
。请注意,基类应该继承自monobhavior
,否则您将无法完全使用这些对象(因为c不允许多重继承)
这种实施的一个例子是:
public interface IKillable
{
void Kill();
}
public interface IExplodable : IKillable
{
float TimeLeft { get; set; }
void Timer(float timeToExplode);
}
public abstract class TimerExplodableDevice: MonoBehaviour, IExplodable
{
public float TimeLeft { get; set; }
public virtual void Kill()
{
//Destroy the object
}
public virtual void Timer(float timeLeft)
{
if (TimeLeft <= 0)
{
Kill();
}
}
}
// this should be in a "Devices" folder, or otherwise be called appropriately
public class Backpack : TimerExplodableDevice
{
void Start()
{
TimeLeft = 100;
}
}
// this should be in a "Devices" folder, or otherwise be called appropriately
public class Bomb : TimerExplodableDevice
{
void Start()
{
TimeLeft = 10;
}
}
公共接口IKillable
{
无效杀死();
}
公共接口IExplodable:IKillable
{
float TimeLeft{get;set;}
无效计时器(浮动时间到展开);
}
公共抽象类TimerExplodableDevice:MonoBehavior,IExplodable
{
公共浮点时间限制{get;set;}
公共虚拟void Kill()
{
//摧毁目标
}
公共虚拟无效计时器(浮动时间限制)
{
if(TimeLeft拥有一个为对象提供公共功能的基类在Unity中既常见又推荐
与其调用类Bomb
,不如将其称为更通用的类,如ExplodableDevice
(如@zmbq所回答)但是,我想更明确地说,设备由于计时器而爆炸,因此可能TimerExplodableDevice
。请注意,基类应该继承自monobhavior
,否则您将无法完全使用这些对象(因为c不允许多重继承)
这种实施的一个例子是:
public interface IKillable
{
void Kill();
}
public interface IExplodable : IKillable
{
float TimeLeft { get; set; }
void Timer(float timeToExplode);
}
public abstract class TimerExplodableDevice: MonoBehaviour, IExplodable
{
public float TimeLeft { get; set; }
public virtual void Kill()
{
//Destroy the object
}
public virtual void Timer(float timeLeft)
{
if (TimeLeft <= 0)
{
Kill();
}
}
}
// this should be in a "Devices" folder, or otherwise be called appropriately
public class Backpack : TimerExplodableDevice
{
void Start()
{
TimeLeft = 100;
}
}
// this should be in a "Devices" folder, or otherwise be called appropriately
public class Bomb : TimerExplodableDevice
{
void Start()
{
TimeLeft = 10;
}
}
公共接口IKillable
{
无效杀死();
}
公共接口IExplodable:IKillable
{
float TimeLeft{get;set;}
无效计时器(浮动时间到展开);
}
公共抽象类TimerExplodableDevice:MonoBehavior,IExplodable
{
公共浮点时间限制{get;set;}
公共虚拟void Kill()
{
//摧毁目标
}
公共虚拟无效计时器(浮动时间限制)
{
如果(TimeLeft)我不清楚,背包和炸弹完全一样?因为它可以和炸弹(爆炸)有相同的功能。我只是举了一个坏例子。你能早点解释一下吗?我不确定你不明白什么?@SeyedRaoufModarresi问题是炸弹有相同的功能(爆炸和杀死)就像背包一样,但这两个设备是不同的。我不太清楚,背包和炸弹完全一样?因为它可以和炸弹(爆炸)有相同的功能。我只是举了一个坏例子。。你能早点解释一下吗?我不知道你不明白什么?@SeyedRaoufModarresi问题是炸弹的功能(爆炸和死亡)与背包相同,但这两种设备是不同的谢谢,这很有帮助!:)还有,在这种情况下,为什么我需要从MonoB扩展?@rootpanthera您不需要,只有在您想的情况下才应该这样做。这取决于需求Hanks,这很有帮助!:)还有,在这种情况下,为什么我需要从MonoB扩展?@rootpanthera您不需要,只有在您想要的情况下才应该这样做。这取决于需求