using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 继承
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
Chinese c1 = new Chinese();
c1.Height = 180;
c1.Name ="李小龙";
c1.Age = 20;
c1.Hukou="北京";
c1.Sayhello();
c1.Kongfu();
Kearon k1 = new Kearon();
k1.Height = 178;
k1.Name = "金三顺";
k1.Food="泡菜";
k1.Age = 18;
k1.Sayhello();
k1.Eating();
p1 = c1; //我要一个人,给我一个中国人,也行。
p1.Sayhello();
//p1.Kongfu() 这个不能访问
p1 = k1;
p1.Sayhello();
p1 = c1;
// Chinese c2 = new Person();
// Chinese c2 = p1; //我要一个中国人,给了我一个人,万一他不是中国人谁负责?
Chinese c2 = (Chinese)p1;//我要一个中国人,给了我一个人,程序员保证指向的肯定是中国人。
c2.Sayhello(); //你好,我是李小龙
//c2 = (Chinese)k1; //程序员的保证不靠谱 照样报错
Object obj = 3; //Object类是所有类的基类,没有指定父类,父类就是Object;
Console.ReadKey();
}
}
class Person //不写继承默认是继承自Object类
{
public int Height { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public void Sayhello()
{
Console.WriteLine("你好,我是{0}", Name);
}
}
class Chinese : Person
{
public void Kongfu()
{
Console.WriteLine("我打!");
}
public string Hukou { get; set; }
}
class Kearon : Person
{
public void Eating()
{
Console.WriteLine("好吃!");
}
public string Food { get; set; }
}
}
C#学习笔记(十五) --继承
最新推荐文章于 2024-01-11 11:31:27 发布