接口工具中使用的技巧? [英] Skill used in interface implement?

查看:81
本文介绍了接口工具中使用的技巧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码

I have code

namespace InheritanceExample
{
    class Program
    {
        static void Main(string[] args)
        {

            var animals = new IAnimal[] { new Monkey(), new Dog() };
            foreach (var animal in animals)
            {
                DisplayAnimalData(animal);
            }
            Console.ReadLine();
        }

        private static void DisplayAnimalData(IAnimal animal)
        {
            Console.WriteLine("Animal has {0} legs and makes this sound", animal.NumberOfLegs);
            animal.Vocalize();
            ISpanishAnimal spanishAnimal = (ISpanishAnimal)animal;

           if(animal == spanishAnimal)
               Console.WriteLine("The same");

            Console.WriteLine("Spanish Animal has {0} legs and makes this sound", spanishAnimal.NumberOfLegs);
            spanishAnimal.Vocalize();

        }
    }

    public interface IAnimal
    {
        int NumberOfLegs { get; }
        void Vocalize();
    }
    public interface ISpanishAnimal
    {
        int NumberOfLegs { get; }
        void Vocalize();
    }

    public abstract class baseAnimal : IAnimal, ISpanishAnimal
    {
        int _numberOfLegs;
        private readonly string _sound;
        public baseAnimal(int numberOfLegs, string sound)
        {
            _numberOfLegs = numberOfLegs;

            _sound = sound;
        }

        void ISpanishAnimal.Vocalize()
        {
            Console.WriteLine(_sound + "in Spanish");
        }

        int ISpanishAnimal.NumberOfLegs
        {
            get
            {
                return _numberOfLegs * 5;
            }          
        }
        void IAnimal.Vocalize()
        {
            Console.WriteLine(_sound);
        }

        int IAnimal.NumberOfLegs
        {
            get
            {
                return _numberOfLegs;
            }
        }


        public string Sound
        {
            get { return _sound; }       
        }

    }
    public class Monkey : baseAnimal
    {
        public Monkey() : base(2,"Monkey Sound!") { }
    }
    public class Dog : baseAnimal
    {
        public Dog() : base(4, "Woof!") { }
    }

}



请查看代码:


Please look at the code:

var animals = new IAnimal[] { new Monkey(), new Dog() };



接口IAnimal没有Monkey和Dog方法。



那么这里使用了哪种技能?向下或向上或隐式实现?


The interface IAnimal doesn't have the methods Monkey and Dog.

So what kind of skills are used here? Downcast or upcast or implicit implement?

推荐答案

Monkey() Dog() 不是它们是实现接口IAnimal的类的方法。



某处你需要像
Monkey() and Dog() are not methods they are classes that implement the interface IAnimal.

Somewhere you will need something like
public class Monkey : IAnimal
{
    public int getLegs()
    {
        return 2;
    }
}
public class Dog : IAnimal
{
    public int getLegs()
    {
        return 4;
    }
}



有关详细信息,请参阅此CP文章 C#中的接口(适用于初学者) [ ^ ]



您没有从Interface继承,您正在实现接口。



另一篇可能有用的CP文章抽象类与接口 [ ^ ]





OP问


See this CP article for more information Interfaces in C# (For Beginners)[^]

You are not inheriting from the Interface, you are implementing the interface.

Another CP article that might be useful Abstract Class versus Interface[^]


OP asked

引用:

是的,我知道。我的意思是新的IAnimal []是一种数组?我们可以这样做吗?

Yes, I know that. I meant new IAnimal[] is kind of array? Can we do that?





是的,你可以。数组中的每个项都必须是实现IAnimal的类的对象。



请注意,当您使用数组中的元素时,您可能需要将它们强制转换为原始类(或基类),然后才能访问它们的所有属性或方法(每个类的唯一属性)



另请注意,您可以'使用(新)声明样式



Yes you can. Every item in the array must be an object of a class that implements IAnimal.

Note that when you use the elements in the array you may need to cast them to their original class (or a base class) before you can access all of their properties or methods (the one's that are unique to each class)

Also note that you can't use the (new) style of declaration

var animals = new[] { new Monkey(), new Dog() };

因为你会得到一个错误

引用:

找不到隐式类型数组的最佳类型

No best type found for implicitly-typed array

换句话说,Visual Studio需要一些额外的帮助才能弄清楚其中的元素是什么数组将是:)

In other words, Visual Studio needs a little extra help to work out what the elements in the array will be :)


这篇关于接口工具中使用的技巧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆