ylbtech-.NET Framework: 类的继承关系,多态的体现,我的觉得题目还是有点欠缺 |
类的继承关系,多态的体现,我的觉得题目还是有点欠缺。
using System;namespace ConsoleApplication1{ class People { int age = 8; public int Age { get { return age; } } } /// /// Student 类里写的有点多余 /// 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)); } }}
| 作者:出处:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |