using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace CSharpInheritanceLab
{
internal class BaseDemo
{
public string SomeMethod()
{
return "base-called";
}
}
internal class DerivedDemo : BaseDemo
{
public string AnotherMethod()
{
return base.SomeMethod() + "+derived";
}
}
internal class Shape
{
protected int width;
protected int height;
public void SetWidth(int w) { width = w; }
public void SetHeight(int h) { height = h; }
}
internal class SimpleRectangle : Shape
{
public int GetArea()
{
return width * height;
}
}
internal class RectBase
{
protected double length;
protected double width;
public static List<string> InitOrder = new List<string>();
public RectBase(double l, double w)
{
length = l;
width = w;
InitOrder.Add("RectBase");
}
public double GetArea()
{
return length * width;
}
public string DisplaySummary()
{
return string.Format(
CultureInfo.InvariantCulture,
"L={0},W={1},A={2}",
length, width, GetArea()
);
}
}
internal class Tabletop : RectBase
{
public Tabletop(double l, double w) : base(l, w)
{
InitOrder.Add("Tabletop");
}
public double GetCost()
{
return GetArea() * 70.0;
}
public new string DisplaySummary()
{
return string.Format(
CultureInfo.InvariantCulture,
"{0},Cost={1}",
base.DisplaySummary(), GetCost()
);
}
}
internal interface IBaseInterface
{
string Method1();
}
internal interface IDerivedInterface : IBaseInterface
{
string Method2();
}
internal class MyInterfaceImpl : IDerivedInterface
{
public string Method1()
{
return "Method1 implementation";
}
public string Method2()
{
return "Method2 implementation";
}
}
internal interface IPaintCost
{
int GetCost(int area);
}
internal class PaintedRectangle : Shape, IPaintCost
{
public int GetArea()
{
return width * height;
}
public int GetCost(int area)
{
return area * 70;
}
}
internal class Program
{
private static string Esc(string s)
{
if (s == null) return "null";
StringBuilder sb = new StringBuilder("\"");
foreach (char c in s)
{
switch (c)
{
case '\\': sb.Append("\\\\"); break;
case '"': sb.Append("\\\""); break;
case '\n': sb.Append("\\n"); break;
case '\r': sb.Append("\\r"); break;
case '\t': sb.Append("\\t"); break;
default:
if (c < 32) sb.AppendFormat("\\u{0:x4}", (int)c);
else sb.Append(c);
break;
}
}
sb.Append("\"");
return sb.ToString();
}
private static string CaseJson(string name, string summary, string expected, string actual)
{
bool passed = string.Equals(expected, actual, StringComparison.Ordinal);
return string.Format(
"{{\"name\":{0},\"summary\":{1},\"expected\":{2},\"actual\":{3},\"passed\":{4}}}",
Esc(name), Esc(summary), Esc(expected), Esc(actual), passed ? "true" : "false"
);
}
private static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
List<string> cases = new List<string>();
// Case 1: Base & derived class, protected members & base method call
DerivedDemo dd = new DerivedDemo();
SimpleRectangle rect = new SimpleRectangle();
rect.SetWidth(5);
rect.SetHeight(7);
string actual1 = string.Format(
"call={0},area={1},isShape={2}",
dd.AnotherMethod(), rect.GetArea(), rect is Shape
);
cases.Add(CaseJson(
"base_and_derived_shape",
"验证派生类通过 base 调用基类方法、访问 protected 字段以及 IS-A 继承关系",
"call=base-called+derived,area=35,isShape=True",
actual1
));
// Case 2: Base class constructor initialization via : base(l, w)
RectBase.InitOrder.Clear();
Tabletop t = new Tabletop(4.5, 7.5);
string actual2 = string.Format(
"order={0},summary={1}",
string.Join("->", RectBase.InitOrder.ToArray()),
t.DisplaySummary()
);
cases.Add(CaseJson(
"base_constructor_initialization",
"验证派生类构造函数通过 : base(l, w) 先于子类体初始化基类并复用 base.DisplaySummary",
"order=RectBase->Tabletop,summary=L=4.5,W=7.5,A=33.75,Cost=2362.5",
actual2
));
// Case 3: Interface inheritance
MyInterfaceImpl impl = new MyInterfaceImpl();
IBaseInterface asBase = impl;
string actual3 = string.Format("{0}|{1}", asBase.Method1(), impl.Method2());
cases.Add(CaseJson(
"interface_inheritance",
"验证派生接口继承基接口成员且实现类必须同时实现 Method1 与 Method2",
"Method1 implementation|Method2 implementation",
actual3
));
// Case 4: Multiple inheritance simulated via single class + interface
PaintedRectangle pr = new PaintedRectangle();
pr.SetWidth(5);
pr.SetHeight(7);
int area = pr.GetArea();
int cost = pr.GetCost(area);
string actual4 = string.Format(
"area={0},cost={1},isShape={2},isPaint={3}",
area, cost, pr is Shape, pr is IPaintCost
);
cases.Add(CaseJson(
"multiple_inheritance_via_interface",
"验证 C# 通过单类继承结合接口实现(Shape + IPaintCost)完成多重契约组合",
"area=35,cost=2450,isShape=True,isPaint=True",
actual4
));
Console.WriteLine(string.Format(
"{{\"version\":{0},\"cases\":[{1}]}}",
Esc("CLR " + Environment.Version.ToString()),
string.Join(",", cases.ToArray())
));
}
}
}
本例需在 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。
入口文件:projects/csharp-inheritance-lab/ExploreInheritance.cs
本地运行步骤快速上手
- 点击上方“保存源码”保存为
ExploreInheritance.cs,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联42章) · 含当前章节或实例 · mbskycom202612a1321.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1337.zip