zoukankan      html  css  js  c++  java
  • C#面向对象10 继承

    1.继承:

    ****

    我们可能会在一些类中,写一些重复的成员。我们可以将这些重复的成员,单独的封装到一个类中,作为这些类的父类。

    Student,Teacher,Driver ----子类  派生类

    Person                          ----父类  基类

    子类继承于父类:

    ****

     子类继承了父类,那么子类从父类那里继承过来了什么?

    首先,子类继承了父类的属性和方法,但是子类并没有继承父类的私有字段。

    问题:子类有没有继承父类的构造函数?

    答:子类并没有继承父类的构造函数,但是,子类会默认的调用父类无参数的构造函数,创建父类对象,让子类可以使用父类中的成员。

         所以,如果在父类中重新写了一个有参数的构造函数之后,那个无参数的就被干掉了,子类就调用不到了,所以子类会报错。

    解决方法:

    1)在类中重新写一个无参数的构造函数。

    2)在子类中显示的调用父类的构造函数,使用关键字:base()

    2.继承的特性:

    1)继承的单根性:一个子类只能有一个父类!

    2)继承的传递性:祖宗的迟早是我的!

    3.查看类图

    例子代码:

    Person类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        public class Person
        {
            public Person(string name,int age,char gender)
            {
                this.Name = name;
                this.Age = age;
                this.Gender = gender;
            }
    
            private string _name;
    
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
            private int _age;
    
            public int Age
            {
                get { return _age; }
                set { _age = value; }
            }
            private char _gender;
    
            public char Gender
            {
                get { return _gender; }
                set { _gender = value; }
            }
    
            public void CHLSS()
            {
                Console.WriteLine("吃喝拉撒睡!");
            }
    
    
        }
    }

    Student类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        public class Student : Person
        {
            public Student(string name, int age, char gender, int id)
                : base(name, age, gender)
            {
                this.Id = id;
            }
    
            private int _id;
    
            public int Id
            {
                get { return _id; }
                set { _id = value; }
            }
    
            public void Study()
            {
                Console.WriteLine("Study!");
            }
        }
    }

    Teacher类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        public class Teacher : Person
        {
            public Teacher(string name, int age, char gender, decimal salary)
                : base(name, age, gender)
            {
                this.Salary = salary;
            }
    
            private decimal _salary;
    
            public decimal Salary
            {
                get { return _salary; }
                set { _salary = value; }
            }
    
            public void Teach()
            {
                Console.WriteLine("Teach!");
            }
        }
    }

    Driver类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        public class Driver : Student
        {
            public Driver(string name, int age, char gender, int id, int drivertime)
                : base(name, age, gender, id)
            {
                this.DriverTime = drivertime;
            }
    
            private int _driverTime;
    
            public int DriverTime
            {
                get { return _driverTime; }
                set { _driverTime = value; }
            }
    
            public void Drive()
            {
                Console.WriteLine("Drive!");
            }
        }
    }

    4.object是所有类的基类。

    5.new 关键字

    1)创建对象

    2)隐藏从父类中继承的同名成员。隐藏的后果就是子类无法调用父类的成员。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        public class Person
        {
            public Person(string name,int age,char gender)
            {
                this.Name = name;
                this.Age = age;
                this.Gender = gender;
            }
    
            private string _name;
    
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
            private int _age;
    
            public int Age
            {
                get { return _age; }
                set { _age = value; }
            }
            private char _gender;
    
            public char Gender
            {
                get { return _gender; }
                set { _gender = value; }
            }
    
            public void CHLSS()
            {
                Console.WriteLine("吃喝拉撒睡!");
            }
            
            /// <summary>
            /// 测试SayHello方法
            /// </summary>
            public void SayHello()
            {
                Console.WriteLine("Hello!");
            }
    
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        public class Student : Person
        {
            public Student(string name, int age, char gender, int id)
                : base(name, age, gender)
            {
                this.Id = id;
            }
    
            private int _id;
    
            public int Id
            {
                get { return _id; }
                set { _id = value; }
            }
    
            public void Study()
            {
                Console.WriteLine("Study!");
            }
    
            /// <summary>
            /// new 关键字隐藏父类中的方法SayHello
            /// </summary>
            public new void SayHello()
            {
                Console.WriteLine("Hello,Student!");
            }
        }
    }

    6.MindeMap

  • 相关阅读:
    IOC容器特性注入第六篇:利用MVC注入点,把容器启动
    IOC容器特性注入第五篇:查找(Attribute)特性注入
    以女朋友为例讲解 TCP/IP 三次握手与四次挥手
    Kali信息收集
    Python3 异常处理
    Python3 hasattr()、getattr()、setattr()、delattr()函数
    Python3 常用模块
    Python设计模式——外观模式
    Python设计模式——模版方法模式
    一个很神奇的类
  • 原文地址:https://www.cnblogs.com/youguess/p/8458012.html
Copyright © 2011-2022 走看看