欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#類的使用示例

 更新時(shí)間:2014年04月28日 11:28:22   作者:  
這篇文章主要介紹了c#類的使用示例,還有我學(xué)習(xí)時(shí)的筆記,需要的朋友可以參考下

Notes:

數(shù)據(jù)private類型,大部分方法public類型;

如果有繼承或者相互引用,注意數(shù)據(jù)的公有還是私有,保證數(shù)據(jù)的只讀性質(zhì);

C#不能夠像C++一樣在數(shù)據(jù)聲明時(shí)調(diào)用構(gòu)造函數(shù),必須使用Myclass temp = new Myclass()來調(diào)用構(gòu)造函數(shù);

C#不支持多重繼承關(guān)系, 也就是說,一個(gè)派生類不允許有多個(gè)基類。簡單點(diǎn)就是,父親可能有好多兒子(父類可以派生出許多子類),但是一個(gè)孩子只能有一個(gè)爸爸(子類不允許多重繼承關(guān)系);

C#和其他面向?qū)ο笠粯?,重載,多態(tài),虛函數(shù),抽象類這些特性都有;

重載和C++中的重載毫無區(qū)別,只有一個(gè)問題是在重載大小比較運(yùn)算符(<, >=, >, <=, ==, !=)的時(shí)候,必須成對的重載;

虛函數(shù)需加virtual聲明,派生類中需要重寫,并且添加override聲明;

抽象函數(shù)需加abstract聲明,并且基類中沒有執(zhí)行代碼,只能在繼承類中添加;

如果需要傳出多個(gè)值,需要用到ref聲明或out聲明;

ref聲明的對象必須提前初始化;

out聲明的對象不需要提前初始化;

如果類中有靜態(tài)對象,可以使用靜態(tài)構(gòu)造函數(shù)對靜態(tài)對象進(jìn)行賦值操作。

Example:
ElemType.cs

復(fù)制代碼 代碼如下:

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

namespace Test_Class
{
    class ElementType
    {
        private int value;
        public ElementType() { value = 0; }
        public ElementType(int _v)
        {
            this.value = _v;
        }
        public void disp()
        {
            Console.WriteLine("value is {0}", value);
        }
        public void edit(int _v)
        {
            this.value = _v;
        }
        public int reff()
        {
            return this.value;
        }
    }
    class package
    {
        private ElementType x;
        private ElementType y;
        public package()
        {
            this.x = new ElementType();
            this.y = new ElementType();
        }
        /// <summary>
        /// 構(gòu)造函數(shù)
        /// </summary>
        /// <param name="_x">x</param>
        /// <param name="_y">y</param>
        public package(int _x, int _y)
        {
            this.x = new ElementType(_x);
            this.y = new ElementType(_y);
        }
        public void disp()
        {
            Console.WriteLine("package display");
            Console.WriteLine("\tx vaule is {0}", this.x.reff());
            Console.WriteLine("\ty vaule is {0}", this.y.reff());
            Console.WriteLine();
        }
        /// <summary>
        /// 修改值
        /// </summary>
        /// <param name="_x">x</param>
        /// <param name="_y">y</param>
        public void modify(int _x, int _y)
        {
            this.x.edit(_x);
            this.y.edit(_y);
        }
        public void copyto(ref package p)
        {
            p.modify(this.x.reff(), this.y.reff());
        }
    }
}

Program.cs

復(fù)制代碼 代碼如下:

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

namespace Test_Class
{
    class Program
    {
        static void Main(string[] args)
        {
            package p1 = new package(5, 5);
            package p0 = new package();
            p0.disp();
            p1.disp();
            p1.copyto(ref p0);
            p0.disp();
            p0.modify(10, 50);
            p0.disp();
            Console.ReadLine();
        }
    }
}

相關(guān)文章

最新評論