類
類的定義是以關鍵字class開始的,后面跟類的名稱,類的主題包含一個花括號里,下面是類定義的一般格式。
<access specifier> class class_name { // member variables <access specifier> <data type> variables1; <access specifier> <data type> variables2; //member method <access specifier> <return Type> method1(parameter_list) { //method body } }
請注意:
- 訪問標識符<access specifier>指定了對類及其成員的訪問規則。如果沒有指定則使用默認的訪問標識符,類的默認訪問標識符是internal,類成員的默認訪問標識符是private。
- 數據類型<data type>指定了變量的類型,返回類型<return type>定義了返回方法的返回的數值類型。
- 如果訪問里面的成員,需要用(.)運算符
- 點運算符鏈接了對象的名稱和成員的名稱
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace test_unityc { class Box { public double length;//長度 public double breadth;//寬度 public double height;//高度 } class Box_tester { static void Main(string[] args) { Box box1 = new Box();//聲明box1,類型為Box Box box2 = new Box();//聲明box2,類型為Box double volume = 0.0f;//體積 //box1詳述 box1.height = 5.0; box1.length = 6.0; box1.breadth = 7.0; //box2詳述 box2.breadth = 10.0; box2.length = 12.0; box2.height = 13.0; //box1的體積 volume = box1.height * box1.breadth * box1.length; Console.WriteLine("box1的體積{0}", volume); //box1的體積 volume = box2.height * box2.breadth * box2.length; Console.WriteLine("box2的體積{0}", volume); Console.ReadKey(); } } }
當上面的代碼執行時,它會出現如下效果:
box1的體積210 box2的體積1560
成員函數和封裝
類的成員函數是一個在類的定義中有它的定義或原型的函數,就像其他的變量一樣。作為類的一個成員,它能在類的任意對象上操作,且能訪問該對象類的所有成員。
成員變量是類的屬性(從設計角度),且它們保持私有來實現封裝。這些變量只能使用公共成員函數來訪問。
現在用使用上面的概念來設置並獲取一個類當中不同類成員的值:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace test_unityc { class Box { private double length;//長度 private double breadth;//寬度 private double height;//高度 public void setLength(double len) { length = len; } public void setBreadth(double bre) { breadth = bre; } public void setHeight(double hei) { height = hei; } public double getVolume() { return length * breadth * height; } } class Box_tester { static void Main(string[] args) { Box box1 = new Box();//聲明box1,類型為Box Box box2 = new Box();//聲明box2,類型為Box double volume;//體積 //box1詳述 box1.setHeight(5.0); box1.setLength(6.0); box1.setBreadth(7.0); //box2詳述 box2.setHeight(12); box2.setLength(13); box2.setBreadth(14); //box1的體積 volume = box1.getVolume(); Console.WriteLine("box1的體積{0}", volume); //box1的體積 volume = box2.getVolume(); Console.WriteLine("box2的體積{0}", volume); Console.ReadKey(); } } }
運行結果:
box1的體積210
box2的體積2184
C#中的構造函數
類的構造函數是類的一個特殊的成員函數,當創建類的新對象是執行。
構造函數的函數名稱與類名稱完全相同,且它沒有任何返回類型。
下面的實例說明了構造函數的概念:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace week2._1 { class Program { private double line; // 線條的長度 public Program() { Console.Write("對象已經創建"); } public void setLength(double len) { line = len; } public double getLength() { return line; } static void Main(string[] args) { Program line = new Program(); //設置線條長度 line.setLength(6.0); Console.Write("線條的長度為{0}", line.getLength()); Console.ReadKey(); } } }
結果為:
對象已經創建線條的長度為6
默認的構造函數沒有參數,但是如果需要可以帶參數,這種構造參數叫做參數化構造參數,這種對象可以幫助你在創建對象的同時給對象賦初始值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace week2._1 { class Program { private double line; // 線條的長度 public Program(double len)//參數化構造參數 { Console.Write("對象已經創建,length = {0}",len); line = len; } public void setLength(double len) { line = len; } public double getLength() { return line; } static void Main(string[] args) { Program line = new Program(10.0); Console.WriteLine("線條的長度為:{0}", line.getLength()); //設置線條長度 line.setLength(6.0); Console.Write("線條的長度為:{0}", line.getLength()); Console.ReadKey(); } } }
結果:
對象已經創建,length = 10 線條的長度為:10 線條的長度為:6
c#中的析構函數
類當中的析構函數是類當中的一種特殊的成員函數,當類的對象超出范圍時執行。
析構函數的名稱是在類名稱前面加一個波浪號(~)做前綴,它不返回值,也不帶任何參數。
析構函數用於結束程序(比如關閉文件,釋放內存等)之前釋放資源,析構函數不能繼承或重載。
下面的實例說明析構函數的概念:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace week2._1 { class Program { private double line; // 線條的長度 public Program()//構造參數 { Console.WriteLine("對象已經創建"); } //析構函數 ~Program() { Console.WriteLine("對象已經刪除"); } static void Main(string[] args) { Program line = new Program(); } } }
結果:
對象已經創建
對象已經刪除
c#的靜態成員
我們可以使用static關鍵字把類成員定義成靜態的,當我們聲明一個類成員是靜態的時,意味着無論有多少個類的對象被創建,只會有一個改靜態成員的副本。
關鍵字static意味着類中只有一個該成員的實例,靜態變量用於定義變量,因為他們的值可以通過直接調用類而不需要創建類的實例來獲取,靜態變量可以再成員函數或類的定義外部進行初始化。你也可以在類的內部初始化。(靜態變量在外部可以通過 類.變量名 訪問 。實例變量通過創建實例對象在外部 對象名.變量名 訪問)
下面實例演示了靜態變量的的用法:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace week2._1 { public class Test { public static int num; public int number; public void addNum() { num++; } public int getNum() { return num; } } class Program { static void Main(string[] args) { Test.num = 2; Test test1 = new Test(); Test test2 = new Test(); test1.number = 1; test1.addNum(); test1.addNum(); test1.addNum(); test2.addNum(); test2.addNum(); test2.addNum(); Console.WriteLine(test1.getNum()); Console.WriteLine(test2.getNum()); Console.ReadLine(); } } }
結果:
8 8
你也可以把一個成員函數聲明為static。這樣的函數只能訪問靜態變量。靜態函數在對象創建之前就已經存在了。下面的實例演示了靜態函數(只能通過類名.方法名調用)的用法:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace week2._1 { public class Test { public static int num; public int number; public void addNum() { num++; number--; } public static int getNum() { return num; return number;//出錯 } } class Program { static void Main(string[] args) { Test test1 = new Test(); Test test2 = new Test(); test1.addNum(); test1.addNum(); test1.addNum(); test2.addNum(); test2.addNum(); test2.addNum(); Console.WriteLine(Test.getNum()); Console.WriteLine(Test.getNum()); Console.ReadLine(); } } }
結果:
6 6
將類成員函數聲明為public static無需實例化即可調用類成員函數
反之,如果不聲明為static,即使和Main方法從屬於同一個類,也必須經過實例化