zoukankan      html  css  js  c++  java
  • C# 继承(4)

    接上章:

     class NameList
        {
            public NameList() => Console.WriteLine("这个是NameList的构造函数");
    
            public NameList(string Name) => Console.WriteLine($"这个是NameList的重载构造函数,输入的参数是{Name}");
    
            ~NameList() => Debug.WriteLine("释放NameList");
    
            public string Name { get; set; }
    
            public void ID() => Console.WriteLine($"我的id是{Name}");
        }
    
    
        class A : NameList
        {
    
            public A() : base() => Console.WriteLine("这是A类的初始化,也就是构造函数");
    
            public A(string Name) : base(Name) =>Console.WriteLine($"这个是A的重载构造函数,输入的参数是{Name}");
    
            ~A() => Debug.WriteLine("释放A");
        }
        class B : NameList
        {
    
            public B() : base() => Console.WriteLine("这是A类的初始化,也就是构造函数");
    
            public B(string Name) => Console.WriteLine($"这个是B的重载构造函数,输入的参数是{Name}");
    
            ~B() => Debug.WriteLine("释放B");
    
        }

    这一章 我们来说说 继承的方法和方法隐藏。

    我们来修改代码:

    这个代码比较尬,主要是演示子类中的方法使用父类的方法。

    A类的ShowType方法使用NameList的Show<T>(T type)方法。

    class NameList
        {
            public NameList() => Console.WriteLine("这个是NameList的构造函数");
    
            public NameList(string Name) => Console.WriteLine($"这个是NameList的重载构造函数,输入的参数是{Name}");
    
            ~NameList() => Debug.WriteLine("释放NameList");
    
            public string Name { get; set; }
    
            public void ID() => Console.WriteLine($"我的id是{Name}");
    
            public void Show<T>(T type) where T : NameList => Console.WriteLine(type.GetType().FullName);//泛型方法
        }
    
    
        class A : NameList
        {
    
            public A() : base() => Console.WriteLine("这是A类的初始化,也就是构造函数");
    
            public A(string Name) : base(Name) =>Console.WriteLine($"这个是A的重载构造函数,输入的参数是{Name}");
    
            ~A() => Debug.WriteLine("释放A");
    
            public void ShowType() => base.Show<A>(this);
        }
        class B : NameList
        {
    
            public B() : base() => Console.WriteLine("这是A类的初始化,也就是构造函数");
    
            public B(string Name) => Console.WriteLine($"这个是B的重载构造函数,输入的参数是{Name}");
    
            ~B() => Debug.WriteLine("释放B");
    
            
            
        }

    实例化代码:

       new A().ShowType();

    结果

    上述代码主要是说子类调用父类的方法,使用Base关键字。当然父类的方法必须是公共的方法。

    上面的代码还是比较尬的,赶紧进入下一个环节 继承的隐藏方法

    我们先修改代码:

    在A类中添加一个名为ID的方法。此时A类有自己的ID方法和继承NameList的ID方法。

     class NameList
        {
            public NameList() => Console.WriteLine("这个是NameList的构造函数");
    
            public NameList(string Name) => Console.WriteLine($"这个是NameList的重载构造函数,输入的参数是{Name}");
    
            ~NameList() => Debug.WriteLine("释放NameList");
    
            public string Name { get; set; }
    
            public void ID() => Console.WriteLine($"我的id是{Name}");
    
            public void Show<T>(T type) where T : NameList => Console.WriteLine(type.GetType().FullName);
        }
    
    
        class A : NameList
        {
    
            public A() : base() => Console.WriteLine("这是A类的初始化,也就是构造函数");
    
            public A(string Name) : base(Name) =>Console.WriteLine($"这个是A的重载构造函数,输入的参数是{Name}");
    
            ~A() => Debug.WriteLine("释放A");
    
            public void ShowType() => base.Show<A>(this);
    
            public void ID() => Console.WriteLine("这个ID方法是A类");
        }
        class B : NameList
        {
    
            public B() : base() => Console.WriteLine("这是A类的初始化,也就是构造函数");
    
            public B(string Name) => Console.WriteLine($"这个是B的重载构造函数,输入的参数是{Name}");
    
            ~B() => Debug.WriteLine("释放B");
    
            
            
        }

     实例化:

    new A().ID();

    结果

    结果是使用的A类专属的ID方法,不再使用继承的ID方法。

    但是看代码

     会提示报错,为什么?

    因为子类的方法和父类的方法是相同。有可能会报错,因为子类继承了父类的方法,子类还拥有的相同的方法。此时不知道到底该运行那个方法。所以这个时候要做出抉择。

    这个抉择基本就是规定了。

    使用new关键字来隐藏基类成员或者方法。

    那么问题来了,我要用父类的方法时候,该怎么办?

    嗯,可将子类转父类。

          ((new A()) as NameList).ID();
    
                /*分割线*/
                var a = new A();
                (a as NameList).ID();

    上述两种方法都是将子类转换父类来使用父类的方法。

    值得一说 new关键字是隐藏基类成员,换句话说我在子类中使用和父类一样的方法名和参数时,使用new时,其中的方法内可以实现和父类不一样的代码。

     最终的代码:

    class NameList
        {
            public NameList() => Console.WriteLine("这个是NameList的构造函数");
    
            public NameList(string Name) => Console.WriteLine($"这个是NameList的重载构造函数,输入的参数是{Name}");
    
            ~NameList() => Debug.WriteLine("释放NameList");
    
            public string Name { get; set; }
    
            public void ID() => Console.WriteLine($"我的id是{Name}");
    
            public void Show<T>(T type) where T : NameList => Console.WriteLine(type.GetType().FullName);
        }
    
    
        class A : NameList
        {
    
            public A() : base() => Console.WriteLine("这是A类的初始化,也就是构造函数");
    
            public A(string Name) : base(Name) =>Console.WriteLine($"这个是A的重载构造函数,输入的参数是{Name}");
    
            ~A() => Debug.WriteLine("释放A");
    
            public void ShowType() => base.Show<A>(this);
    
            public new void ID() => Console.WriteLine("这个ID方法是A类");
        }
        class B : NameList
        {
    
            public B() : base() => Console.WriteLine("这是A类的初始化,也就是构造函数");
    
            public B(string Name) => Console.WriteLine($"这个是B的重载构造函数,输入的参数是{Name}");
    
            ~B() => Debug.WriteLine("释放B");
    
            
            
        }

    继承的基本使用就是这样子了

  • 相关阅读:
    LeetCode 382. Linked List Random Node
    LeetCode 398. Random Pick Index
    LeetCode 1002. Find Common Characters
    LeetCode 498. Diagonal Traverse
    LeetCode 825. Friends Of Appropriate Ages
    LeetCode 824. Goat Latin
    LeetCode 896. Monotonic Array
    LeetCode 987. Vertical Order Traversal of a Binary Tree
    LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays
    LeetCode 636. Exclusive Time of Functions
  • 原文地址:https://www.cnblogs.com/T-ARF/p/9208339.html
Copyright © 2011-2022 走看看