• 继承 多态


    2.继承:在程序中,如果一个类A:类B,这种机制就是继承。
    子类可以继承父类的所有内容(成员)吗?
    解析:
    1.私有成员(属性和方法)
    2.构造函数
    3.final修饰过的方法,子类不能进行重写



    3.访问修饰符
    public 公有的
    private 私有的
    protected 受保护的


    在java中,如果大家使用protected访问修饰符,来修饰一个变量,那么在当前包中的所有类中都可以访问。外加上不同包
    类中有继承关系的类也可以访问。


    4.揭秘子类构造
    1.Main函数
    2.子类构造,不进入子类构造体
    3.执行到父类构造,不进入父类构造体
    4.执行父类构造体 创建父类对象
    5.回到子类构造体执行
    6.子类构造体执行完毕 构造子类对象
    7.回到Main,继续后续代码执行


    5.base(父类构造)
    base第一个用法:使用属性 base.属性名
    base第二个用法;使用方法 base.方法名称相同
    base第三个用法:调用父类构造 base()


    6.继承的传递性和单根性
    C#中不支持多继承
    Java中不支持多继承 使用接口可以变相的支持多继承
    C#和Java 面向对象 不支持多继承

    实例代码:父类

    public class Employee
    {
    public Employee() {
    Console.WriteLine("父类无参对象构造执行");
    }
    public Employee(string id, int age, string name, Gender gender) {
    this.ID = id;
    this.Age = age;
    this.Name = name;
    this.Gender = gender;
    }
    protected string ID { get; set; }

    protected int Age { get; set; }

    protected string Name { get; set; }

    protected Gender Gender { get; set; }

    }

    子类:

    public SE(string id,string name,int age,Gender gender,int popularity) :base(id,age,name,gender)
    {
    this.Popularity = popularity;
    }public SE() { }
    private int _popularity;
    public int Popularity {
    get { return _popularity; }
    set { _popularity = value; }
    }
    public string SayHi(){
    string message = string.Format("大家好,我是{0},今年{1}岁,我的人气值是{2}。", this.Name, this.Age, this.Popularity);
    return message;
    }


    7.多态初步
    多态:多种形态
    不同的对象 对于 同一个操作 做出的响应不同 。多态。

    举例子:
    1. USB父类 USB鼠标/USB键盘/USB照明
    2. 鸭子 真实鸭子 /橡皮鸭子 木头鸭子(不会叫)
    3. CUT 医生/理发师/演员
    4. 打招呼 英文/含于/韩国人
    5. 交通工具 汽车/地铁/自行车


    父类有一个Cut方法,子类中有同名方法Cut
    如何满足多态的条件

    父类:Person
    子类A:Hairdresser 理发师
    子类B:Doctor:医生
    子类C:Actor :演员



    实现多态条件
    1.父类有一个用virtual关键字修饰的方法

    2.子类必须有一个同名方法,使用Override关键字

    3.将N个子类对象放入父类类型集合。。

    4.依次迭代

    列:父类

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

    namespace ConsoleApplication1
    {
    public class parent
    {
    public virtual void Cut() {

    Console.WriteLine("这是");
    }
    }
    }

    子类:

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

    namespace ConsoleApplication1
    {
    public class Zi:parent
    {
    public override void Cut() {

    Console.WriteLine("医生");
    }

    }
    }

    main:

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

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    List<parent> list = new List<parent>(){
    new Zi()
    };
    foreach (parent item in list)
    {
    item.Cut();

    }
    Console.ReadLine();
    }
    }
    }

  • 相关阅读:
    Python-Image 基本的图像处理操作
    剪枝
    poj1182(食物链)续
    HLG2035广搜
    HLG2040二叉树遍历已知前中,求后
    先序,中序,后序,已知两者求第三者
    C++中new的解说
    僵尸进程
    HLG2062(make,heap问题)
    make_head,,,pop_head,,,push_head,,,sort_head..
  • 原文地址:https://www.cnblogs.com/wangbenqing/p/6567680.html
Copyright © 2020-2023  润新知