test2-1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test2_11
{
class Animal//基类
{
private bool m_sex;
private int m_age;
public Animal()
{
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 void Introduce()//虚方法
{
if (Sex == true)
Console.WriteLine("this is a male Animal");
if (Sex == false)
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");
if (Sex == false)
Console.WriteLine("this is a female Dog");
}

}
class Cat:Animal
{
public override void Introduce()
{
if (Sex == true)
Console.WriteLine("this is a male Cat");
if (Sex == false)
Console.WriteLine("this is a female Cat");
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test2_11
{
class Program
{
static void Main(string[] args)
{
Animal ani = new Animal();
Dog dog = new Dog();
Cat cat = new Cat();
ani.Introduce();
dog.Introduce();
cat.Introduce();

}
}
}

原文地址:https://www.cnblogs.com/YuJiaJia/p/7506953.html