e心e意

导航

C#学习日志(类、参数、返回值、静态字段、属性、方法等)

类的综合使用

namespace x4
{
    class DaysTemp
    {
        public int High, Low;//申明实例字段
        public int Average()//申明实例方法
        { 
        return (High +Low)/2;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //创建两个DaysTemp实例
            DaysTemp t1 = new DaysTemp();
            DaysTemp t2 = new DaysTemp();
            t1.High = 76;
            t2.High = 75;
            t1.Low = 57;
            t2.Low = 53;
            Console.WriteLine("t1:{0},{1},{2}",t1.High ,t1.Low ,t1.Average ());
            Console.WriteLine("t2:{0},{1},{2}",t2.High ,t2.Low ,t2.Average ());
        }
    }
}

方法调用

namespace 方法调用
{
    class MyClass
    {
      
            void PrintDateAndTime()//申明方法
            {
                DateTime dt = DateTime.Now;//获取当前日期
                Console.WriteLine("{0}", dt);
            }
        static void Main(string[] args)//声明方法
        {
            MyClass mc = new MyClass();//实例化类
            mc.PrintDateAndTime();//调用方法
        }
    }
}

返回值

namespace ConsoleApplication1
{
    class MyClass
    {
        public int GetHout()//其中int为返回类型
        {
            DateTime dt = DateTime.Now;
            int hour = dt.Hour;
            return hour;//返回一个int
        }
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();
            Console.WriteLine("Hour:{0}",mc.GetHout());//方法调用
        }
    }
}

参数

namespace 参数
{
    class MyClass
    {
        public int Sum(int x, int y)
            //申明方法,括号中的内容为“形参”
        {
            return x + y;
        }
        public float Avg(float input1, float input2)
            //括号中的内容为“形参”
        {
            return (input1 + input2) / 2.0F;
        }
        static void Main(string[] args)
        {
            MyClass myT = new MyClass();
            int someInt = 6;
            Console.WriteLine("Newsflash:Sum:{0}and{1}is {2}",
                5,someInt ,myT.Sum (5,someInt ));
            //调用方法,括号中的(5,someInt)为实参
            Console.WriteLine("Newsflash:Avg:{0}and{1}is {2}",
                5,someInt ,myT .Avg (5,someInt ));
            //调用方法,括号中的(5,someInt)为实参
        }
    }
}

引用参数

namespace 引用参数
{
    class myClass
    {
        public int Val = 20;//初始化字段为20
    }
    class Program
    {
        static void MyMethod(ref myClass f1, ref int f2)
        {
            f1.Val = f1.Val + 5;//参数的字段加5
            f2 = f2 + 5;//另一参数加5
        }
        static void Main(string[] args)
        {
            myClass a1 = new myClass();
            int a2 = 10;
            MyMethod(ref a1 ,ref  a2 );//调用方法
        }
    }
}

参数数组

namespace 参数数组
{
    class MyClass
    {
        public void ListInts(params int[] inVals)
            //括号里为参数数组
        { 
        if ((inVals !=null)&&(inVals .Length !=0))
        for(int i=0;i<inVals .Length ;i++)//遍历数组
        {
            inVals[i] = inVals[i] * 10;
            Console.WriteLine("{0}",inVals [i]);//
        }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int first = 5, second = 6,third = 7;
            //申明三个int
            MyClass mc = new MyClass();
            mc.ListInts(first, second, third);
            //调用方法,括号里为实参
            Console.WriteLine("{0},{1},{2}",first,second,third);
        }
    }
}

方法调用

namespace 方法调用
{
    class MyClass
    {
      
            void PrintDateAndTime()//申明方法
            {
                DateTime dt = DateTime.Now;//获取当前日期
                Console.WriteLine("{0}", dt);
            }
        static void Main(string[] args)//声明方法
        {
            MyClass mc = new MyClass();//实例化类
            mc.PrintDateAndTime();//调用方法
        }
    }
}

可选参数

namespace 使用可选参数
{
    class MyClass
    {
        public int Calc(int a = 2, int b = 3, int c = 4)
           //括号里为可选参数
        {
            return (a + b) * c;
        }
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();
            int ro = mc.Calc(5,6,7);//使用所有显示值
            int r1 = mc.Calc(5,6);//为c使用默认值
            int r2 = mc.Calc(5);//为b和c使用默认值
            int r3 = mc.Calc();//使用所有的默认值
            Console.WriteLine("{0},{1},{2},{3}",ro,r1,r2 ,r3);
        }
    }
}

静态函数成员

namespace 静态函数成员
{
    class X
    {
        static public int A;//静态字段
        static public void PrintValA()//静态方法
        {
            Console.WriteLine("Value of A:{0}", A);
        }
    }
    /*静态函数成员独立于任何类实例
     即使没有类的实例,任然可以调用静态方法,
     静态函数成员不能访问实例成员。然而他们
     能访问其他静态成员*/
    class Program
    {
        static void Main(string[] args)
        {
            X.A = 10;
            X.PrintValA();
        }
    }
}

属性和关联字段

namespace 属性和关联字段
{
    class C1
    {
        private int TheRealvalue = 10;//后备字段,分配内存
        public int MyValue//属性:不分配内存
        {
            set { TheRealvalue = value; }//设置TheRealValue字段的值
            get { return TheRealvalue; }//获取字段的值
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            C1 c = new C1();
            Console.WriteLine("MyValue:{0}",c.MyValue );
            c.MyValue = 20;//使用赋值语句设置属性的值
            Console.WriteLine("MyValue:{0}", c.MyValue);
            Console.ReadLine();
        }
    }
}

自动实现属性

namespace 自动实现属性
{
    class C1
    {
        public int MyValue//申明属性,没有后备字段
        {
            set;
            get;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            C1 c = new C1();
            Console.WriteLine("MyValue:{0}",c.MyValue );
            //使用自动属性
            c.MyValue = 20;
            Console.WriteLine("MyValue:{0}",c.MyValue );
        }
    }
}

静态属性

namespace 静态属性
{
    class Trival
    {
        public static int MyValue { get; set; }
        public void PrintValue()
        {
            Console.WriteLine("Value from inside:{0}",MyValue );
        }
    }
    /*不能访问类的实例成员,不管类是否有实例,
     当从类的外部访问时,必须使用类名引用,
     而不是实例名*/
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Init Value:{0}",Trival.MyValue );
            Trival.MyValue = 10;
            Console.WriteLine("New Value:{0}",Trival .MyValue );
            Trival tr = new Trival();
            tr.PrintValue();
        }
    }
}

 

posted on 2014-05-10 20:46  e心e意  阅读(200)  评论(0)    收藏  举报