zoukankan      html  css  js  c++  java
  • C# 如何创建接口以及使用接口的简单Demo(转载!)

    //No:1  首先,我们要封装一个接口,接口中不要实现具体的方法(说白了这就是一个架子而已!)

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

    namespace ConsoleApplication1
    {
        interface Ipeople //接口声明:
        {
            int w
            {
                get;
                set;
            }
            int h
            {
                get;
                set;
            }
        }

    //No:2 接口的调用
        class MyPoint : Ipeople //实现接口
        {

            //这里的类就必须要实现接口中未实现的方法
            private int myW;
            private int myH;
            public MyPoint(int w, int h)
            {
                myW = w;
                myH = h;
            }
            public int w
            {
                get { return myW; }
                set { myW = value;}
            }
            public int h
            {
                get { return myH; }
                set { myH = value;}
            }
        }

    //No:2 接口的调用
        class Program
        {
            
            static void Main(string[] args)
            {
                MyPoint mp = new MyPoint(35, 170); //传入实际参数
                print(mp);
                Console.Read();
            }
            private static void print(Ipeople ip)
            {
                Console.WriteLine("体重为:{0},身高为:{1} ", ip.w, ip.h);
            }
        }
    }

    No : 4结果如下,不知道你是否有所领悟呢。

     
     
    其实我们写的代码都可以封装成接口以便以后使用时不会重复花更多时间做无用功。
  • 相关阅读:
    HashMap 实现原理
    王东江网站
    网站建设
    mysql 查询 执行流程
    两个线程交替打印1到100
    三个线程交替打印十次ABC
    Java动态链接是什么意思
    双亲委派机制
    笔记
    redis集群搭建
  • 原文地址:https://www.cnblogs.com/xiaobaicai12138/p/5498741.html
Copyright © 2011-2022 走看看