📄 11-3.cs
字号:
//程序11-3
using System;
interface IDistributable
{
void Distribute();
void Store();
}
interface ISalable
{
void Sale();
void Distribute();
}
public class Publish:IDistributable,ISalable
{
protected int num;
protected double prc;
public Publish()
{}
public Publish(int n, double p)
{
num=n;
prc=p;
}
public void Distribute() // 实现接口成员
{
Console.WriteLine("Distributed in campus only.");
}
public void Sale() // 实现接口成员
{
Console.WriteLine("Sale on cost.");
}
void ISalable.Distribute() // 实现显式接口成员
{
Console.WriteLine("Where to distribute?");
}
void IDistributable.Store() // 实现显式接口成员
{
Console.WriteLine("To Store.");
}
public void ToPublish ()
{
Console.WriteLine("Number: {0}, Price: ¥{1} ",num,prc);
//ISalable.Distribute(); //错误,无法直接调用显式接口成员实现
((ISalable)this).Distribute(); //允许通过转换调用显式接口成员实现
}
}
public class LectureNotes :Publish
{
protected string Name;
public LectureNotes()
{}
public LectureNotes(string nm,int n, double p):base(n,p)
{
Name = nm;
num=n;
prc=p;
}
public void Title()
{
Console.WriteLine("The LectureNotes Title: "+Name);
}
public void ForStore()
{
((IDistributable)this).Store(); //在派生类中调用基类的显式接口成员实现
}
}
public class Test
{
public static void Main()
{
LectureNotes LN = new LectureNotes("C# Programming", 400, 28.6); //类对象
ISalable ExplicitImpl = LN; //接口对象
LN.Title();
LN.ToPublish();
ExplicitImpl.Distribute(); //通过接口对象调用显式接口成员实现
((ISalable)LN).Distribute(); //通过对类对象的转换调用显式接口成员实现
LN.Distribute(); //通过类对象调用接口成员实现
LN.Sale();
LN.ForStore();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -