2016-02-06 33 views
0

为什么接口不能实现这样的方法?C#为什么接口不能实现这样的方法?什么是解决我的问题的解决方法?

public interface ITargetableUnit { 
     //Returns whether the object of a class that implements this interface is targetable 
    bool unitCanBeTargeted(){ 
     bool targetable = false; 
     if(this is Insect){ 
      targetable = (this as Insect).isFasterThanLight(); 
     } 
     else if(this is FighterJet){ 
      targetable = !(this as FighterJet).Flying; 
     } 
     else if(this is Zombie){ 
      targetable = !(this as Zombie).Invisible; 
     } 
     return targetable; 
    } 
} 

昆虫,和僵尸都已经从基类派生生物和FighterJet从类机派生然而,并非所有的生物-S是靶向,不使用ITargetableUnit inteface。

有没有解决我面临的问题的解决方法?

+0

接口没有行为,你要找的是抽象类 – Uriil

+0

因为接口不允许你提供实现。取而代之的是像这样的结构,'Creature' - >'TargetableCreature' - >'Insect' /'Ghost' /'Zombie'和'Creature' - >'NonTargetableCreature' - >'WhateverIsntTargetable'然而,因为你的界面有不同的每种类型的实现,你应该在每个类中实现接口。 – dman2306

+1

你应该只需要一个'ITargetableCreature'和'INonTargetableCreature',然后检查主要方法中的任何一个。 –

回答

1

也许你想要一个抽象类而不是一个接口?

接口定义了类提供的方法。抽象类也可以做到这一点,但也可以为每个孩子接管一些计算。

请注意,从技术角度来看,Insect也可以是Zombie

快乐编码!

public abstract class TargetableUnit 
{ 
    //Returns whether the object of a class that implements this interface is targetable 
    public bool unitCanBeTargeted() 
    { 
     bool targetable = false; 
     if (this is Insect) 
     { 
      targetable = (this as Insect).isFasterThanLight(); 
     } 
     else if (this is FighterJet) 
     { 
      targetable = !(this as FighterJet).Flying; 
     } 
     else if (this is Zombie) 
     { 
      targetable = !(this as Zombie).Invisible; 
     } 

     return targetable; 
    } 
} 

public class Insect : TargetableUnit 
{ 
    public bool isFasterThanLight() 
    { 
     return System.DateTime.UtcNow.Second == 0; 
    } 
} 
public class FighterJet : TargetableUnit 
{ 
    public bool Flying { get; set; } 
} 
public class Zombie : TargetableUnit 
{ 
    public bool Invisible { get; set; } 
} 
+3

延长你的答案。添加原因。否则,这应该是一个评论。 –

+0

这里你是... –

7

就像大家说的,你不能定义接口的行为。将接口继承到特定的类。

public interface ITargetableUnit 
{ 

    bool unitCanBeTargeted(); 

} 

public class Insect : ITargetableUnit //you can add other interfaces here 
{ 

    public bool unitCanBeTarget() 
    { 
      return isFasterThanLight(); 
    } 
} 

public class Ghost : ITargetableUnit 
{ 
    public bool unitCanBeTarget() 
    { 
      return !Flying(); 
    } 
} 

public class Zombie : ItargetableUnit 
{ 
    public bool unitCanBeTarget() 
    { 
      return !Invisible(); 
    } 
} 
+1

作为一个挑剔的人,你不会继承类中的接口,而是实现它们。 –

+0

@PrestonGuillot说它的正确方法是接口实现inheritance.https://msdn.microsoft.com/en-us/library/aa664593(v = vs.71).aspx。至少我这样想,有可能是错的。 – mybirthname

+0

@mybirthname不,你误解了该页面的标题。它指的是从一个实现接口的类继承*是否意味着你也实现了这个接口。 –

1

只是为了记录在案,可以真正做到这一点(DONT!),但这个心不是考虑一个很好的做法,使你有存取权限代码extensionmethods。 Mybirthname's解决方案是要走的路,这只是为了演示。

public interface ITargetableUnit { } 


public static class ITargetableUnitExtension 
{ 

    public static bool unitCanBeTargeted(this ITargetableUnit unit) 
    { 
     bool targetable = false; 
     Insect insect = unit as Insect; 
     if(insect != null) 
      return insect.isFasterThanLight(); 
     FighterJet jet = unit as FighterJet; 
     if(jet != null) 
      return !jet.Flying; 
     Zombie zombie = unit as Zombie; 
     if(zombie != null) 
      return zombie.Invisible; 
     return false; 
    } 
} 
+0

另请参阅:[扩展方法](https://msdn.microsoft.com/library/bb383977.aspx) – Corak

相关问题