一.基类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Inherit { public class Person { private string _name; private int _age; private int _height; private string _gender; private string _worker; //职业 public Person(string name, int age, int height, string gender, string worker) { _name = name; _age = age; _height = height; _gender = gender; _worker = worker; } public void getPersonInfo() { Console.WriteLine("姓名:{0}", _name); Console.WriteLine("年龄:{0}", _age); Console.WriteLine("身高:{0}", _height); Console.WriteLine("性别:{0}", _gender); Console.WriteLine("职业:{0}", _worker); } } }
二.继承类(1)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Inherit { public class Student:Person { private int _averageScore; //平均分数 public Student(int averageScore, string name, int age, int height, string gender, string worker) : base(name, age, height, gender, worker) { _averageScore = averageScore; } //new为了隐藏基类中的getPersonInfo() public new void getPersonInfo() { base.getPersonInfo(); Console.WriteLine("平均分数:{0}分", _averageScore); } } }
三.继承类(2)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Inherit { class Worker:Person { private decimal _monthlySalary; //月薪 public Worker(decimal monthlySalary, string name, int age, int height, string gender, string worker) : base(name, age, height, gender, worker) { _monthlySalary = monthlySalary; } //new为了隐藏基类中的getPersonInfo() public new void getPersonInfo(){ base.getPersonInfo(); Console.WriteLine("月薪:{0}元", _monthlySalary); } } }
四.调用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Inherit { class Program { static void Main(string[] args) { Student s = new Student(650, "小明", 23, 175, "男", "学生"); s.getPersonInfo(); Console.WriteLine("/************************************************/"); Worker w = new Worker(8000, "小红", 24, 165, "女", "白领"); w.getPersonInfo(); Console.ReadKey(); } } }
五.打印结果
原文地址:https://www.cnblogs.com/QingYiShouJiuRen/p/11079277.html
时间: 2024-11-06 13:09:48