C# 需要指导,了解head first C接口

C# 需要指导,了解head first C接口,c#,C#,我想要一些理解界面基础知识的帮助。有人能告诉我我下面的评论是否准确,并进一步解释吗 static void Main(string[] args) { //Creating a new ScaryScary object. The value of FunnyFunny's funnyThingIHave is now "big //shoes". The value of ScaryScary's numberOfScaryTh

我想要一些理解界面基础知识的帮助。有人能告诉我我下面的评论是否准确,并进一步解释吗

static void Main(string[] args)
        {
            //Creating a new ScaryScary object. The value of FunnyFunny's funnyThingIHave is now "big 
            //shoes". The value of ScaryScary's numberOfScaryThings is 14.
            ScaryScary fingersTheClown = new ScaryScary("big shoes", 14);

            //Creating a new reference called someFunnyClown and pointing it to the ScaryScary 
            //object? We still have the fingersTheClown object's values in place. How can we still 
            //use ScaryScary object's values if we are upcasting to FunnyFunny?
            FunnyFunny someFunnyClown = fingersTheClown;

            //Downcasting back to ScaryScary object (same as creating a new ScaryScary object)? 
            //We can use IScaryClown interface reference because ScaryScary implements IScaryClown. 
            //Why we first made a ScaryScary object, then cast it to FunnyFunny object, and then cast 
            //back to ScaryScary object?
            IScaryClown someOtherScaryClown = someFunnyClown as ScaryScary;

            //Is this the FunnyFunny's Honk() method? Does someOtherScaryClown now have both 
            //ScaryScary properties and methods and FunnyFunny's properties and methods?
            someOtherScaryClown.Honk();

            //Now calling the someOtherScaryClown(which is both ScaryScary and FunnyFunny object at 
            //the same time?) ScaryThingIHave method.
            Console.WriteLine(someOtherScaryClown.ScaryThingIHave);

            //Calling the someOtherScaryClown ScareLittleChildren() method.
            //My question is why we need the second and third steps. Couldn't we get the same thing 
            //done with just the ScaryScary fingersTheClown = new ScaryScary("big shoes", 14) line?
            someOtherScaryClown.ScareLittleChildren();                      
            Console.ReadKey();
        }

class FunnyFunny : IClown
    {
        public FunnyFunny(string funnyThingIHave)
        {
            this.funnyThingIHave = funnyThingIHave;
        }
        protected string funnyThingIHave;
        public string FunnyThingIHave
        {
            get { return "Hi kids! I have " + funnyThingIHave; }
        }
        public void Honk()
        {
            Console.WriteLine(this.FunnyThingIHave);
        }
    }

class ScaryScary : FunnyFunny, IScaryClown
    {
        public ScaryScary(string funnyThingIHave,
                          int numberOfScaryThings)
            : base(funnyThingIHave)
        {
            this.numberOfScaryThings = numberOfScaryThings;
        }
        private int numberOfScaryThings;
        public string ScaryThingIHave
        {
            get { return "I have " + numberOfScaryThings + " spiders"; }
        }
        public void ScareLittleChildren()
        {
            Console.WriteLine("You can’t have my "
                             + base.funnyThingIHave);
        }
    }

interface IClown
    {
        string FunnyThingIHave { get; }
        void Honk();
    }

interface IScaryClown : IClown
    {
        string ScaryThingIHave { get; }
        void ScareLittleChildren();
    }

您的代码和实现看起来很好,您可以插入断点并在调试模式下运行,并在VisualStudio中使用步骤导航,这将帮助您理解流程

以下是我为更好地理解接口而保留的注释。希望这有助于澄清问题

什么 接口基本上是所有实现接口的类都应该遵循的契约。它们看起来像一个类,但没有实现

在C语言中,按约定的接口名称是通过在“I”前面加前缀来定义的,所以如果您想要一个名为shapes的接口,您可以将其声明为IShapes

现在为什么? 提高代码的可重用性 假设你想画圆,三角形。 您可以将它们组合在一起,并将它们称为形状,还可以使用方法绘制圆和三角形 但有具体的实现将是一个坏主意,因为明天你可能会决定有两个以上的形状矩形和正方形。现在,当您添加它们时,很有可能会破坏代码的其他部分

通过接口,您可以将不同的实现与合同隔离开来

现场场景第1天

您被要求创建一个应用程序来绘制圆和三角形

interface IShapes
{
   void DrawShape();
   
 }

class Circle : IShapes
{
    
    public void DrawShape()
    {
        Console.WriteLine("Implementation to Draw a Circle");
    }
}

Class Triangle: IShapes
{
     public void DrawShape()
    {
        Console.WriteLine("Implementation to draw a Triangle");
    }
}
现场场景第2天


如果你被要求向它添加正方形和矩形,你所要做的就是在Square类中为它创建实现:IShapes和主添加到列表形状

感谢您对接口的精彩概述和示例。还指出使用调试是有帮助的!你很高兴它帮助了你,如果你能点击绿色的勾选框将答案标记为已接受,那就太好了,这样它就可以作为其他人的参考。干杯
static void Main()
{
     List <IShapes> shapes = new List<IShapes>();
        shapes.Add(new Circle());
        shapes.Add(new Triangle());

        foreach(var shape in shapes)
        {
            shape.DrawShape();
        }
}