有朋友问,却对C#半生不熟,怕写错了对不起他,帖出来大家一起解决。谢谢(一)定义基类Animal:为项目添加公有类Animal,何存在文件Animal.cs中。
(二)为基类Animal添加以下列字段、属性、方法:
1。私有字段m_sex,代表性别,数据类型为bool;
2。私有字段m_age,代表年龄,数据类型为int;
3。公有属性Sex,用于获取和设置m_sex字段,包含get和set访问器。在类的构造函数中设置初始值为false.
4。公有属性Age,用于获取和设置m_age字段,包含get和set访问器。
虚拟方法Introduce(),用于介绍动物:
若属性Sex的值为ture,方法返回一个字符串“this is a male Animal!”.
若属性Sex的值为flase,方法返回一个字符串“this is a female Animal!”.(三)定义派生类Dog和Cat,在其中重写基类的构造函数和虚拟方法:
1。从基类Animal中派生公有类Dog和Cat,仍然保存在文件Animal.cs中;
2。在类Dog的构造函数中,设置属性Sex的初值值为true;
3。在类Dog中重写基类Animal的虚拟方法Introduce:
若属性Sex的值为true,方法返回一个字符串:“this is a male Dog!”
若属性Sex的值为false,方法返回一个字符串:“this is a famale Dog!”4。在类Cat中重写基类Animal的虚拟方法Introduce:
若属性Sex的值为true,方法返回一个字符串:“this is a male Cat!”
若属性Sex的值为false,方法返回一个字符串:“this is a famale Cat!”(四)在程序主方法中实例化类的对象,调用方法输入介绍动物的字符串:
1。实例化Animal的一个对象ani,调用类的方法Introduce(),并输出方法返回的字符串;
2。实例化Dog的一个对象dog,调用类的方法Introduce(),并输出方法返回的字符串;
3。实例化Cat的一个对象cat,调用类的方法Introduce(),并输出方法返回的字符串;
(二)为基类Animal添加以下列字段、属性、方法:
1。私有字段m_sex,代表性别,数据类型为bool;
2。私有字段m_age,代表年龄,数据类型为int;
3。公有属性Sex,用于获取和设置m_sex字段,包含get和set访问器。在类的构造函数中设置初始值为false.
4。公有属性Age,用于获取和设置m_age字段,包含get和set访问器。
虚拟方法Introduce(),用于介绍动物:
若属性Sex的值为ture,方法返回一个字符串“this is a male Animal!”.
若属性Sex的值为flase,方法返回一个字符串“this is a female Animal!”.(三)定义派生类Dog和Cat,在其中重写基类的构造函数和虚拟方法:
1。从基类Animal中派生公有类Dog和Cat,仍然保存在文件Animal.cs中;
2。在类Dog的构造函数中,设置属性Sex的初值值为true;
3。在类Dog中重写基类Animal的虚拟方法Introduce:
若属性Sex的值为true,方法返回一个字符串:“this is a male Dog!”
若属性Sex的值为false,方法返回一个字符串:“this is a famale Dog!”4。在类Cat中重写基类Animal的虚拟方法Introduce:
若属性Sex的值为true,方法返回一个字符串:“this is a male Cat!”
若属性Sex的值为false,方法返回一个字符串:“this is a famale Cat!”(四)在程序主方法中实例化类的对象,调用方法输入介绍动物的字符串:
1。实例化Animal的一个对象ani,调用类的方法Introduce(),并输出方法返回的字符串;
2。实例化Dog的一个对象dog,调用类的方法Introduce(),并输出方法返回的字符串;
3。实例化Cat的一个对象cat,调用类的方法Introduce(),并输出方法返回的字符串;
namespace AnimalTest
{
public class Animal
{
private bool m_sex;
private int m_age;
public bool Sex
{
get{return this.m_sex;}
set{this.m_sex=value;}
}
public int Age
{
get{return this.m_age;}
set{this.m_age=value;}
}
public Animal()
{
this.m_age=false;
}
public virtual string Introduce()
{
return this.m_sex?"this is a male Animal!":"this is a female Animal!";
}
} public class Dog : Animal
{
public Dog()
{
this.m_sex=true;
}
public override string Introduce()
{
return this.m_sex?"this is a male Dog!":"this is a female Dog!";
}
} public class Cat : Animal
{
public override string Introduce()
{
return this.m_sex?"this is a male Cat!":"this is a female Cat!";
}
} public class Program
{
public static void Main(string[] args)
{
Animal ani = new Animal();
Dog dog =new Dog();
Cat cat = new Cat();
Console.WriteLine(ani.Introduce());
Console.WriteLine(dog.Introduce());
Console.WriteLine(cat.Introduce());
}
}
}
{
private bool m_sex;
private int m_age;
public Animal()
{
//不设置也是false 按题意写 Sex = false;也可
m_sex = false;
} public bool Sex
{
get { return m_sex; }
set { m_sex = value; }
}
public int Age
{
get { return m_age; }
set { m_age = value; }
} public virtual string Introduce()
{
string str = Sex == true ? "male" : "female";
return "this is a " + str + " Animal!";
}
}public class Dog:Animal
{
public Dog()
{
this.Sex = true;
} public override string Introduce()
{
string str = Sex == true ? "male" : "female";
return "this is a " + str + " Dog!";
}
}public class Cat : Animal
{
public Cat()
{
} public override string Introduce()
{
string str = Sex == true ? "male" : "female";
return "this is a " + str + " Cat!";
}
}
Program.cs
using System;
public class Program
{
static void Main(string[] args)
{
Animal ani = new Animal();
Console.WriteLine(ani.Introduce());
Dog dog = new Dog();
Console.WriteLine(dog.Introduce());
Cat cat = new Cat();
Console.WriteLine(cat.Introduce());
}
}