你能给我一个近乎过于简单的理解抽象类与继承使用的方式,并帮助我真正理解这个概念以及如何实现吗?我正在尝试完成一个项目,但不知道如何实现。我一直在与我的教授聊天,但被告诉说如果我无法理解,我可能还没有准备好这门课程。我已经学习了先修课程,并且仍然有困难理解这些概念。
为了澄清,我目前为止完成的项目如下。我还没有填写狗/猫类等信息。你能给我一点建议吗?我并不是要求别人给我“答案”。我只是不知道该怎么做。我正在上网课,他对我的沟通非常困扰。我在所有其他课程中都以4.0结尾,所以我愿意付出努力,但我对这些概念的理解和如何实际应用它们感到困惑。
任何可以让我在这个项目中进一步进展的评论或帮助?
我要实现的描述如下:
概述:
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:
我的名字是斑点,我是一只狗。
我的名字叫菲利克斯,我是一只猫。
要求:
1) You must have an abstract base class called Animal from which the Dog and Cat classes derive.
2) The Animal base class must derive from the Interface IAnimal , it is the only class that should derive from IAnimal.
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
base class should be where the name is stored and where the WhatIsMyName get-property is implemented.
4) You will need to create a Dog and a Cat class that will derive only from the Animal base class.
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);
}
}
}
Sorry, there is no text provided to translate. Please provide a text to be translated.
我到目前为止编写的代码:
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);
}
}
}