3月6日 面向对象编程-继承(1)

Profile Picture
- Published on Mar 6, 2020🌏 Public

面向对象编程 - 继承

面向对象编程的三要素: 1. 封装 访问修饰符的控制、字段的封装保护 2. 继承 3. 多态

继承

  1. 父类可以派生出子类(派生类),我们也称子类继承自父类。子类继承父类的所有属性、字段、方法(无论是开放的还是私有的),同时,也可以加入自己的属性、字段、方法。
  2. 所有类的基类都是Object;
  3. 类继承的写法: class ChildrenClass : ParentClass
  4. 继承可以多级, class B : A , class C : B ; ( B:A , C:B -> C:A)
  5. 每一个类,最多只能继承自一个父类
  6. 如果一个类加上sealed 修饰符,这个类就丁克了,不能被从来作为父类。
  7. 父类构造函数是不会被子类继承的,但是他可以在子类的构造函数中手动调用。
class Program
{
    static void Main(string[] args)
    {
        var p = new Male("1","2");
        p.Method2();  //继承过来的
        p.MineMethod3();  //自己的
        p.age = 20;
        p.Sex = 1;
    }
}

class Male : Person
{
    public Male(string x, string y) : base(x, y, 1)
    {

    }
}

class Person : Animal
{
    /**
        *  我们可以在调用Person的构造函数前,去调用父类的构造函数
        *  如果我们没有明确写出要调用的父类构造函数, 会默认调用没有任何参数的父类构造函数
        *  即: base()
        *  必要时,我们可以自己指定要调用的父类构造函数,传入参数进行调用
        */
    public Person(string x,string y,int z)  : base(x,y) {
        Sex = z;
    }

    public int age = 10;

    public int Sex { get; set; }

    public void MineMethod3()
    {
            
    }
}

class Animal
{
   
    public Animal(string prop1,string prop2) {
        Prop1 = prop1;
        Prop2 = prop2;
    }

    private string test1 = "private";
    public string test2 = "public";
    private string Prop1
    {
        get;set;
    }
    public string Prop2
    {
        get;set;
    }
    private void Method1() {
        Console.WriteLine("Method1");
    }
    public void Method2() {
        Method1();
    }
}

练习

写三个类型,分别是人Person、猫Cat、狗Dog、继承自Animal。初始化时, var cat=new Cat("Tom"); cat.SayHi();

    public class Animal{
        public String Name { get; set; }
        public String WuZhong { get; set; }
        public void SayHi()
        {
              //我叫xxx,我是yyy物种。
        }
    }

建立人、狗、猫的实例,存入一个列表,然后用foreach循环调用每个实例的SayHi方法;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;

namespace Test4
{
   class Program
    {
        static void Main()
        {
            var cat = new Cat("Tom");
            var person = new Person("王小明");
            var dog = new Dog("wangwang");
            cat.SayHi();
            person.SayHi();
            dog.SayHi();
            Console.ReadKey();

            List<Animal> animalList = new List<Animal>();
            animalList.Add(cat);
            animalList.Add(person);
            animalList.Add(dog);

            foreach (Animal item in animalList)
            {
                item.SayHi();
            }
            Console.ReadKey();


        }

    }
    public class Cat: Animal
    {
        public Cat(string name) {
            Name = name;
            WuZhong = "猫";
        }
    }
    public class Dog: Animal
    {
        public Dog(string name) {
            Name = name;
            WuZhong = "狗";
        }
    }
    public class Person: Animal
    {
        public Person(string name) {
            Name = name;
            WuZhong = "人";
        }
    }

    public class Animal{
        public String Name { get; set; }
        public String WuZhong { get; set; }
        public void SayHi()
        {
            Console.WriteLine("我叫" + Name + ",我是" + WuZhong);
        }
    }
}