2016-02-06 41 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

相關問題