C#基础(14)——继承、new

1、继承父类

将重复的成员封装到一个类中,子类继承父类的公有字段、属性、方法
子类:派生类
父类:基类
子类没有继承父类的私有字段
这里写图片描述
这里写图片描述

public class Student:Person

子类有没有继承父类的构造函数?
1)当根性:子类只有一个父类
2)传递性:teacher是继承person,student继承teacher
当父类中定义构造函数时,报如下错误:
这里写图片描述
说明默认的构造函数被干掉了,继承时只有new一个对象的构造函数,才是真正创建了一个父类对象,父类成员才可以使用,要补一个无参的就可以。
1)父类重新写一个无参的构造函数;
2)在子类中显示的调用父类的有参构造函数,使用关键字:base(),这个常用;
这里写图片描述

这里写图片描述

说明子类并没有继承父类的构造函数。但是子类会调用父类无参的构造函数,要创建父类对象,才能使用父类的成员。

2、查看类图

这里写图片描述
这里写图片描述
Person:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{
    public class Person
    {
        //构造函数
        public Person(string name, char gender, int age)
        {
            this.Name = name;
            this.Gender = gender;
            this.Age = age;
        }
        //构造函数重载
        public Person(string name): this(name,'男',2)
        {
        }
        //构造函数重载
        public Person(string name, char gender): this(name, gender, 0)
        {
        }
        //无参的构造函数
        //public Person()
        //{

        //}

        //字段
        private string _name;
        public string Name //属性
        {
            get { return _name; }//当输出打印时
            set { _name = value; }//当给属性赋值时
        }

        //字段
        private int _age;
        public int Age //属性
        {
            get { return _age; }
            set
            {
                if (value < 0 || value > 120)
                {
                    value = 0;
                }
                _age = value;
            }
        }

        //字段
        private char _gender;
        public char Gender//属性
        {
            get
            {
                if (_gender != '男' && _gender != '女')//也可以在set里设置
                {
                    return _gender = '男';
                }
                return _gender;
            }
            set { _gender = value; }
        }


        //方法
        public void PrintInformation()
        {
            Console.WriteLine("我叫{0},今年{1}岁了,性别:{2}!", this.Name, this.Age, this.Gender);
        }


    }
}

Student:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{
    public class Student:Person
    {
        public Student(string name, char gender, int age, int chinese, int math, int english):base(name,gender,age)
        {
            this.Chinese = chinese;
            this.Math = math;
            this.English = english;
        }

        //语文
        private int _chinese;
        public int Chinese
        {
            get { return _chinese; }
            set { _chinese = value; }
        }
        //数学
        private int _math;
        public int Math
        {
            get { return _math; }
            set { _math = value; }
        }
        //英语
        private int _english;

        public int English
        {
            get { return _english; }
            set { _english = value; }
        }

        //方法
        public void PrintInfo()
        {
            Console.WriteLine("我叫{0},今年{1}岁,是一名{2}生!", this.Name, this.Age, this.Gender);
        }

        public void PrintScore()
        {
            Console.WriteLine("我的英语成绩:{0},语文成绩:{1},数学成绩{2},总分:{3},平均分:{4}",this.English,this.Chinese,this.Math,
                this.English+this.Chinese+this.Math,(this.English+this.Chinese+this.Math)*1.0/3);
        }
    }
}

Teacher:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{
    public class Teacher:Person
    {
        public Teacher(string name,char gender,int age,double salary):base(name,gender,age)
        {
            this.Salary = salary;
        }

        private double _salary;
        public double Salary
        {
            get { return _salary; }
            set { _salary = value; }
        }


        public void Sleep()
        {
            Console.WriteLine("老师功能:睡觉啦!");
        }
    }
}

3、继承练习

在C#中,所有的类都直接或者间接地继承自object类,当我们定义一个类时,没有写明“:”继承哪个类,那么这个类就继承了object。
这里写图片描述
PersonInfo:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 练习
{
    public class PersonInfo
    {
        //Constructor
        public PersonInfo(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }

        //Fields
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private int _age;
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }
    }
}

Reporter:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 练习
{
    public class Reporter:PersonInfo
    {
        public Reporter(string name, int age, string habit)
            : base(name, age)
        {
            this.Habit = habit;
        }

        //Fields
        private string _habit;
        public string Habit
        {
            get { return _habit; }
            set { _habit = value; }
        }
        //Method
        public void Reorting()
        {
            Console.WriteLine("我叫{0},一名记者,我的爱好是{1}", this.Name, this.Habit);
        }
    }
}

Programmer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 练习
{
    class Programmer:PersonInfo
    {
        //Constructor
        public Programmer(string name, int age, int seniority)
            : base(name, age)
        {
            this.Seniority = seniority;
        }

        public Programmer(string name, int seniority)
            : base(name, 0)
        {
            this.Seniority = seniority;
        }

        //Fields
        private int _seniority;
        public int Seniority
        {
            get { return _seniority; }
            set { _seniority = value; }
        }


        //Methods
        public void Programming()
        {
            Console.WriteLine("我叫{0},一名程序员,今年{1}岁了,已经工作了{2}年。", this.Name, this.Age, this.Seniority);
        }
    }
}

Main:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 练习
{
    class Program
    {
        static void Main(string[] args)
        {
            Reporter reporter1 = new Reporter("Alex", 22, "写作");
            Programmer programmer1 = new Programmer("David", 32, 10);
            reporter1.Reorting();
            programmer1.Programming();
            Console.ReadKey();
        }
    }
}

4、new关键字

1)创建对象
2)当子类与父类的方法同名时,隐藏从父类那里继承过来的成员,即将public方法后加“new”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何以问天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值