2013-10-29 47 views
4

新手問題,我的課程指導者已經有效地將焦油從我身上混淆了。 Is-A的關係是說狗是一種動物。然後,你將動物作爲基礎類與狗作爲派生類。Is-A vs Has-A的關係

但是對於我們的課程項目,我必須根據一個接口來創建一個課程。用一個單獨的類只有一個方法給一個隨機數。看起來他們並不真正相關,但我必須從主類調用randomNumber方法。

他在指令(這是可怕的)期間的問題說要弄清楚這是IS-A還是HAS-A關係。基於事實,我會說這是一個HAS-A關係,但我不知道如何引用該類,但是要創建一個分配給該類的變量,以便我可以使用它。

有沒有另一種方法來引用我不知道的類?

Devices randomMeasure = new Devices(); //Random measurement Class 
this.mostRecentMeasure = randomMeasure.GetMeasurement(); 
+0

可能重複的[C#接口實現關係只是「可以做」的關係?](http://stackoverflow.com/questions/255644/c-sharp-interface-implementation-relationship-is-just -can-do-relationship) – Habib

+1

接口是CAN-DO關係。這不是一個IS-A也不是一個HAS-A –

+0

我不清楚前兩段是如何得出這個問題的。如果該功能是「靜態」,則可以在沒有實例的情況下在類上引用功能,但這與繼承或接口抽象無關。 – David

回答

6

一種接口是一個可以做的關係。
可以一個類的實例分配給是

  1. 類本身或
  2. 類的基類的一個或
  3. 的接口的類或類型的任何變量它的一個基類實現。

在您的例子(情況3),這意味着:

IDevice randomMeasure = new Devices(); 
randomMeasure.GetMeasurement(); 

這個概念被作爲polymorphism提及。

+0

感謝Can-Do定義。我已經瞭解了接口(簡要地)已經足夠完成該項目至少。仍然不明白使用它們的優點,但謝謝。 – Crazyd

+0

接口定義實現它們的類的外部結構。一個實用的優點是一個類可以實現大量的接口,而它只能有一個基類。重要的是繼承是一個字符串Is-A關係。 Can-Do是一種較弱的關係,也可以用於在不相互關聯的類之間共享行爲。 – Markus

4

如果你的類與其他類繼承,它是一個「IS-A」關係。

如果類在構造函數中傳遞給另一個類,這是一個「HAS-A」的關係。

例如

public class foo : bar 
{ 
    // IS-A 
} 



public interface IBar 
{ 
} 

public class Bar : IBar 
{ 
} 

public class Foo : IBar 
{ 
    private Bar _bar; 

    public foo(Bar bar) 
    { 
     _bar = bar; 

    } 

    // HAS-A 
} 

但是,這並不明顯,你的問題。在第二種情況下,FooIS-AIBarHAS-ABar

+0

謝謝你的HAS-A定義。我需要......從我的角度來看,兩人之間自然沒有任何關係。無論誰寫的說明必須有不同的想法。想象一下,獲得一個項目的指示,永遠不會給你這個項目的目標。 – Crazyd

3

這是一個IS-A的關係,所述

要引用它,則應該創建混凝土類的一個實例,並將其分配給接口類型的變量:

interface IMeasureable 
{ 
    public int GetMeasurement(); 
} 
class Device : IMeasureable 
{ 
    public int GetMeasurement() 
    { 
     return .... 
    } 
} 
class App 
{ 
    public void Main() 
    { 
     IMeasureable thing = new Device(); 
     int x = thing.GetMeasurement(); 
    } 
} 
2

要回答你的問題(雖然我不知道你爲什麼問) 您可能

  1. 實例化類

  2. 讓它靜態

  3. 保持類對象應用程序變量/緩存/視圖狀態等

  4. 使用擴展方法whic^h會給你隨機數(你不必使用另一個類在這種情況下)

2

如果我們接受接口是CAN-DO關係則教授必須要求兩個具體的類之間的關係。我會打電話給這些類ClassBasedOnInterfaceSeperateClassClassBasedOnInterface又名「主類」。我們也知道要求是:

我必須從主類調用randomNumber方法。

interface IMyInterface 
{ 
    void Call(); 
} 

//Requirement: I have to call the randomNumber Method from the main class. 
public class ClassBasedOnInterface : IMyInterface 
{ 
    SeperateClass hasASeperateClass = new SeperateClass(); 

    public void Call() 
    { 
     //Could be local variable. 
     //Or, the professor could be implying that ClassBasedOnInterface has a SeperateClass 
     //member that is used to make the call to GetRandomNumber(). 
     //Or, GetRandomNumber() could be static. 
     //var seperateClass = new SeperateClass();//I do not consider a private local variable a HAS-A relationship 
     //seperateClass.GetRandomNumber(); 

     hasASeperateClass.GetRandomNumber(); 
    } 
} 

public class SeperateClass 
{ 
    public void GetRandomNumber() 
    { 

    } 
} 

仍有許多懸而未決的問題。但是,沒有理由相信ClassBasedOnInterface也來自SeperateClass。有證據表明SeperateClassClassBasedOnInterface的成員,因爲要求是ClassBasedOnInterface必須調用SeperateClass.GetRandomNumber()。換句話說,沒有理由相信IS-A關係存在。

出於這個原因,我相信答案是:

ClassBasedOnInterface有HAS-A與SeperateClass

1

接口介紹一些關於一類關係,但不一定定義它。正如其他人所說,這更多的是一種Can-Do關係。你可以做些什麼/以及這個類,等等。

有一個關係將是一個類,它利用其他類來代表0 .. *關係的東西。

// This interface doesn't really define what a class is, only 
// that it can, in fact, have Cheeseburgers. 
public interface ICanHasCheeseburgers 
{ 
    List<Cheeseburger> Cheeseburgers { get; } 
} 

// This abstract class, defines what a derived class 'is'. 
// If you are familiar with biology, imagine: kingdom, phylum, class, order, 
// genius, species. It's different levels of abstraction for a 'thing'. 
public abstract class Animal 
{ 

} 

// The cat class derives from the Animal class just as a Dog class might. 
// This is a Is-A relationship; the Cat is an Animal. It also implements 
// the ICanHasCheeseburgers interface which represents a Can-Do relationship. 
public class Cat : Animal, ICanHasCheeseburgers 
{ 
    // this property represents a Has-A relationship between our Cat 
    // class and a Cheeseburger class. The cat can have any number of 
    // Cheeseburger objects. 
    public List<Cheeseburger> Cheeseburgers { get; private set; } 

    public Cat(RandomNumberGenerator generator) 
    { 
      if (generator != null) 
      { 
       var number = generator.GetRandom(); 
       var burgers = new List<Cheeseburger>(); 

       while(number > 0) { 
        burgers.add(new Cheeseburger()); 
        number--; 
       } 

       Cheeseburgers = burgers; 
      } 
    } 
} 

Can-Do和Is-A關係允許我們抽象邏輯。假設我們有任何數量的Animals,我們想知道他們有多少Cheeseburgers在一起。如果我們必須爲每隻動物編寫一種方法,然後嘗試將它們加在一起,這將不會很有趣。但是抽象一下,我們可以寫出一種方法。

public static class Util 
{ 
    public int GetTotalCheeseburgerCount(List<Animal> animals) 
    { 
      var total = 0; 

      foreach(var animal in animals) 
      { 
       // not every animal can have cheeseburgers, so we 
       // can ignore this animal if it can't. 
       var canHasCheeseburger = animal as ICanHasCheeseburger; 
       if (canHasCheeseburger != null) 
       { 
        if (canHasCheeseburger.Cheeseburgers != null) 
        { 
         total += canHasCheeseburger.Cheeseburgers.Count; 
        } 
       } 
      } 

      return total; 
     } 
}