zoukankan      html  css  js  c++  java
  • (4)C#的“假类”和数组

    大家都知道java是单继承语言,想要继承多个类,就要靠接口去实现了。C#中也是一样,众多的接口为编程提供了极大的便利。自己定义接口也是个不错的选择。生活中一个儿子想继承多个父亲的财产是不可能的,天上不能掉馅饼,接口能提供给每个类的只是一个个函数的空壳,所有的方法都要类自己实现。人人公平,不搞特殊化。简单的接口实现:

    完整的代码如下:

    namespace ConsoleApplication4

    {

        public interface Ifirst

        {

            void readblog();

        }

     

        interface Isecond

        {

            void readblog2();

        }

        class Program :Ifirst , Isecond

        {

            public void readblog(){

                    Console.WriteLine("第一个接口被实现");

                }

            public void readblog2()

            {

                Console.WriteLine("第二个接口被实现");

            }

            static void Main(string[] args)

            {

                Program a = new Program();

                a.readblog();

                a.readblog2();

            }

        }

    }

     

     

    有些同学要问了,如果两个接口中有同名函数,但是函数都干着不同的事情怎么办?C#提供了隐式定义和显示定义两种方法,来解决这个问题:

    namespace ConsoleApplication4

    {

        public interface Ifirst

        {

            void readblog();

        }

     

       public interface Isecond

        {

            void readblog();

        }

        class Program :Ifirst , Isecond

        {

            //隐式定义

            public void readblog(){

                    Console.WriteLine("第一个接口被实现");

                }

            //显示定义

             void Isecond.readblog()

            {

                Console.WriteLine("第二个接口被实现");

            }

            static void Main(string[] args)

            {

                Program a = new Program();

                a.readblog();

                Isecond b = a;

                b.readblog();        //调用显示定义的readblog()

            }

        }

    }

    从上面这个例子看,接口是不是更具有类的特征?语言设计师的意图从不拘泥于一种样式,既然有了多继承,那就弄出个接口的概念,比继承父类更加灵活多变。

     

    还有一个关键字很生动形象,叫where

    Where的用法

    where 子句用于指定类型约束,这些约束可以作为泛型声明中定义的类型参数的变量。
     1.接口约束。
     例如,可以声明一个泛型类 MyGenericClass,这样,类型参数 T 就可以实现 IComparable<T> 接口:

    public class MyGenericClass<T> where T:IComparable { }


     2.基类约束:指出某个类型必须将指定的类作为基类(或者就是该类本身),才能用作该泛型类型的类型参数。
     这样的约束一经使用,就必须出现在该类型参数的所有其他约束之前。

    class MyClassy<T, U>
     where T : class
     where U : struct
    {
    }听说过不规则数组吗?C#里就能实现,对于这种长得很难看的东西,C#有自己的办法。

    namespace ConsoleApplication4

    {

       

        class Program 

        {

            public void displayvalue(params int[] values)

            {

                foreach (int i in values)

                {

                    Console.WriteLine("displayvalues {0}", i);

                }

            } 

          

            static void Main(string[] args)

            {

                Program a = new Program();

                a.displayvalue(3, 4, 5, 8, 9);

                 int[][] jagarray;

                jagarray = new int[3][] { new int[4], new int[6],new int[8] };

                jagarray[0][3] = 34;

                jagarray[2][2] = 54;

                

            }

        }

    }

    里面还有个关键字params,聪明的读者能想到它是干什么的吗?若急切想知道答案,可在博客下方评论。。

     

    此外,还有一种直接定义数组的方法,还能确定数组的上界和下界。

    public class SAB

        {

           public static void CreatArrayWithBounds(){

               int []lengthsArray = new int[2]{3,5};

               int []boundsArray = new int [2]{2,3};

               Array mutiDimension = Array.CreateInstance(typeof(string),lengthsArray,boundsArray);

               Console.WriteLine("Bounds:\tLower\tUpper");

               for(int i=0;i<mutiDimension.Rank;i++)

               {

                    Console.Write("Rank: {0}\t",i);

                    Console.Write(mutiDimension.GetLowerBound(i)+"\t");

                    Console.Write(mutiDimension.GetUpperBound(i)+"\n");

                     

     

               }

           }static void Main()

            {

                SAB.CreatArrayWithBounds();

             }

           

        } 

     

    这里维度的概念很重要,在这个例子中,维就是行和列。Rank指的就是数组的维度,这种指明界限的数组,使越界的风险大大降低。

  • 相关阅读:
    二维码登录网页版微信的实现方式分析
    扒取网站内容(后台方法和前台方法的两种实现)
    网站基本架构模式以及优化方案
    JS 日期对象在浏览器间的若干差异
    Web Farm 和Web Garden
    邓白氏申请和查询
    Xcode 真机调试报错:This application's application-identifier entitleme
    libc++abi.dylib`__cxa_throw: 使用[AVAudioPlayer play]会产生__cxa_throw异常
    ios 瀑布流的那些事情
    ios UIImage 圆形图片剪切方案
  • 原文地址:https://www.cnblogs.com/shenyuelong/p/4395882.html
Copyright © 2011-2022 走看看