C#對象與方法
一、相關概念:
1、對象:現實世界中的實體
2、 類:具有相似屬性和方法的對象的集合
3、面向對象程序設計的特點:封裝 繼承 多態
二、類的定義與語法
1、定義類: 修飾符 類名稱 類成員
a)定義類語法:
修飾符 class 類名
{
類成員
}
2、類的訪問修飾符:public internal
a) public:可訪問域是所在的程序 和任何引用的程序 訪問不受限制
定義語法:
public class 類名
{
類成員
}
b) internal:可訪問域定義范圍內 (默認訪問修飾符)
語法:
(internal) class 類名
{
類成員
}
3、類成員:數據成員和字段
a) 數據成員:字段和常量
字段:變量
聲明:類型 字段名
例:
代碼如下 | 復制代碼 |
public class Persion { public string name; }
class Test { static void Main(string[] args) { Persion persion=new Persion(); persion.name="kaka"; Console.WriteLine("{0}",persion.name); } } |
b) 方法成員
聲明:
修飾符 返回值類型 方法名(參數列表)
{
方法體
}
修飾符:如:public、PRivate、protected、internal
返回值類型:若方法無返回值,則使用 void
例:
代碼如下 | 復制代碼 |
public void Method() { Console.riteLine("方法聲明 www.111Cn.net"); } |
4、成員的訪問修飾符:public、private、protected、internal
a) public:公有成員
b) private:私有成員
c) protected:保護成員
d) internal:內部成員
例:
代碼如下 | 復制代碼 |
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Consoleapplication2 { public class Employee { private float sum; public int day; public float wage; //定義方法輸出工資信息 public void Show() { sum = day * wage; Console.WriteLine("工作時間:{0},每天工資:{1},總工資:{2}",day,wage,sum); } } class Program { static void Main(string[] args) { Employee employee = new Employee(); employee.day = 20; employee.wage = 50; //employee.sum:無法訪問 因為它為私有成員 //調用方法現實工資 employee.Show(); } } } |
三、實例化對象:關鍵字:new
例:
代碼如下 | 復制代碼 |
?using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class car { private string carName; private string carType; private int price; public string CarName { get { return carName; } set { carName = value; } } public string CarType { get { return carType; } set { carType = value; } } public int Price { get { return price; } set { price = value; } } public void action() { Console.WriteLine("一輛名叫{0}車,型號是{1},價錢是:{2}",carName,carType,price); } } //創建實例并訪問字段和方法 class Program { static void Main(string[] args) { //創建car類的實例 car vehicle = new car(); //給實例賦值 vehicle.CarName = "奔馳"; vehicle.CarType = "XZ001"; vehicle.Price = 1000000; //調用方法 vehicle.action(); } } }
|
C#對象數組排序方法
排序是編程中常用的法算之一,排序的方法有很多種,下面介紹一種簡單有效的排序方法,代碼如下:
代碼如下 | 復制代碼 |
private bool isReverse = false; private void Sort(PersonalNotificationEntity [] list,string key) { if ( isReverse ) { Array.Reverse(list); isReverse = false; } else { int len = list.Length; Type type = typeof(PersonalNotificationEntity); object [] keys = new object[len]; for(int i = 0 ; i < len ; i++) { keys[i] = type.InvokeMember(key,BindingFlags.GetField ,null,list[i],null); } Array.Sort(keys,list); isReverse = true; } } |
這里使用了Array.Sort()和Array.Reverse()方法對數據進行正/反排序,變量isReverse做為反排序的標志位
方法傳入了2個參數,一個是要排序的對象數組list,一個是排序關鍵字key,即要對象的根據哪個屬性或字段來進行排序(這個值是等于對象的屬性/字段名)
type.InvokeMember()方法可以得到對象實例的屬性/字段值,這里使用的是字段
在得到數組中的每一個要排序的字段值后,把這個字段值數組做為Array.Sort()方法的參數傳入,Sort方法就會將對象數按這個字段的值進行排序
四、屬性
1、
a) 概念:用于訪問類的字段的成員
b) 屬性用途:保證數據安全 作數據的驗證
2、聲明:
訪問修飾符 數據類型 屬性名
{
get{}
set{}
}
3、get 訪問器
a) 含義:不帶參數,用于向外部指定字段的值,通常使用return 語句返回某個變量的值 在屬性取值時自動調用
b) get 訪問器的使用:
代碼如下 | 復制代碼 |
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication2 { public class Employee { private string name = "微微"; public string Name { get { Console.WriteLine("程序訪問了get訪問器"); return name; } } } class Program { static void Main(string[] args) { Employee employee = new Employee(); Console.WriteLine("訪問屬性之前"); Console.WriteLine(); Console.WriteLine(employee.Name); Console.WriteLine(); Console.WriteLine("訪問屬性之后"); } } } |
4、set 訪問器:返回值類型為void
5、屬性類型:
a) 讀/寫:有get()和set()訪問器的屬性
b) 只讀:有get(0訪問器的屬性,對成員提供讀取
c)只寫(不推薦使用):僅包含set() 訪問器
五、方法的參數
1、值參數:按值傳遞
例:
代碼如下 | 復制代碼 |
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication2 { public class Test { public void Method(int x,int y) { int temp = x; x = y; y = temp; Console.WriteLine("交換之前:x={0},y={1}",x,y); } } class Program { static void Main(string[] args) { int a = 2; int b = 5; Test test = new Test(); test.Method(a,b); Console.WriteLine("交換之后:x={0},y={1}",a,b); } } } |
2、引用參數:向方法傳遞實參在內存中的地址,按地址傳遞
例:
代碼如下 | 復制代碼 |
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication2 { public class Test { public void Method(ref int x,ref int y) { int temp = x; x = y; y = temp; Console.WriteLine("交換之前:x={0},y={1}",x,y); } } class Program { static void Main(string[] args) { int a = 2; int b = 5; Test test = new Test(); test.Method(ref a,ref b); Console.WriteLine("交換之后:x={0},y={1}",a,b); } } } |
3、輸出參數:從方法傳遞回一個結果
關鍵字:out http://www.111cn.net/net/net/46994.htm
例:
代碼如下 | 復制代碼 |
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication2 { public class Test { public void Method(int x,out int y) { y = x + x; } } class Program { static void Main(string[] args) { Test test = new Test(); int outy; test.Method(35, out outy); Console.WriteLine("y={0}",outy); } } } |
4、數組型參數:參數只允許是一組數組,當方法的參數前帶有params關鍵字時,就是帶數組型參數的方法(使用引用傳遞)
例:
代碼如下 | 復制代碼 |
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication2 { public class Test { public void Method(params int[] num) { Console.WriteLine("數組大小:{0}",num.Length); foreach (int i in num) { Console.Write("{0}",i); } Console.WriteLine(); } } class Program { static void Main(string[] args) { int[] nums = { 1, 2, 3, 4, 5 }; Test test = new Test(); test.Method(nums); test.Method(2,3,4,5); test.Method(); } } } |
補充 對象 方法 屬性有和區別
在面向對象里面,對象和類是不同的,對象是特定類的一個實例,比如如果車是一個類的話,某個人的一輛奔馳車就是一個對象,它是車這個類的實例。類是抽象的,而對象是具體的。方法是定義在對象上的操作,屬性是記錄對象性質和狀態的變量,拿上面車的例子來說,車的重量,最大速度是車的屬性,啟動,停在這些動作則可以定義為車的方法。我說的可能不太準確,建議樓主看看面向對象相關的書籍。
補充:
對象和類當然是不一樣的,對象是類的具體化(其實不準確),再打個比方吧,
告訴你貓是一個類,它包含兩個屬性,重量和毛色,
根據上面的信息你能知道是指是哪只貓嗎?不能吧,因為你不知道它的重量和毛色。 現在把貓實例化,即指定它的重量和毛色,假定為1kg、黑色,而這個1kg黑色的貓就是對象了,同樣,2kg白色的貓,3kg黃色的貓,等都是對象。
當然1kg黑色的貓也可以是作為一個類,為這個類加個主人屬性,
實例化類就得到對像,比如李四的(1kg黑色貓),張三的(1kg黑色貓)... 就是這個類的對象。
接著,李四的lkg黑色貓也可以成為一個類了,那這個類的對象呢,和上面一樣,加個能夠區分的屬性。
......
這樣就形成了類的層次結構了,然后父類,子類(派生類),繼承等概念都可以理解了。