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


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部