using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*C#程序设计的基本单元是类,但面向对象的程序设计的基本单元是类型。
类可以定义类型,如果定义类型而不必定义类,那将会是相当强大而有用的。
接口以一种抽象的形式定义类型,作为方法或者其他类型的集合,从而形成该类型的约定*/
namespace 接口
{
//定义接口
interface IStorable //interface接口关键字,IStorable接口名称
{
//定义接口,注意没有为接口的方法提供实现
void Read();
void Write(object obj);
int Status { get; set; }
}
//创建一个Document类,实现IStorable接口
class Document : IStorable
{
//定义构造函数
public Document(string s)
{
Console.WriteLine("创建文档:{0}", s);
}
//实现Read方法
public void Read()
{
Console.WriteLine("为接口IStorable实现Read()方法");
}
//实现Write方法
public void Write(object o)
{
Console.WriteLine("为接口IStorable实现Write()方法");
}
//实现属性
private int status = 0;
public int Status
{
get { return status; }
set { status = value; }
}
}
class Program
{
public void Run()
{
Document doc = new Document("测试文档");
doc.Status=-1;
doc.Read();
Console.WriteLine("文档状态:{0}",doc.Status);
}
static void Main(string[] args)
{
Program p=new Program();
p.Run(); //这里展示了如何调用类自身的函数,先new一个Program(),然后再调用
Console.ReadLine();
}
}
}
C# 接口
最新推荐文章于 2024-05-11 09:38:13 发布