你能帮我在一个实际例子中理解抽象类与接口的用法吗?

你能帮我在一个实际例子中理解抽象类与接口的用法吗?

问题描述:

您能否让我对抽象类与继承的使用有一个几乎过于简单化的理解,并帮助我真正理解这个概念以及如何实现?我有一个要完成的项目,但不知道如何实施.我一直在和我的教授聊天,并被告知很多,说如果我无法弄清楚,我可能还没有准备好参加这门课程.我已经涵盖了 prerequestite 课程,但仍然无法理解这些概念.

Can you give me an almost overly simplistic understanding of abstract class vs inheritance use and help me so I can truly understand the concept and how to implement? I have a project I'm trying to complete, and am lost on how to implement. I've been chatting with my professor and been told off pretty much, saying that if I can't figure it out, I'm probably not ready for the course. I have OVERCOVERED the prerequestite courses, and still have trouble understanding these concepts.

澄清一下,我到目前为止所做的项目如下.我还没有填写狗/猫课程等.能不能给个指点.我不是要求任何人给我答案".我只是不知道该去哪里.我参加在线课程,他与我的沟通努力令人不安.我刚刚完成了所有其他课程的 4.0,所以我愿意付出努力,但我迷失在对这些概念的理解以及如何实际应用它们的过程中.

To clarify, the project as I've done so far is below. I don't have the dog/cat classes etc filled out yet. Can you give me a pointer. I'm not asking for anyone to give me the "answers." I just am lost on where to go with this. I take online courses and his communication efforts with me have been troubling. I just finished with 4.0 with all my other courses, so I'm willing to put the effort in, but I'm lost in the comprehension of these concepts and how to PRACTICALLY apply them.

有什么意见或帮助可以让我在这个项目中取得进一步进展?

Any comments or help that will let me progress further in this project?

我要实现的描述如下:

概述:

这个练习的目的是为了演示接口的使用,继承、抽象类和多态性.你的任务是采取提供的程序外壳并添加适当的类和相应的类成员/方法来获得这个程序才能正常运行.你可以不要更改任何代码提供,您只能添加类你写.虽然有获取程序的多种方式工作,你必须使用技术演示接口的使用,
继承、抽象类和多态性.再次明确,您可以添加到提供的代码,但您不能更改或删除任何它.提供的代码将使用很少的附加代码并将满足要求练习.

The purpose of this exercise is to demonstrate the use of Interfaces, Inheritance, Abstract classes, and Polymorphism. Your task is to take the supplied program shell and ADD the appropriate classes and corresponding class members/methods to get this program to function correctly. You may not make changes to any of the code supplied, you may only add the classes you write. Although there are numerous ways to get the program working, you must use techniques that demonstrate the use of Interfaces,
Inheritance, Abstract classes, and Polymorphism. Again, to make clear, you can add to the supplied code but you cannot change or delete any of it. The code that is supplied will work with very little additional code and will satisfy the requirements of the exercise.

如果您成功完成分配,你的程序应该输出运行时的以下语句:

If you successfully complete the assignment, your program should output the following statements when run:

我叫 Spot,我是一只狗

My name is Spot, I am a Dog

我叫菲利克斯,我是一只猫

My name is Felix, I am a Cat

要求:

1) 你必须有一个抽象的基础名为动物"的类,其中派生狗和猫类.

1) You must have an abstract base class called 'Animal' from which the Dog and Cat classes derive.

2) Animal 基类必须派生从接口'IAnimal',它是唯一应该派生的类IAnimal.

2) The Animal base class must derive from the Interface 'IAnimal', it is the only class that should derive from IAnimal.

3) 因为所有的动物都有名字和名字name 不是一个属性特定于狗或猫,动物

3) Since all animals have a name and a name is not an attribute that is specific to a dog or a cat, the Animal

基类应该是名字的地方存储以及 WhatIsMyName 的位置get-property 已实现.

base class should be where the name is stored and where the WhatIsMyName get-property is implemented.

4) 你需要创建一个 Dog 和一个仅派生自的 Cat 类动物基类.

4) You will need to create a Dog and a Cat class that will derive only from the Animal base class.

5) Dog 和 Cat 类应该实施 WhatAmI get-property 和返回适当的字符串值.

5) The Dog and Cat classes should implement the WhatAmI get-property and return the appropriate string value.

您无法更改的代码:

using System;

namespace IT274_U2
{
    public interface IAnimal
    {
        string WhatAmI { get; }
        string WhatIsMyName { get; }
    }

    public class TesterClass
    {
        public static void DescribeAnimal(IAnimal animal)
        {
            Console.WriteLine("My name is {0}, I am a {1}", animal.WhatIsMyName, animal.WhatAmI);
        }

        static void Main(string[] args)
        {
            Dog mydog = new Dog("Spot");
            Cat mycat = new Cat("Felix");
            DescribeAnimal(mydog);
            DescribeAnimal(mycat);
        }
    }
}

///////////////////////

///////////////////////

到目前为止我写的代码:

Code I've written so far:

using System;


namespace IT274_U2
{
    public interface IAnimal
    {
        string WhatAmI { get; }
        string WhatIsMyName { get; }
    }


    public class Dog
    {
        public abstract string WhatAmI
        {
            get;
            set;
        }
    }//end public class Dog

    public class Cat
    {
    public abstract string WhatIsMyName  
    {
        get;
        set;
    }
    }//end public class Cat

    public abstract class Animal : IAnimal
    {
    // fields
    protected string Dog;
    protected string Cat;

                  // implement WhatIsMyName 

    //properties
    public abstract String Dog
    {
        get;  
        set;
    }
    public abstract String Cat
    {
        get;
        set;
    }
    public abstract string WhatIsMyName();

    } //end public abstract class Animal


    public class TesterClass
    {
        public static void DescribeAnimal(IAnimal animal)
        {
            Console.WriteLine("My name is {0}, I am a {1}", animal.WhatIsMyName, animal.WhatAmI);
        }

        static void Main(string[] args)
        {

            Dog mydog = new Dog("Spot");
            Cat mycat = new Cat("Felix");
            DescribeAnimal(mydog);
            DescribeAnimal(mycat);
        }
    }
}

我已经取出了每个类的代码体 - 如果您想看到我的答案,请查看编辑修订版 :)

I have taken the body of code for each class out - If you want to see my answer, have a look at the edit revisions :)

首先我们定义接口

public interface IAnimal
{
    string WhatAmI { get; }
    string WhatIsMyName { get; }
}

任何实现此接口的类都必须实现这些属性.一个接口就像一个合同;实现接口的类同意提供接口方法、属性事件或索引器的实现.

Any class that implements this interface must implement these properties. An interface is like a contract; a class implementing an interface agrees to provide an implementation of the interface's methods, properties events or indexers.

接下来,我们需要定义抽象的Animal类

Next, we need to define your abstract Animal class

public abstract class Animal : IAnimal
{
    //Removed for Training, See Edit for the code
}

类是抽象的事实表示该类仅用作其他类的基类.我们已经实现了接口的两个属性,还有一个私有字段来存储动物名称.此外,我们将 WhatAmI 属性访问器抽象化,以便我们可以在每个派生类中实现我们自己特定的属性访问器逻辑,并且还定义了一个构造函数,该构造函数接受字符串参数并将值分配给_name 私有字段.

The fact that the class is abstract indicates that the class is intended only to be a base class for other classes. We have implemented both properties of the interface and also have a private field to store the animal name. In addition, we have made the WhatAmI property accessor abstract so that we can implement our own specific property accessor logic in each derived class and have also defined a constructor that accepts a string argument and assigns the value to the _name private field.

现在,让我们定义我们的 CatDog

Now, let's define our Cat and Dog classes

public class Dog : Animal
{
    //Removed for Training, See Edit for the code
}

public class Cat : Animal
{
    //Removed for Training, See Edit for the code
}

两个类都继承自 Animal 并且每个类都有一个构造函数,该构造函数定义了一个字符串参数并将该参数作为参数传递给基本构造函数.此外,每个类都为 WhatAmI 实现了自己的属性访问器,分别返回了它们类型的字符串.

Both classes inherit from Animal and each has a constructor that defines a string argument and passes that argument as a parameter to the base constructor. In addition, each class implements it's own property accessor for WhatAmI, returning a string of their type, respectively.

对于其余的代码

public class Program
{
    public static void DescribeAnimal(IAnimal animal)
    {
        Console.WriteLine("My name is {0}, I am a {1}", animal.WhatIsMyName, animal.WhatAmI);
    }

    static void Main(string[] args)
    {
        Dog mydog = new Dog("Spot");
        Cat mycat = new Cat("Felix");
        DescribeAnimal(mydog);
        DescribeAnimal(mycat);
        Console.ReadKey();
    }
}

静态方法DescribeAnimal 接受一个IAnimal 作为参数并写出WhatIsMyNameWhatAmI返回的值用于传入 IAnimal 的 code> 属性访问器.

the static method DescribeAnimal accepts an IAnimal as an argument and writes out the values returned by the WhatIsMyName and WhatAmI property accessors for the passed in IAnimal.

由于Animal 实现了IAnimal 并且DogCat 都继承自Animal,任何CatDog 对象都可以作为参数传递给DescribeAnimal 方法.

Since Animal implements IAnimal and both Dog and Cat inherit from Animal, any Cat or Dog object can be passed as a parameter to the DescribeAnimal method.

我希望我已经解释清楚了,如果有人觉得我的措辞需要收紧,请发表评论,我很乐意编辑我的答案.

I hope that I have explained this clearly, If anyone feels my choice of words needs tightening up, please comment and I will be happy to edit my answer.