.net接口实现不同的功能 ,降低耦合

5 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static test.father;
using static test.Program;

namespace test
{
    class test
    {
        public int num;
    }

    class paper : Content //读报纸
    {
        public void getContent()
        {
            Console.WriteLine("爸爸开始读报纸了~");
        }
        public void Test()
        {
            Console.WriteLine("test");
        }
    }

    class tv:Content  //看电视
    {
        public void getContent()
        {
            Console.WriteLine("爸爸开始看电视了~");
        }
        public void Test()
        {
            Console.WriteLine("test");
        }

    }
    class father
    {
        private Content content;
        
        public father(Content content)
        {
            this.content = content;
        }
        public void  ToRead()   //接口调用
        {
            Console.WriteLine("爸爸开始干事情了");
            content.getContent();
        }
    }
    class Program
    {
        public interface Content   //接口实现爸爸读不同东西的功能

        {
            void getContent();

        }
        static void Main(string[] args)
        {
          
          
            father father1 = new father(new paper());
            father1.ToRead();

            father father2 = new father(new tv());
            father2.ToRead();

            new paper().Test();
            new tv().Test();
            Console.ReadKey();
        }
    }
}

运行结果:

爸爸开始干事情了
爸爸开始读报纸了~
爸爸开始干事情了
爸爸开始看电视了~
test
test

转载:https://www.cnblogs.com/youmingkuang/category/775976.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值