用c#定义一个类Animal,分别定义属性Name,Age和一个抽象方法Eating(),定义一个子类Human继承Animal,Human的属性有Number和Address和方法Runing(),并且通过Human的构造方法把参数传递给各个属性。在程序Main函数中定义一个Human的对象Jack,分别传入一下参数:(马云,55,440196196501230365,浙江省杭州市),并且通过Jack分别执行Eating()和Runing()方法
时间: 2024-02-13 22:04:39 浏览: 151
好的,以下是用C#定义Animal类和Human子类的代码:
```csharp
public abstract class Animal
{
public string Name { get; set; }
public int Age { get; set; }
public abstract void Eating();
}
public class Human : Animal
{
public string Number { get; set; }
public string Address { get; set; }
public Human(string name, int age, string number, string address)
{
Name = name;
Age = age;
Number = number;
Address = address;
}
public override void Eating()
{
Console.WriteLine("I'm eating with a knife and fork.");
}
public void Running()
{
Console.WriteLine("I'm running on the track.");
}
}
```
在程序Main函数中创建一个Human对象Jack,并调用其Eating()和Running()方法:
```csharp
static void Main(string[] args)
{
Human Jack = new Human("马云", 55, "440196196501230365", "浙江省杭州市");
Jack.Eating(); // 输出:I'm eating with a knife and fork.
Jack.Running(); // 输出:I'm running on the track.
}
```
希望这个例子能够帮助你理解如何定义类和继承类,以及如何创建对象和调用方法。
阅读全文