参考书目:C#6.0学习笔记——从第一行C#代码到第一个项目设计(作者周家安)P73
派生类示例。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Example3_7
{
class Program
{
static void Main(string[] args)
{
//创建student的实例
Student stu = new Student();
stu.Name = "李梅";
stu.Age = 21;
stu.Address = "北京海淀";
stu.Course = "计算机编程";
//输出
Console.WriteLine("学员信息:\n姓名:{0}\n住址:{1}\n年龄:{2}\n课程:{3}",
stu.Name, stu.Address, stu.Age, stu.Course);
Console.ReadKey();
}
}
/// <summary>
/// 基类:Person
/// </summary>
public class Person
{
public string Name { set; get; } //姓名
public string Address { set; get; } //地址
public int Age { set; get; } //年龄
public Person() //构造函数
{
Console.WriteLine("基类Person类的构造函数被调用.");
}
}
/// <summary>
/// 派生类
/// </summary>
public class Student:Person
{
public string Course { get; set; } //课程
public Student()
{
Console.WriteLine("派生的Student类的构造函数被调用.");
}
}
}
运行结果如下: