第三次作业

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myclass
{
    class myclass1
    {
        private int age;
        private int num;
        public myclass1()
        {
            age = 0;
            num = 0;
        }
        public myclass1(int a,int b)
        {
            age = a;
            num = b;
        }
        public void setage(int c)
        {
            age = c;
        }
        public void setnum(int d)
        {
            num = d;
        }
        public int getage()
        {
            return age;
        }
        public int getnum()
        {
            return num;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            myclass1 a = new myclass1();
            myclass1 b = new myclass1(1,2);
          
            b.setage(6);
            Console.WriteLine("{0} {1}", a.getage(),a.getnum());
            Console.WriteLine("{0} {1}", b.getage(), b.getnum());

        }
    }
}

 这是我写的一个简单的类,类中有一些简单的方法。个人感觉C#和C++在类的创建和书写上没有太大的差别。

特别的,由于我将age 和 num设置为private,所以我申明的对象person1和person2不能直接访问age和num,

必须通过我申明的方法来访问。如果将private改为public,这两个对象就可以直接访问。

 

c#的参数传递 (out,ref)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 参数传递
{

    class Qwer
    {
        public void GetTime(ref int h, ref int m, ref int s)
        {
            h = 12;
            m = 34;
            s = 56;
        }

    }
    class program
    {
        static void Main(string[] args)
        {
            int a = 1;
            int b = 2;
            int c = 3;
            Qwer qwer = new Qwer();
            qwer.GetTime(ref a,ref b,ref c);
            Console.WriteLine("{0} {1} {2}",a,b,c);
        }
    }
}

如果去掉ref,这会发现a,b,c 的值依然是1,2,3.如果把ref 改为out,运行结果与上图一样。

说明ref 与out是深拷贝,而系统默认的拷贝方式为浅拷贝。

posted @ 2015-04-01 13:11  微风&细雨  阅读(114)  评论(0编辑  收藏  举报