第二单元练习 第一题和第十一题
Posted 猪不壮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第二单元练习 第一题和第十一题相关的知识,希望对你有一定的参考价值。
第一题
animal类
class Animal
{
private bool m_sex;
private int m_age;
public bool Sex
{
get { return m_sex; }
set { m_sex = value; }
}
public int Age
{
get { return m_age; }
set { m_age = value; }
}
public Animal()
{
Sex = false;
}
public virtual void Introduce()
{
if (Sex == true)
Console.WriteLine("This is a male Animal!");
else
Console.WriteLine("This is a female Animal!");
}
}
class Dog : Animal
{
public Dog()
{
Sex = true;
}
public override void Introduce()
{
if (Sex == true)
Console.WriteLine("This is a male Dog!");
else
Console.WriteLine("This is a female Dog!");
}
}
class Cat : Animal
{
public override void Introduce()
{
if (Sex == true)
Console.WriteLine("This is a male Cat!");
else
Console.WriteLine("This is a female Cat!");
}
}
主函数
Animal ani = new Animal();
ani.Introduce();
Dog dog = new Dog();
dog.Introduce();
Cat cat = new Cat();
cat.Introduce();
Console.WriteLine("按回车键结束");
Console.ReadKey();
第十一题
animal类
class Animal
{
private bool m_sex;
private string m_sound;
public bool Sex
{
get { return m_sex; }
set { m_sex = value; }
}
public string Sound
{
get { return m_sound; }
set { m_sound = value; }
}
public Animal()
{
m_sex = false;
m_sound = "Howl";
}
public virtual string Roar()
{
return Sound;
}
}
class Dog : Animal
{
public Dog()
{
Sex = true;
Sound = "Wow...";
}
public override string Roar()
{
return "Dog:" + Sound;
}
}
class Cat : Animal
{
public Cat()
{
Sound = "Miaow...";
}
public override string Roar()
{
return "Cat:" + Sound;
}
}
class Cow : Animal
{
public Cow()
{
Sound = "Moo...";
}
public override string Roar()
{
return "Cow:" + Sound;
}
}
主函数
Animal animal;
Dog dog = new Dog();
animal = dog;
Console.WriteLine(animal.Roar());
Cat cat = new Cat();
animal = cat;
Console.WriteLine(animal.Roar());
Cow cow = new Cow();
animal = cow;
Console.WriteLine(animal.Roar());
Console.WriteLine("按回车键结束");
Console.ReadKey();
以上是关于第二单元练习 第一题和第十一题的主要内容,如果未能解决你的问题,请参考以下文章
全国计算机等级考试二级Python(2021年9月)备考笔记 第十二天
全国计算机等级考试二级Python(2021年9月)备考笔记 第十四天