program.cs
来自「csharp课本的源代码」· CS 代码 · 共 60 行
CS
60 行
using System;
namespace InterfaceExample1
{
interface Ifunction1
{
int sum(int x1, int x2);
}
interface Ifunction2
{
string str { get;set;}
}
class MyTest : Ifunction1, Ifunction2 //此处的冒号表示接口的实现
{
private string mystr;
//构造函数
public MyTest()
{ }
//构造函数
public MyTest(string str)
{
mystr = str;
}
//实现接口Ifunction1中的方法
public int sum(int x1, int x2)
{
return x1 + x2;
}
//实现接口Ifunction2中的属性
public string str
{
get
{
return mystr;
}
set
{
mystr = value;
}
}
}
class Program
{
static void Main(string[] args)
{
//直接访问实例
MyTest a = new MyTest();
Console.WriteLine(a.sum(10, 20));
MyTest b = new MyTest("How are you");
Console.WriteLine(b.str);
//使用接口
Ifunction1 f1 = (Ifunction1)a;
Console.WriteLine(f1.sum(20, 30));
Ifunction2 f2 = (Ifunction2)b;
Console.WriteLine(f2.str);
//按回车键结束
Console.ReadLine();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?