C#4.0图解教程 - 第7章 类和继承
- 7.5 使用基类的引用#
- #
- 7.5.1 虚方法和覆写方法#
- #
- 7.5.2 覆写标记为override的方法#
- #
- 7.8 成员访问修饰符#
- 7.10 抽象类 abstract#
- 7.11 密封类 sealed#
- 7.12 静态类#
7.5使用基类的引用#
派生类的实例由基类的实例加上派生类附加的成员组成,派生类引用指向整个类对象,包括基类部分。
MyDerivedClass derived = new MyDerivedClass();
MyBaseClass mybc = (MyBaseClass)derived;
using System;
namespace Examples
{
class MyBaseClass
{
public void Print()
{
Console.WriteLine("This is the base class.");
}
}
class MyDerivedClass : MyBaseClass
{
new public void Print()
{
Console.WriteLine("This is the derived class.");
}
}
class Program
{
static void Main()
{
MyDerivedClass derived = new MyDerivedClass();
MyBaseClass mybc = (MyBaseClass)derived;
derived.Print();
mybc.Print();
}
}
}
This is the derived class.
This is the base class.
请按任意键继续. . .
#
7.5.1 虚方法和覆写方法#
虚方法可以使基类的引用方式“升至”派生类内。
using System;
namespace Examples
{
class MyBaseClass
{
virtual public void Print()
{
Console.WriteLine("This is the base class.");
}
}
class MyDerivedClass : MyBaseClass
{
override public void Print()
{
Console.WriteLine("This is the derived class.");
}
}
class Program
{
static void Main()
{
MyDerivedClass derived = new MyDerivedClass();
MyBaseClass mybc = (MyBaseClass)derived;
derived.Print();
mybc.Print();
}
}
}
This is the derived class.
This is the derived class.
请按任意键继续. . .
#
7.5.2 覆写标记为override的方法#
当使用对象基类部分的引用调用一个覆写方法时,方法的调用被沿派生层次上溯执行,一直到标记为override的方法的最派生(most-derived)版本。
情况1:使用override声明Print()
using System;
namespace Examples
{
class MyBaseClass // Base class
{
virtual public void Print()
{
Console.WriteLine("This is the base class.");
}
}
class MyDerivedClass : MyBaseClass // Derived class
{
override public void Print()
{
Console.WriteLine("This is the derived class.");
}
}
class SecondDerived : MyDerivedClass
{
override public void Print() //这里使用override
{
Console.WriteLine("This is the second derived class.");
}
}
class Program
{
static void Main()
{
SecondDerived derived = new SecondDerived(); // Use SecondDerived.
MyBaseClass mybc = (MyBaseClass)derived; // Use MyBaseClass.
derived.Print();
mybc.Print();
}
}
}
This is the second derived class.
This is the second derived class.
请按任意键继续. . .
情况2:使用new声明Print()
class MyDerivedClass : MyBaseClass
{
new public void Print()
{
Console.WriteLine("This is the derived class.");
}
}
class Program
{
static void Main()
{
MyDerivedClass derived = new MyDerivedClass();
MyBaseClass mybc = (MyBaseClass)derived;
derived.Print();
mybc.Print();
}
}
This is the second derived class.
This is the derived class.
请按任意键继续. . .
#
7.8 成员访问修饰符#
internal 同一程序集访问
protected 派生类可访问
protected internal 听那个一程序集和派生类可访问
7.10 抽象类 abstract#
7.11 密封类 sealed#
抽象方法必须是基类,它不能实例化。密封类恰好相反。
7.12 静态类#
类必须为静态,类成员也必须为静态。
作者:【唐】三三
出处:https://www.cnblogs.com/tangge/archive/2012/12/07/2807330.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具