C#:在继承的子类中初始化基类字段,并访问基类字段,方法

Posted qingyishoujiuren

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#:在继承的子类中初始化基类字段,并访问基类字段,方法相关的知识,希望对你有一定的参考价值。

一.基类

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();
        
    

五.打印结果

技术图片

 

以上是关于C#:在继承的子类中初始化基类字段,并访问基类字段,方法的主要内容,如果未能解决你的问题,请参考以下文章

C# 包含继承类的基类数组,访问非继承字段

从 Java 中的基类访问子类字段

c#基础 继承和接口,扩展类型

课件中动手动脑4

面向对象3大特性之一:继承

急急急!!!!C# 中关于子类访问基类成员的可访问性