2014-12-27 103 views
0

我想知道如何在不知道類型的情況下傳遞對象的實例。我想知道這一點,因爲如果我有100種動物類型,那麼我不想有100個if語句或轉換。我提供了一個片段,這是我想要基本實現的一個例子。現在,在我發表評論的地方顯然不起作用。在不知道類型的情況下傳遞類的實例

using System.IO; 
using System; 
using System.Collections.Generic; 

class Program 
{ 
    Dictionary<string, dynamic> myAnimals = new Dictionary<string, dynamic>(); 

    Program(){ 
     myAnimals.Add("Maggie", new Dog("Maggie")); 
     myAnimals["Maggie"].bark(); 

     myAnimals.Add("Whiskers", new Cat("Whiskers")); 
     myAnimals["Whiskers"].meow(); 

     animalClinic clinic = new animalClinic(); 
     clinic.cureAnimal(myAnimals["Whiskers"]); 
    } 

    static void Main() 
    { 
     new Program(); 
    } 
} 

class Dog{ 
    string name; 

    public Dog(string n){ 
     name = n; 
    } 

    public void bark(){ 
     Console.WriteLine("\"Woof Woof\" - " + name); 
    } 
} 

class Cat{ 
    string name; 

    public Cat(string n){ 
     name = n; 
    } 

    public void meow(){ 
     Console.WriteLine("\"Meow Meow\" - " + name); 
    } 
} 

class animalClinic(){ 
    public void cureAnimal(object animal){ //This is where I need some help. 
     if(animal.name == "Maggie"){ //I know I can use 'animal.GetType() == ...' That isn't the point. 
      Console.WriteLine("We heal fine dogs!"); //The point is to access various methods within the object. 
     }else{//I know it kind of beats the point of Type-Safety, but this is only an example and another way to do this is perfectly fine with me. 
      Console.WriteLine("Eww a cat!") 
     } 
    } 
} 

如果有人知道替代解決方案,請繼續分享!

謝謝。

編輯:我想你還需要引用動物而不是僅僅傳遞它。

回答

3

這是多態是:

public interface IAnimal 
{ 
    string name {get;set;} 

    void speak(); 

    void cure(); 
} 

public class Dog : IAnimal 
{ 
    public Dog (string n) 
    { 
     name = n; 
    } 

    public string name {get;set;} 

    public void bark() 
    { 
     Console.WriteLine("\"Woof Woof\" - " + name); 
    } 

    public void speak() { bark(); } 

    public void cure() 
    { 
     Console.WriteLine("We heal fine dogs!"); 
    } 
} 

public class Cat : IAnimal 
{ 
    public Cat(string n) 
    { 
     name = n; 
    } 

    public string name {get;set;} 

    public void meow() 
    { 
     Console.WriteLine("\"Meow Meow\" - " + name); 
    } 

    public void speak() { meow(); } 

    public void cure() 
    { 
     Console.WriteLine("Eww a cat!"); 
    } 
} 

class Program 
{ 
    static Dictionary<string, IAnimal> myAnimals = new Dictionary<string, IAnimal>(); 

    static void Main() 
    { 
     myAnimals.Add("Maggie", new Dog("Maggie")); 
     myAnimals["Maggie"].speak(); 

     myAnimals.Add("Whiskers", new Cat("Whiskers")); 
     myAnimals["Whiskers"].speak(); 

     animalClinic clinic = new animalClinic(); 
     clinic.cureAnimal(myAnimals["Whiskers"]); 
    } 
} 

public class animalClinic 
{ 
    public void cureAnimal(IAnimal animal) 
    { 
     animal.cure(); 
    } 
} 
+0

你爲什麼要創建在animalClinic每個動物類型明確分開的方法來提供?爲什麼不只是治癒動物(內動物)和動物。描述或任何適當的財產?看起來你在這裏顛覆了多態的全部觀點。 – Jeb 2014-12-27 18:32:04

+0

我試圖展示兩種技術......有時候你想要像這樣的功能來生活在代碼庫的不同位置。但是這不起作用,因爲Dictionary不支持類型差異。編輯即將到來。 – 2014-12-27 18:36:12

0

創建interface(包含定義一組相關的功能,一個類或結構可以實現的)呼籲IAnimal含有Description屬性返回「我們撫平細小的狗!」爲您的Dog類等。您的每個具體動物類實現此接口意味着您可以撥打cureAnimal方法中的Description屬性。

0

使用polymorphism

public abstract class Animal 
{ 
    public string Name { get; set; } 

    public abstract void Cure(); 
} 

public class AnimalClinic 
{ 
    public void CureAnimal(Animal animal) 
    { 
     animal.Cure(); 
    } 
} 

public class Dog : Animal 
{ 
    public override void Cure() 
    { 
     Console.WriteLine("We heal fine dogs!"); 
    } 
} 

如果要定義AnimalClinic類的裏面就像你現在要做的Cure邏輯,你可能有某種的執行條件執行

此條件執行不必像大規模的if語句甚至switch那樣笨重。您可以在SO上研究關於if陳述的替代解決方案。事實上,Joel Coehoorn提供了一個。

相關問題