接上章:

 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"); }

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

最新文章

  1. postgresql无法安装pldbgapi的问题
  2. 安装php-amqplib(RabbitMQ的phpAPI)
  3. Lua 学习笔记(一)环境搭建
  4. vim does not map customized key?
  5. iOS MRC ARC 内存管理
  6. xml scheme 示例解析
  7. Wcf资料收集
  8. 简单的JQuery top返回顶部
  9. BZOJ 2724: [Violet 6]蒲公英( 分块 )
  10. Python操作redis、memcache和ORM框架_Day13
  11. sql语句-排序后加入序号再运算判断取想要的项
  12. Linux下 Unison 实现文件双向同步
  13. 将非常规Json字符串转换为常用的json对象
  14. springtest-junit-jidi--测试接口
  15. 要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10;
  16. bootstrap全局样式二
  17. 转载:java中Thread.sleep()函数使用
  18. [转载]Linux目录说明
  19. sysdig
  20. display:table-cell自适应布局下连续单词字符换行——张鑫旭

热门文章

  1. c# 各种tips
  2. springboot成神之——springboot入门使用
  3. leetcode343
  4. java中的 equals 与 ==
  5. google/dense_hash_map
  6. IWebBrowser和IE浏览器的行为不一样
  7. 微信小程序怎么获取用户输入
  8. Tensorflow Mask-RCNN训练识别箱子的模型运行结果(练习)
  9. msql 计算连续签到天数
  10. cv 验证