现在的位置: 首页 > 综合 > 正文

C#语言—-对象和类型(学习总结)

2013年04月15日 ⁄ 综合 ⁄ 共 2096字 ⁄ 字号 评论关闭

第一部分: 

using System;
namespace basicCsharp
{
    class TestClass_1
    {
        private string foreName;
        static void Main()
        {  /* ref传递数据*/
            int[] ai = { 0, 1, 2 };
            int i = 1;
            Console.WriteLine("1="+i+",ai[0]="+ai[0]);           
            otherclass.change(ref i,ai); //如果使用ref来传递数据,则传其引用,那么方法里的改变会影响外面的变量
            Console.WriteLine("1="+i+",ai[0]="+ai[0]);
          
            /*out关键字*/ 
            int j;
            new TestClass_1().method1(out j);//out用于关键字初始化,是传递的数据可以不被初始化,传送起引用!         
            Console.WriteLine("j="+j);

            /*类的重载*/
            method2(1);
            method2(1, 2);
            method2(true);

            /*属性get or set*/
            TestClass_1 obj = new TestClass_1();
            obj.ForeName = "100";//类似于obj.setForeName(100) 千万不需要再写get or set了
            Console.WriteLine(obj.ForeName);//类似于obj.getForeName(100)

 

                      

        }
        public string ForeName
        {
            get { return foreName; }//省略get就可以实现只写方式  C#独有的get/set方法 前面还可以设定private等属性
            set { foreName = value; }//省略set就可以实现只读方式
        }
        void method1(out int j){
            j=100;
        }

        static void method2(int i)
        {
            Console.WriteLine("这是method2(1个参数的)");
        }
        static void method2(int i, int j)
        {
            Console.WriteLine("这是method2(2个参数的)");
        }
        static void method2(Boolean t)
        {
            Console.WriteLine("这是method2(类型不相同的)");
        }
    }

    class otherclass
    {
        static void main() { }
        public static void change(ref int i,int[] ai){ //这里一定要注意使用public 否则另外的一个类调用不到此方法
            i=100;
            ai[0]=200;
        }
    }
}

第二部分

using System;
using System.Drawing;
using System.DateTime;

namespace basicCsharp
{
    class TestClass_2
    {
        public static readonly Color backColor;//只读的backColor属性只能在静态构造函数中设定其值~!
        static TestClass_2()
        {
            DateTime now=new DateTime.Now;
            if (now.DayOfWeek==DayOfWeek.Saturday) || (now.DayOfWeek==DayOfWeek.Sunday)
            {backColor=Color.Green;} else {backColor=Color.Red;}
        }
    }
    class other
    {
        static void Main()
        {
            Console.WriteLine(TestClass_2.backColor.ToString());
        }
    }

}

 

抱歉!评论已关闭.