1 Star 1 Fork 0

ClearStari99/C Sharp 教程

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
041 C Sharp 继承 多重继承.cs 1.43 KB
一键复制 编辑 原始数据 按行查看 历史
using System;
/*
C# 多重继承
多重继承指的是一个类别可以同时从多于一个父类继承行为与特征的功能。与单一继承相对,单一继承指一个类别只可以继承自一个父类。
C# 不支持多重继承。但是,您可以使用接口来实现多重继承。
*/
namespace InheritanceApplication
{
class Shape
{
protected int width;
protected int height;
public void SetWidth(int w)
{
width = w;
}
public void SetHeight(int h)
{
height = h;
}
}
// 基类 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();
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/ClearStari99/c-sharp-tutorial.git
git@gitee.com:ClearStari99/c-sharp-tutorial.git
ClearStari99
c-sharp-tutorial
C Sharp 教程
master

搜索帮助