ylbtech-.NET Framework: 类的继承关系,多态的体现,我的觉得题目还是有点欠缺 |
类的继承关系,多态的体现,我的觉得题目还是有点欠缺。
using System;
namespace ConsoleApplication1
{
class People
{
int age = 8;
public int Age {
get { return age; }
}
}
/// <summary>
/// Student 类里写的有点多余
/// </summary>
class Student : People
{
int age = 9;
public int Age
{
get { return age; }
}
}
class Program
{
static void Main(string[] args)
{
//父类引用子类对象(多态)
People p = new Student();
Console.WriteLine(string.Format("age={0}", p.Age));
Student s = new Student();
Console.WriteLine(string.Format("age={0}",s.Age));
}
}
}