深入分析c# 继承
- 作者: 亖呉?盀
- 来源: 51数据库
- 2021-08-26
继承是面向对象程序设计中最重要的概念之一。继承允许我们根据一个类来定义另一个类,这使得创建和维护应用程序变得更容易。同时也有利于重用代码和节省开发时间。
当创建一个类时,程序员不需要完全重新编写新的数据成员和成员函数,只需要设计一个新的类,继承了已有的类的成员即可。这个已有的类被称为的基类,这个新的类被称为派生类。
继承的思想实现了 属于(is-a) 关系。例如,哺乳动物 属于(is-a) 动物,狗 属于(is-a) 哺乳动物,因此狗 属于(is-a) 动物。
基类和派生类
一个类可以派生自多个类或接口,这意味着它可以从多个基类或接口继承数据和函数。
c# 中创建派生类的语法如下:
<访问修饰符符> class <基类>
{
...
}
class <派生类> : <基类>
{
...
}
假设,有一个基类 shape,它的派生类是 rectangle:
using system;
namespace inheritanceapplication
{
class shape
{
public void setwidth(int w)
{
width = w;
}
public void setheight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// 派生类
class rectangle: shape
{
public int getarea()
{
return (width * height);
}
}
class rectangletester
{
static void main(string[] args)
{
rectangle rect = new rectangle();
rect.setwidth(5);
rect.setheight(7);
// 打印对象的面积
console.writeline("总面积: {0}", rect.getarea());
console.readkey();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
总面积: 35
基类的初始化
派生类继承了基类的成员变量和成员方法。因此父类对象应在子类对象创建之前被创建。您可以在成员初始化列表中进行父类的初始化。
下面的程序演示了这点:
using system;
namespace rectangleapplication
{
class rectangle
{
// 成员变量
protected double length;
protected double width;
public rectangle(double l, double w)
{
length = l;
width = w;
}
public double getarea()
{
return length * width;
}
public void display()
{
console.writeline("长度: {0}", length);
console.writeline("宽度: {0}", width);
console.writeline("面积: {0}", getarea());
}
}//end class rectangle
class tabletop : rectangle
{
private double cost;
public tabletop(double l, double w) : base(l, w)
{ }
public double getcost()
{
double cost;
cost = getarea() * 70;
return cost;
}
public void display()
{
base.display();
console.writeline("成本: {0}", getcost());
}
}
class executerectangle
{
static void main(string[] args)
{
tabletop t = new tabletop(4.5, 7.5);
t.display();
console.readline();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
长度: 4.5
宽度: 7.5
面积: 33.75
成本: 2362.5
c# 多重继承
多重继承指的是一个类别可以同时从多于一个父类继承行为与特征的功能。与单一继承相对,单一继承指一个类别只可以继承自一个父类。
c# 不支持多重继承。但是,您可以使用接口来实现多重继承。下面的程序演示了这点:
using system;
namespace inheritanceapplication
{
class shape
{
public void setwidth(int w)
{
width = w;
}
public void setheight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// 基类 paintcost
public interface paintcost
{
int getcost(int area);
}
// 派生类
class rectangle : shape, paintcost
{
public int getarea()
{
return (width * height);
}
public int getcost(int area)
{
return area * 70;
}
}
class rectangletester
{
static void main(string[] args)
{
rectangle rect = new rectangle();
int area;
rect.setwidth(5);
rect.setheight(7);
area = rect.getarea();
// 打印对象的面积
console.writeline("总面积: {0}", rect.getarea());
console.writeline("油漆总成本: ${0}" , rect.getcost(area));
console.readkey();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
总面积: 35
油漆总成本: $2450
以上就是深入分析c# 继承的详细内容,更多关于c# 继承的资料请关注其它相关文章!
推荐阅读
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
热点文章
团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
0
使用 MSBuild.exe 在发布模式下构建 C# 解决方案
0
当我发布 Web 应用程序时,AfterPublish 脚本不运行
0
构建时 T4 转换的产品仅在下一个构建中使用
0
ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
0
新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
0
如何将条件编译符号(DefineConstants)传递给 msbuild
0
MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
0
NuGet 包还原找不到包,没有源
0
使用 C# 6.0 功能运行 TFS 构建
0
