一、相關概念:
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("方法聲明"); }
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(); } } }
四、屬性
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(); Console.WriteLine("交換之前:x={0},y={1}",a,b);
test.Method(ref a,ref b); } } }
3、輸出參數:從方法傳遞回一個結果
關鍵字:out
例:
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(); } } }