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

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

发布了27 篇原创文章 · 获赞 1 · 访问量 1673

猜你喜欢

转载自blog.csdn.net/qq_37959151/article/details/104988573