首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >传递类的实例而不知道类型

传递类的实例而不知道类型
EN

Stack Overflow用户
提问于 2014-12-27 18:07:06
回答 4查看 85关注 0票数 0

我想知道如何在不知道对象的类型的情况下传递对象的实例。我想知道这一点,因为如果我有100种动物类型,那么我不想有一个100 if语句或一个开关。我提供了一个片段,这是我想要实现的一个例子。现在,它显然不起作用,我把评论放在哪里。

代码语言:javascript
复制
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!")
        }
    }
}

如果有人知道这方面的替代解决方案,那么请继续分享!

谢谢。

编辑:我认为你也需要参考动物,而不是只是把它传下来。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-12-27 18:17:57

这就是多态性的含义:

代码语言:javascript
复制
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();
    }
}
票数 3
EN

Stack Overflow用户

发布于 2014-12-27 18:15:18

创建一个名为接口IAnimal (包含一个类或结构可以实现的一组相关功能的定义),它包含一个返回“我们治愈好狗!”的Description属性!对于Dog类等等,每个具体的动物类都实现了这个接口,这意味着您可以在cureAnimal方法中调用Description属性。

票数 0
EN

Stack Overflow用户

发布于 2014-12-27 18:18:37

使用https://stackoverflow.com/questions/210460/try-to-describe-polymorphism-as-easy-as-you-can

代码语言:javascript
复制
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类中定义AnimalClinic逻辑,则可能需要执行某种类型的条件执行。

这种有条件的执行不必像大型if语句甚至switch那样笨重。您可以在这里研究if语句的备选解决方案。事实上,乔尔·科霍恩提供了一个。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27670036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档