C# 委托2

不用委托思想的例子 : 

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

namespace detegateClass2
{
    class Student
    {
        private int score;

        private ResponseScore responseScore;


        /// <summary>
        /// 这里是用接口设置的方法来使具体业务调用抽象对象
        /// </summary>
        /// <param name="people"></param>
        public void setResponseScore(ResponseScore people)
        {
            responseScore = people;
        }

        public void setSocre(int value)
        {
            score = value;

            if (score < 0 || score > 100)
            {
                Console.WriteLine("分数不正确");
            }
            else
            {
                //学生究竟受到谁的答复呢?这个方法已经是业务类的了,它不需要知道具体的东西,但由于这个例子,我们不采用委托的思想,所以,这里我们采用接口 

                //string result = 接口.方法(参数) ;
                string result = responseScore.response(score);

                Console.WriteLine("学生收到的回复是: "+ result); //学生收到的回复!
            }
        }
    }

    /// <summary>
    /// 这里是一个业务类型的接口
    /// </summary>
    interface ResponseScore
    {
        string response(int score); //一个负责回复的方法
    }


    class Teacher : ResponseScore
    {
        public string response(int score)
        {
            if (score > 60)
            {
                return "不错 ! " + score + " 是一个好分数";
            }
            else
            {
                return "继续加油! 能考到" + score + "分,已经不错的了";
            }
        }
    }
    /// <summary>
    /// 在这个例子中我们模拟一个学生考试后,收到不同人物对他的评价,可以是老师,同学,家长,甚至警察,这里我们先不采用委托的放,我们先模拟一个接口
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Student s = new Student();

            ResponseScore t = new Teacher();

            s.setResponseScore(t);

            s.setSocre(80);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值