C# 接口

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();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值