• 接口


    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 接口
    {
        class Program
        {
            static void Main(string[] args)
            {
                ArrayList arr = new ArrayList();
    
                Ren r = new Ren();
                //r.chifan();
                //r.Skill();
                //r.Sport1();
    
                Random ran = new Random();
    
                arr.Add(r);
                arr.Add(ran);
    
                foreach (object o in arr)
                {
                    Ren rr = o as Ren;
                    if (rr != null)
                    {
                        rr.chifan();
                    }
                    else
                    {
                        Console.WriteLine("没转换成功!");
                    }
                }
    
                Console.ReadKey();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 接口
    {
        class Ren : HuoZhe, Work, Sports
        {
            public override void chifan()
            {
                Console.WriteLine("用嘴吃饭!");
            }
    
            public void Skill()
            {
                Console.WriteLine("会编程!");
            }
    
            public void Sport1()
            {
                Console.WriteLine("会踢足球!");
            }
    
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 接口
    {
        abstract class HuoZhe
        {
            public abstract void chifan();
    
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 接口
    {
        public interface Work  //接口数据 interface
        {
            void Skill();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 接口
    {
        public interface Sports
        {
            void Sport1();
        }
    }

    接口:
    在团队开发中,一个类,需要多个模块组合起来才是完整的类;
    多个人开发不同的模块,最后要把它们拼接起来,靠的就是接口;

    一个类,需要继承多个类才是完整的,但是程序规定,一个类只能继承一个父类;
    为了解决这个问题,就出现了接口,一个类可以继承无数个接口;

    人 这个类,需要会吃饭,技能,运动,才是一个完整的人;
    吃饭这个功能是A单独开发的,作为人的最基本的父类,使用的是抽象类;
    技能和运动,是B和C分别开发的,需要人这个类来继承,但是已经有父类了;
    那么B和C就使用的接口,来让人这个类可以继承他们写的两个功能模块;

    接口里面的方法很像抽象方法;

    接口也是起到一个规范和约束的作用;


    is和as运算符:
    is是判断是否是某个类型,返回true或false
    o as Ren; 如果转换成功了,没问题;
    如果没转换成功,不会报出错误,而是返回一个null值

  • 相关阅读:
    ZJOI2018]历史 做题心得 Flandre
    Codeforces 1495F 搞了一上午的心得 Flandre
    关于JS的跨域通信的几种解决方案 (转)
    我对php框架的理解和看法
    ob_get_contents();ob_end_clean();ob_start();的具体用法
    MySQL性能优化 (转载)
    遇到过的一些php笔试题
    php memcached 安装 install(转载)
    [转]mysql_fetch_row,mysql_fetch_array,mysql_fetch_object,mysql_fetch_assoc的区别
    深度探讨PHP之性能(看到的好文章,就转载啦)
  • 原文地址:https://www.cnblogs.com/zhangdemin/p/5604067.html
Copyright © 2020-2023  润新知