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