使用C#定义一个类Animal,分别定义属性Name,Age和一个抽象方法Eating(),定义一个子类Human继承Animal,Human的属性有Number和Address和方法Runing(),并且通过Human的构造方法把参数传递给各个属性。在程序Main函数中定义一个Human的对象Jack,分别传入一下参数:(马云,55,440196196501230365,浙江省杭州市),并且通过Jack分别执行Eating()和Runing()方法
时间: 2024-02-12 21:07:03 浏览: 65
c#属性和方法
好的,以下是C#代码实现:
```csharp
using System;
// 定义Animal类
public abstract class Animal
{
public string Name { get; set; }
public int Age { get; set; }
// 定义抽象方法Eating()
public abstract void Eating();
}
// 定义Human类,继承Animal类
public class Human : Animal
{
public string Number { get; set; }
public string Address { get; set; }
public Human(string name, int age, string number, string address)
{
this.Name = name;
this.Age = age;
this.Number = number;
this.Address = address;
}
// 定义方法Runing()
public void Runing()
{
Console.WriteLine("Human is running...");
}
// 实现抽象方法Eating()
public override void Eating()
{
Console.WriteLine("Human is eating...");
}
}
class Program
{
static void Main(string[] args)
{
// 创建Human对象Jack,传入参数
Human Jack = new Human("马云", 55, "440196196501230365", "浙江省杭州市");
// 调用Eating()方法
Jack.Eating();
// 调用Runing()方法
Jack.Runing();
}
}
```
输出结果:
```
Human is eating...
Human is running...
```
阅读全文