The Third Assignment

这一周的东西虽然看上去很熟悉。。。。However,和C++还有JAVA的差距还是挺大的。。。。

 

1.首先类的定义中,成员变量还有函数默认是private这和之前C++还有Java是一样的。。

然而在C#中又多了internal和protected internal两个访问级别

2.C# 4.0 函数允许声明optional arguments.

class Program
    {
        static void Main(string[] args)
        {
            test(5); //这里只传进去了一个int a
            test(5, 3.1f);
        }
        static void test(int a,float b=5.5f)//如果没有参数b传入就默认b等于5.5,如果有传入就将其赋值给b
        {
            Console.WriteLine("the first arguments is : {0}, the first arguments is : {1}", a, b);
        }
    }

比如这样,可以给参数一个默认值,如果没有传入参数就取默认值。

那么问题来了。。。挖掘机。。。咳咳。。如果只是中间几个变量想给默认值怎么办?

static void Main(string[] 
{ Email a
= new Email(); a.SendMail("aaaa", "bbb"); a.SendMail("aaaa", "bbb", d: true); a.SendMail("aaaa", "bbb", true, true); } class Email { public void SendMail(string a,string b,bool c = true,bool d= false) { Console.WriteLine("{0}, {1}, {2}, {3}", a,b, c, d); } }

像这样,传入参数的时候用定义函数时的参数名:传入的参数即可。。。

 3.C#中多了ref和out这两个关键字。。。类似于C/C++中的&。。

其中在使用这两个关键字传入参数时,ref要求传入的参数必须有一个初始值。。

而out允许未初始化的变量传入,并在函数中进行初始化。。

只用说的不直观。。看代码。。

    class Program
    {
        static void Main(string[] args)
        {
            int h1=0,m1=0,s1=0;
            GetTime( h1, m1, s1 );
     
            Console.WriteLine("h1={0},m1={1},s1={2}",h1,m1,s1);
        }
        static public void GetTime( int h, int m, int s )
        {
            h = 12;
            m = 34;
            s = 56;
        }
        
    }

这是只把数值传入函数。。

     static void Main(string[] args)
        {
            int h1=0,m1=0,s1=0;  
            GetTime( ref h1, ref m1, ref s1 );
            Console.WriteLine("h1={0},m1={1},s1={2}",h1,m1,s1);
        }
        static public void  GetTime( ref int h, ref int m, ref int s)
        {
            h = 12;
            m = 34;
            s = 56;
        }

ref的正确使用方法。。。

        static void Main(string[] args)
        {
            int h1,m1,s1;  
            GetTime( out h1, out m1, out s1 );
            Console.WriteLine("h1={0},m1={1},s1={2}",h1,m1,s1);
        }
        static public void  GetTime( out int h, out int m, out int s)
        {
            h = 12;
            m = 34;
            s = 56;
        }    

out的正确使用方法。。。

 (overloading和overriding和之前一样就不再唠叨了。。

4.继承和多态。。。

基本定义和C++/Java一样。。只不过多了一些关键字。。。

比如sealed(感觉和Java里的final一样。。

sealed类不能被继承。。。

 

posted on 2015-03-30 18:09  Tiny丶  阅读(120)  评论(0编辑  收藏  举报

导航