C#:在继承的子类中初始化基类字段,并访问基类字段,方法
2021-05-29 18:04
标签:main string cti reading 子类 int code 技术 public 一.基类 二.继承类(1) 三.继承类(2) 四.调用 五.打印结果 C#:在继承的子类中初始化基类字段,并访问基类字段,方法 标签:main string cti reading 子类 int code 技术 public 原文地址:https://www.cnblogs.com/QingYiShouJiuRen/p/11079277.htmlusing 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);
}
}
}
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);
}
}
}
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();
}
}
}
上一篇:Windows server 2008 R2 配置AD域控服务并为用户设置统一桌面
下一篇:使用ASP.NET Web API和Web API Client Gen使Angular 2应用程序的开发更加高效
文章标题:C#:在继承的子类中初始化基类字段,并访问基类字段,方法
文章链接:http://soscw.com/essay/89225.html