類中的特性、函式實現
阿新 • • 發佈:2018-12-18
類中的特性與函式實現
關於特性
特性定義方法
- 修飾符 資料型別 變數名;
利用屬性來訪問特性
- 為什麼需要屬性?通常我們把特性設定為私有變數,一般可以通過函式來進行修改和讀取,需要一個函式來讀取,一個函式來寫。但這樣太麻煩了。
因此有了屬性,屬性本質上也是函式,但是可以用 物件.屬性名 進行訪問。 - 語法:
public 型別 屬性名
{
get{return ..}
set{..=value}
}
- 例項:年齡約束在0-150,這個範圍外都認為錯誤
class Test
{
private int age;
public int Age
{
get
{ return age; }
set
{
if (value > 0 || value < 150)
age = value;
else
Console.WriteLine("Age的範圍是0-150");
}
}
}
class 使用屬性
{
static void Main(string[] args)
{
Test test = new Test();
test.Age = -2;
test.Age = 3;
Console.WriteLine(test.Age);
}
}
關於函式屬性
建構函式與解構函式
1. 函式 | 呼叫時間 | 返回型別 |
---|---|---|
建構函式 | 建立物件時呼叫 | 無返回型別 |
解構函式 | 被垃圾回收機制回收時呼叫 | 無返回型別 |
一般的物件在程式結束時才釋放物件
- 語法
(1)public 類名(引數)
(2)public ~類名(引數)
函式過載
要求函式名相同,引數的型別、數目、順序不同。
注意僅返回型別不同,不叫過載,屬於一種錯誤。
函式引數型別
型別 | 效果 | 關鍵字 | 呼叫方法 |
---|---|---|---|
值型別 | 不會改變原本變數內容 | 預設就是這種型別,不需要關鍵字 型別 變數名 | 函式名(引數) |
引用型別 | 會改變原本變數的內容 | ref 型別 變數名 | 函式名(ref 變數名) |
輸出型別 | 引數可以沒有初值,用來改變原本變數的內容 | out 型別變數名 | 函式名(out 變數名) |
- 例項
class Canshu
{
public void ZhiChuanDi(int a ,int b)//不會修改
{
a += 1;
b += 1;
}
public void YinYongChuanDi(ref int a,ref int b)//會修改
{
a += 1;
b += 1;
}
public void ShuChuCanShu(out int c , out int d)//會賦值
{
c = 10;
d = 20;
}
}
class 函式引數型別
{
static void Main1(string[] args)
{
Canshu test = new Canshu();
int a = 0, b = 0;
test.ZhiChuanDi(a, b);//值傳遞
Console.WriteLine("{0},{1}", a, b);
test.YinYongChuanDi(ref a,ref b);//引用傳遞必須加ref
Console.WriteLine("{0},{1}", a, b);
test.ShuChuCanShu(out a, out b);//輸出傳遞,可以給已經有值的變數賦值
Console.WriteLine("{0},{1}", a, b);
int c, d;
test.ShuChuCanShu(out c, out d);//輸出傳遞最顯著的特點:可以傳遞未賦值的變數
Console.WriteLine("{0},{1}", c, d);
Console.ReadLine();
}
}
泛型函式,泛型類
背景:我們在建立函式時候需要指定引數的型別,比如要寫一個函式傳入三個引數,可以在螢幕上輸出它,對於不同的資料型別,都需要這個函式名起作用,這樣就會非常的麻煩,要做許多過載函式。
但是我們模糊的把他們當作一個不確定資料型別,只要三個資料型別滿足某些特定的規則就可以用一個函式對各種情況起作用。泛型就是這樣的作用。
(實際上,可以把引數型別設定為object,所有型別的引數都可以傳入)
1.泛型函式:在建立函式時指定一種未知的資料型別名字為。。函式內部就可以使用這種型別
(1)函式建立時:修飾符 返回型別 函式名<泛型>(泛型或特定資料型別 引數)
(2)函式呼叫時:函式<指定型別>(引數)
2.泛型類:在建立類的時候指定,類內部就可以使用這種型別
(1)類建立時:修飾符 class 類名<泛型>{}
(2)用類建立例項時:類名 <指定型別>=new 類名 <指定型別>(建構函式的引數)
3.例項:實現各種資料型別的列印,泛型類和泛型函式可以結合使用
class Fanxing<X_type, Y_type>
{
public X_type x;
public Y_type y;
public Fanxing(X_type x, Y_type y)//此處的建構函式就用了泛型
{
this.x = x;
this.y = y;
}
public void FanXingFunc<Z_type>(X_type x, Y_type y, Z_type z)//泛型函式
{
Console.WriteLine("{0},{1},{2}", x, y, z);
}
}
class 泛型類
{
static void Main1(string[] args)
{
//兩種不同型別的泛型類物件。
Fanxing<int, int> test1 = new Fanxing<int, int>(1, 3);
//泛型類在建立例項的時候指定型別
Console.WriteLine(test1.x+ test1.y);
Fanxing<double, int> test2 = new Fanxing<double,int>(1.2, 3);
Console.WriteLine(test2.x+test2.y);
test1.FanXingFunc<int>(1, 3, 2);
//泛型函式在使用函式時指定型別
}
}
索引器
如何自制一個索引器
1.語法:內部需要像屬性一樣設定
public 返回的資料型別 this[index]
{
get
{return ...}
set
{...=value}
}
本質是一個函式,只不過可以用 物件名[索引] 來呼叫
2.例項:利用索引器實現物件內部資料修改
class SuoYinQi
{
private int[] nums;
public SuoYinQi(int num1, int num2, int num3)//建構函式
{
nums = new int[3] { num1, num2, num3 };//初始化陣列
}
public int this[int index]//必須用屬性訪問
{
get
{
return nums[index];
}
set
{
nums[index] = value;
}
}
}
class 索引器
{
static void Main(string[] args)
{
SuoYinQi test = new SuoYinQi(1, 2, 3);
Console.WriteLine("{0} {1} [2]",test[0], test[1], test[2]);
test[0] = 3;//修改了第一個元素
Console.WriteLine("{0} {1} [2]",test[0], test[1], test[2]);
}
內建索引器
1.介紹:與java的列表相似,可以直接用物件[索引]訪問。並且內建了許多函式可以直接呼叫
2.語法:
(1)建立時:List <型別> 列表名=new List<型別> ()
(2)使用:列表.函式
3.例項
static void Main1(string[] args)
{
List<string> names = new List<string>();
names.Add("Daming");
names.Add("LiLi");
names.Add("Sam");
int n = names.IndexOf("LiLi");//顯示特定元素的索引
Console.WriteLine(n);
for (int i = 0; i < names.Count; i++)//有多少個元素
{
Console.WriteLine(names[i]);
}
Console.ReadLine();//結束
}