zoukankan      html  css  js  c++  java
  • C#100例之11~20

    11)装箱与拆箱:值类型与引用类型之间的转换.
    using System;

    class App
    {
        
    public static void Main()
        {
            
    long i = 100;    //这个是值类型,i是具体的有自己内存的变量

            
    //x是object类型的引用,它是一个引用类型,这里将数值类型i的值给了x指向的对象内存,即装箱,是隐式的.
            
    //等价于:object x = new object(i);
            object x = i;
            
    long j = (long)x;   //从引用类型赋值到数值类型就叫做拆箱.必须显式进行.
            Console.WriteLine(j);
            Console.Read();
        }
    }
    //装箱与拆箱是.NET平台最基本的操作,不过只需要知道这两种类型的差异与转换方式即可,不需要过多的加以注意.
    12)代表又称委托:即C++的安全类型的函数指针.本质还是指针
    using System;
    //声明一个委托(函数指针),这里的类型是无参数返回int类型的函数.此委托只能委托int function_name()类型的函数.
    delegate int MyDelegate();  

    class A
    {
        
    public int show()
        {
            Console.WriteLine(
    "class A's show method!");
            
    return 0;
        }
        
    public static int print()
        {
            Console.WriteLine(
    "class A's print method!");
            
    return 0;
        }
        
    public void sprint()
        {
            Console.WriteLine(
    "class A's sprint method!");
        }
    }
    class App
    {
        
    public static void Main()
        {
            A a 
    = new A();
            
    //在实例化代表对象时,将其与一个特定的类对象的方法关联了起来
            MyDelegate d = new MyDelegate(a.show);  //这样定义之后,可以说d就是a.show方法了.
            d();    //此时,代表对象d就是a.show()方法.两者在此时是一样的!
            d = A.print;    //将类A的静态方法也与代表对象d关联起来.
            d();
            
    //d = a.sprint;//这一步就不行,因为a.sprint()方法的返回值与委托的返回值类型不匹配.
            Console.WriteLine("---------------------");
            
    //可以将几个代表类型可以表示的方法连起来,形成方法链依次调用
            d += a.show;        //像这样,可以同时委托多个方法
            d();            //这里将同时调用两个方法
            Console.Read();
        }
    }
    //委托是C#的一大特色,它是使用函数指针链实现的,此外,C#的事件机制就是建立在委托的基础上的.
    13)运算符重载
    using System;
    class A
    {
        
    private int x;
        
    private int y;
        
    public A(int x, int y)
        {
            
    this.x = x;
            
    this.y = y;
        }
        
    public static A operator +(A p1, A p2)  //声明"+"号运算符重载
        {
            p1.x 
    += p2.x;
            p1.y 
    += p2.y;
            
    return p1;
        }
        
    public void show()
        {
            Console.WriteLine(
    "x = {0},y = {1}", x, y);
        }
    }
    class App
    {
        
    public static void Main()
        {
            A a 
    = new A(1245);
            a.show();
            A b 
    = new A(12);
            b.show();

            A c;
            c 
    = a + b;  //这里的"+"运算符,是重载过的指针类A对象操作的.
            c.show();
            Console.Read();
        }
    }
    //运算符重载本质上也就是方法的重载,不过它应用系统提供的运算父作为了方法名了.
    14)重载(多态)与覆盖的区别
    using System;

    class A
    {
        
    public void F()
        {
            Console.WriteLine(
    "A.F()");
        }
        
    virtual public void G()
        {
            Console.WriteLine(
    "A.G()");
        }
    }
    class B : A         //类B继承自类A
    {
        
    public new void F()         //使用new覆盖掉其基类的同名方法
        {
            Console.WriteLine(
    "B.F()");
        }
        
    public override void G()   //override是对其基类虚方法的重载而不是覆盖.
        {
            Console.WriteLine(
    "B.G()");
        }
    }
    class App
    {
        
    public static void Main()
        {
            B b 
    = new B();
            b.F();
            b.G();
            Console.WriteLine(
    "---------------------");
            
    //下面演示覆盖和重载虚方法的不同之处
            A a = b;    //注意这里,类A的变量a,但是其引用的对象是类B的对象b.
            a.F();      //差别就在这两个方法的调用上:a.F()调用的是类A的方法F();而a.G()调用的却是类B的方法G();
            a.G();      //这就new覆盖与override重载实现多态的差别:虚方法实现的多态,是老代码调用新代码.
            Console.Read();
        }
    }
    15)params关键字.与数组参数配合使用:如果形参中包含了数组型参数,那么它必须为参数表中位于最后.另外,参数只允许是一维数组:string[],string[][]都可以,string[,]不行.另,数组型参数不能有ref和out修饰符.
    using System;

    class App
    {
        
    static void F(params int[] args)    //以数组作为参数,使用params关键字,传递的参数个数将可以使任意的
        {
            Console.WriteLine(
    "此数组有{0}个元素", args.Length);
            
    foreach (int i in args) //遍历输出显示数组中的元素值
            {
                Console.Write(
    "{0} ", i);
            }
            Console.WriteLine();
        }
        
    public static void Main()
        {
            
    int[] a = { 1234 };
            F(a);
            F(
    11154212355787854);   //这里,传递的参数格式是任意个的,但是参数类型必须是int.        
            F();
            
    //F(11.2);              //如传递float则编译出错.
            Console.Read();
        }
    }
    16)sealed:,密封修饰符(不可重载不可继承).
    using System;

    sealed class A  //用sealed声明的类,除了不能被改变外,和一个普通的类没什么两样.
    {
        
    public void show()
        {
            Console.WriteLine(
    "show A");
        }
    }
    class B
    {
        
    public virtual void show()
        {
            Console.WriteLine(
    "show B");
        }    
    }
    class C : B//A    //这里不能继承声明了sealed封闭的类A,
    {
        
    public sealed override void show()      //这里用sealed修饰过的方法,也不可被继承下去了.
        {
            Console.WriteLine(
    "show C");
        }
    }
    class App
    {
        
    public static void Main()
        {
            A a 
    = new A();
            a.show();
            C c 
    = new C();
            c.show();
            Console.Read();
        }
    }
    //如string类型,就是系统提供的不可继承的类.

    17)new:1)创建类实例;2)创建数组实例;3)创建委托实例

    using System;

    delegate void MyDelegate(); //声明一个委托

    class A
    {
        
    int[] x;
        
    public void show()
        {
            
    this.x = new int[5];  //new创建了数组实例
            Console.WriteLine("show is called!");
        }
    }
    class App
    {
        
    public static void Main()
        {
            A a 
    = new A();  //new创建类的对象实例
            MyDelegate p = new MyDelegate(a.show); //new为委托创建一个实例,并绑定了一个方法
            p();
            
    int[] b = new int[10];//new来创建数组的实例
            Console.WriteLine(b.Length);        //显示此数组的元素个数
            Console.Read();
        }
    }
    18)base:基类说明符
    using System;

    class A
    {
        
    public void show()
        {
            Console.WriteLine(
    "show A");
        }
    }
    class B : A
    {
        
    public new void show()  //new 覆盖掉了基类的同名方法
        {
            Console.WriteLine(
    "show B");
            
    base.show();        //在内部,使用base调用基类的方法
        }
    }
    class App
    {
        
    public static void Main()
        {
            B b 
    = new B();
            b.show();
            Console.Read();
        }
    }
    19)abstract:抽象类声明
    using System;

    abstract class A    //abstract声明类时,此类为抽象类,即不可进行实例化操作
    {
        
    abstract public void show();    //抽象类必须有一个abstract方法
        public void print()
        {
            Console.WriteLine(
    "print A");
        }
    }
    class B : A
    {
        
    //继承抽象类的抽象abstract方法时,如果想要为此类实例化操作,必须用override进行覆盖
        public override void show()
        {
            Console.WriteLine(
    "show B");
        }
    }
    class App
    {
        
    public static void Main()
        {
            
    //A a = new A();    由于类A声明为抽象类,所以不能进行实例化操作
            B b = new B();
            b.show();   
    //类B实现了抽象基类A的抽象方法show.
            b.print();  //类B同时也继承了类A的非抽象方法.
            Console.Read();
        }
    }
    20)静态和非静态方法:非静态方法可以访问类中的任何成员,而静态方法只能访问类中的静态成员
    using System;

    class A
    {
        
    int x;          //非静态成员
        static int y;   //静态成员
        public void nstaticMethed()  //非静态方法
        {
            x 
    = 100;    //非静态方法访问非静态成员
            y = 200;    //非静态方法访问静态成员
        }
        
    public static void staticMethed()    //静态方法
        {
            
    //x = 1000;   //这里,静态方法不能访问非静态成员.
            y = 2000;
        }
        
    public void show()
        {
            Console.WriteLine(
    "x = {0},y = {1}", x, y);
        }
    }
    class App
    {
        
    public static void Main()
        {
            A a 
    = new A();
            a.nstaticMethed();  
    //由类的对象实例调用类的非静态方法
            a.show();
            A.staticMethed();   
    //由类调用类的静态方法
            a.show();
            Console.Read();
        }
    }
  • 相关阅读:
    ububtu 14.04 问题集合
    ubuntu grub 引导修复
    Ubuntu 下 glpk 的安装及使用
    ubuntu vim 7.4 编译安装
    ubuntu 12.04 clang 3.4 安装
    CMakeLists实战解读--YouCompleteMe
    Flume安装及部署
    SpringBoot整合kafka
    linux安装kafka
    Linux安装zookeeper
  • 原文地址:https://www.cnblogs.com/wppt/p/562246.html
Copyright © 2011-2022 走看看