我是新手,今天碰见了个题,求大神帮忙
定义一个类Animal,定义字段name、age、sexual,分别代表名字和年龄,类型分别为string、intbool,定义属性Name和Age分别读取和设置name和age的值再定义一个方法Eat()
using System; using System.Collections.Generic; using System.Linq; using System.Text; // 引用命名空间 namespace ConsoleOne { class Program { #region 主函数 static void Main(string[] args) { Animal mammal = new Animal(); mammal.Name = "犬类"; mammal.Age = 5; Console.WriteLine("{0}的年龄是{1}。", mammal.Name, mammal.Age); Dog dog = new Dog(); dog.Name = "赛虎"; dog.Age = 3; Console.WriteLine("{0}......{1}的年龄是{2}。",dog.Hello(), dog.Name, dog.Age); Console.ReadKey(); } #endregion #region Animal类 class Animal { private string name; private int age; private bool sexual; public string Name { set { this.name = value; // value代表用户赋值过来的值 } get { return this.name; } } public int Age { set { this.age = value; } get { return this.age; } } public void Eating() { Console.WriteLine("Having a meal."); } }; #endregion #region Dog类 从Animal类继承 class Dog: Animal { public string Hello() { return("Wang! Wang! Wang!"); } }; #endregion } }