C #: initialize the base class field in the subclass inherits of, and access to the base class field, method

A. Base class

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

II. Inherited class (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;        // average score
      

        public Student(int averageScore, string name, int age, int height, string gender, string worker)
            : base(name, age, height, gender, worker)
        {
            _averageScore = averageScore;
          
        }

        // new new base class to hide getPersonInfo () 
        public  new new  void   getPersonInfo ()
        {
            base.getPersonInfo();        
            Console.WriteLine ( " Average score: min {0} " , _averageScore);

        }
    }
}

III. Inherited class (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;   // monthly

        public Worker(decimal monthlySalary, string name, int age, int height, string gender, string worker)
            : base(name, age, height, gender, worker)
        {
            _monthlySalary = monthlySalary;
        }

        // new new getPersonInfo order to hide the base class () 
        public  new new  void getPersonInfo () {   
             Base .getPersonInfo ();
            Console.WriteLine ( " Salary: element {0} " , _monthlySalary);
        }
    }
}

IV. Calls

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)
        {
            S Student = new new Student ( 650 , " Bob " , 23 , 175 , " M " , " student " );
            s.getPersonInfo();
            Console.WriteLine("/************************************************/");
            W Worker = new new Worker ( 8000 , " red " , 24 , 165 , " female " , " white collar " );
            w.getPersonInfo ();
            Console.ReadKey();
        }
    }
}

V. print results

 

Guess you like

Origin www.cnblogs.com/QingYiShouJiuRen/p/11079277.html