C# Interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace thinger.cn.TeachDemo03
{
    /// <summary>
    /// 定义一个会议接口
    /// </summary>
    public interface IMeeting
    {
        /// <summary>
        /// 演讲
        /// </summary>
        void Speech();

        /// <summary>
        /// 讨论
        /// </summary>
        /// <param name="topic"></param>
        /// <returns></returns>
        string Talk(string topic);

        //根据需要在继续添加...
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace thinger.cn.TeachDemo03
{
    /// <summary>
    /// 教学相关的接口
    /// </summary>
    public interface ITeach
    {
        /// <summary>
        /// 教学研究
        /// </summary>
        void StudyCourse();

        /// <summary>
        /// 组织考试
        /// </summary>
        /// <param name="term"></param>
        /// <returns></returns>
        bool Exam(string term);

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace thinger.cn.TeachDemo03
{
    public abstract class Person
    {
        public Person() { }
        public Person(string idNo, string name)
        {

            this.IDNo = idNo;
            this.Name = name;
        }

        public string IDNo { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }

        //这两个是抽像方法
        public abstract void Dowork();
        public abstract void Rest();

        //虚方法
        public virtual string GetInfo()
        {
            return $"编号:{IDNo }  姓名:{Name}";
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace thinger.cn.TeachDemo03
{
    public class Dean : IMeeting
    {
        public void Speech()
        {
            Console.WriteLine($"主任正在演讲...");
        }
        public string Talk(string topic)
        {
            return $"主任正在讨论针对 {topic} ...";
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace thinger.cn.TeachDemo03
{
    public class President : Person, IMeeting
    {
        public President() { }
        public President(string name) { this.Name = name; }


        public override void Dowork()
        {
            Console.WriteLine("校长开始工作...");
        }
        public override void Rest()
        {
            Console.WriteLine("校长下班休息...");
        }

        //实现接口
        public void Speech()
        {
            Console.WriteLine($"{Name}  校长正在演讲...");
        }
        public string Talk(string topic)
        {
            return $"{Name}  校长正在讨论针对 {topic} ...";
        }

        显示实现接口:通常是你实现了两个或两个以上的接口类,但是这些接口类中,有相同的方法
        //void IMeeting.Speech()
        //{
        //    throw new NotImplementedException();
        //}

        //string IMeeting.Talk(string topic)
        //{
        //    throw new NotImplementedException();
        //}

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace thinger.cn.TeachDemo03
{
    public class Teacher : Person, IMeeting,ITeach
    {
        //特有的属性
        public string Post { get; set; }
        public string PhoneNumber { get; set; }

        public Teacher()
        { }
        public Teacher(string name)
        {
            this.Name = name;
        }
        public Teacher(string name, string phone)
        {
            this.Name = name;
            this.PhoneNumber = phone;
        }
        //特有的方法
        public void Lecture()
        {
            Console.WriteLine($"【子类】方法输出:{Name} 正在授课... ");
        }

        /// <summary>
        /// 重写父类的方法
        /// </summary>
        public override void Dowork()
        {
            Console.WriteLine("老师开始工作...");
        }
        public override void Rest()
        {
            Console.WriteLine("老师下班休息...");
        }

        /// <summary>
        /// 来自IMeeting接口实现方法1:演讲
        /// </summary>
        public void Speech()
        {
            Console.WriteLine($"{Name}  老师正在演讲...");
        }
        /// <summary>
        /// 来自IMeeting接口实现方法2:讨论
        /// </summary>
        /// <param name="topic"></param>
        /// <returns></returns>
        public string Talk(string topic)
        {
            return $"{Name}  老师正在讨论针对 {topic} ...";
        }

        /// <summary>
        /// 来自ITeach的接口实现方法1:教学研究
        /// </summary>
        public void StudyCourse()
        {
            Console.WriteLine("老师正在研究教学内容...");
        }
        /// <summary>
        ///  来自ITeach的接口实现方法2:组织考试
        /// </summary>
        /// <param name="term"></param>
        /// <returns></returns>
        public bool Exam(string term)
        {

            //其他相关的业务处理。。。。

            return true;
        }
    }
}

protected
抽象方法:abstract
虚方法:virtual
重写:override
接口:Interface

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值