标签:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 面向对象之继承 { class Program { static void Main(string[] args) { //记者:我是记者,我的爱好是偷拍,我的年龄34,我是一个男的 //程序员:我叫孙权 我的年龄23 我是男生 我的工作年限是3年 Reporter rep = new Reporter("狗仔", 34, ‘男‘, "偷拍"); rep.ReporterSayhello(); Programmer pro = new Programmer("孙权",23,‘男‘,3); pro.ProgrammerSayhello(); Console.ReadKey(); } public class person { private string _name; public string Name { get { return _name; } set { _name = value; } } private int _age; public int Age { get { return _age; } set { _age = value; } } private char _gender; public char Gender { get { return _gender; } set { _gender = value; } } public person(string name, int age, char gender) // 声明一个父类的构造函数 { this.Name = name; this.Age = age; this.Gender = gender; } } public class Reporter : person { private string _hobby; public string Hobby { get { return _hobby; } set { _hobby = value; } } public Reporter(string name, int age, char gender, string hobby) : base(name, age, gender)//声明继承父类。。 { this.Hobby = hobby; } public void ReporterSayhello() { Console.WriteLine("我叫{0},我是一名记者,我的爱好是{1},我是{2}生,我今年{3}岁了", this.Name, this.Hobby, this.Gender, this.Age); } } public class Programmer : person { private int _WorkYear; public int WorkYear { get { return _WorkYear; } set { _WorkYear = value; } } public Programmer(string name, int age, char gender, int workyear) : base(name, age, gender) { this.WorkYear = workyear; } public void ProgrammerSayhello() { Console.WriteLine("我叫{0},我是一名程序猿,我是{1}生,我今年{2}岁了,我的工作年限是{3}年", this.Name, this.Gender, this.Age, this.WorkYear); } } } }
标签:
原文地址:http://www.cnblogs.com/kangshuai/p/4693333.html