c#之結構struct(2)_小記
2011年8月23日 星期二 13時33分
struct結構型別
public struct zxy //結構名稱有訪問修SHI符
{
public int length;//結構成員體也有訪問修SHI符
public int weight;
}
//結構是值型別
//FENG裝小型變數組
struct 型別是一種值型別,通常用來封裝小型相關變數組,例如,矩形的座標或庫存商品的特徵。 下面的示例顯示了一個簡單的結構宣告。
public struct Book
{
public decimal price;
public string title;
public string author;
}
備註
//結構包含建構函式,FIELD,METHOD,ATTRIBUTE,索引器,運算子,事件
//NESTED TYPE
結構還可以包含建構函式、常量、欄位、方法、屬性、索引器、運算子、事件和巢狀型別,但如果同時需要上述幾種成員,則應當考慮改為使用類作為型別。
結構可以實現介面,但它們無法繼承另一個結構。 因此,結構成員無法宣告為 protected。
using System;
public class St
{
private static int _s;
public struct zxy
{
public int length;//結構型別的成員也有訪問修SHI符,不然呼叫方沒法訪問它
public int weight;
//結構成員就是一個方法的宣告
//結構體成員的方法宣告須有訪問修SHI符
public void SetSt(int a)
{
length = a;//為結構體成員weight提供值
}
//結構成員是屬性訪問器
public int S
{
get
{
return St._s;//在結構體成員不能直接訪問非靜態類的成員
}
set
{
St._s = value;
}
}
}
//靜態方法用於改變結構成員的屬性訪問器set
public static void Chg_s(int _new)
{
_s = _new;
}
public static void Main()
{
zxy z;
z.length = 1;
z.weight = 3;
Console.WriteLine(z.length);
Console.WriteLine(z.weight);
Console.WriteLine();//回車換新行,其實就是搞一個新空行出來
z.SetSt(88);//結構體成員weight透過結構體成員方法由變為
Console.WriteLine(z.length);
St._s = 1234;
Console.WriteLine(St._s);
Console.WriteLine(z.S);//透過結構成員的屬性訪問器get返回_s的值為
St.Chg_s(666);
Console.WriteLine(z.S);
Console.ReadKey();
}
}
using System;
public class Test
{
struct Dimensions //結構宣告
{
public double length;
public double width;
//結構體成員為建構函式
//就是為結構初始化所用
public Dimensions(Double len, double wid)
{
length = len;
width = wid;
}
//結構其實是繼承於system.object基類,所以可以重寫object類的方法tostring
public override string ToString()
{
return "(" + length.ToString() + ";" + width.ToString() + ")";
}
}
public static void Main()
{
//要在main方法中呼叫類的結構,必須結構宣告為public不然難以訪問
Dimensions d = new Dimensions(1, 8);
Console.WriteLine(d.length);//輸出結構成員
Console.WriteLine(d.width);
Console.WriteLine(d.ToString());//呼叫結構成員的重寫object類之tostring方法
Console.ReadKey();
}
}
struct結構型別
public struct zxy //結構名稱有訪問修SHI符
{
public int length;//結構成員體也有訪問修SHI符
public int weight;
}
//結構是值型別
//FENG裝小型變數組
struct 型別是一種值型別,通常用來封裝小型相關變數組,例如,矩形的座標或庫存商品的特徵。 下面的示例顯示了一個簡單的結構宣告。
public struct Book
{
public decimal price;
public string title;
public string author;
}
備註
//結構包含建構函式,FIELD,METHOD,ATTRIBUTE,索引器,運算子,事件
//NESTED TYPE
結構還可以包含建構函式、常量、欄位、方法、屬性、索引器、運算子、事件和巢狀型別,但如果同時需要上述幾種成員,則應當考慮改為使用類作為型別。
結構可以實現介面,但它們無法繼承另一個結構。 因此,結構成員無法宣告為 protected。
using System;
public class St
{
private static int _s;
public struct zxy
{
public int length;//結構型別的成員也有訪問修SHI符,不然呼叫方沒法訪問它
public int weight;
//結構成員就是一個方法的宣告
//結構體成員的方法宣告須有訪問修SHI符
public void SetSt(int a)
{
length = a;//為結構體成員weight提供值
}
//結構成員是屬性訪問器
public int S
{
get
{
return St._s;//在結構體成員不能直接訪問非靜態類的成員
}
set
{
St._s = value;
}
}
}
//靜態方法用於改變結構成員的屬性訪問器set
public static void Chg_s(int _new)
{
_s = _new;
}
public static void Main()
{
zxy z;
z.length = 1;
z.weight = 3;
Console.WriteLine(z.length);
Console.WriteLine(z.weight);
Console.WriteLine();//回車換新行,其實就是搞一個新空行出來
z.SetSt(88);//結構體成員weight透過結構體成員方法由變為
Console.WriteLine(z.length);
St._s = 1234;
Console.WriteLine(St._s);
Console.WriteLine(z.S);//透過結構成員的屬性訪問器get返回_s的值為
St.Chg_s(666);
Console.WriteLine(z.S);
Console.ReadKey();
}
}
using System;
public class Test
{
struct Dimensions //結構宣告
{
public double length;
public double width;
//結構體成員為建構函式
//就是為結構初始化所用
public Dimensions(Double len, double wid)
{
length = len;
width = wid;
}
//結構其實是繼承於system.object基類,所以可以重寫object類的方法tostring
public override string ToString()
{
return "(" + length.ToString() + ";" + width.ToString() + ")";
}
}
public static void Main()
{
//要在main方法中呼叫類的結構,必須結構宣告為public不然難以訪問
Dimensions d = new Dimensions(1, 8);
Console.WriteLine(d.length);//輸出結構成員
Console.WriteLine(d.width);
Console.WriteLine(d.ToString());//呼叫結構成員的重寫object類之tostring方法
Console.ReadKey();
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-705623/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 瞭解下C# 結構體(Struct)C#結構體Struct
- C# 中的只讀結構體(readonly struct)C#結構體Struct
- golang 學習之路之 struct 結構體GolangStruct結構體
- struct 結構體 -Go 學習記錄Struct結構體Go
- C++ struct結構體記憶體對齊C++Struct結構體記憶體
- Solidity語言學習筆記————15、結構體StructSolid筆記結構體Struct
- struct結構體專案1Struct結構體
- struct結構體大小的計算(記憶體對齊)Struct結構體記憶體
- C# 中 Struct 和 Class 的區別總結C#Struct
- FFmpeg筆記2——2.2結構體分析之AVFormatContext筆記結構體ORMContext
- 重學c#————structC#Struct
- go 結構體 (struct) 和方法 (method)Go結構體Struct
- Golang 學習——結構體 struct (一)Golang結構體Struct
- Golang 學習——結構體 struct (二)Golang結構體Struct
- mysql表結構自動生成golang structMySqlGolangStruct
- 上下文 Context 與結構體 StructContext結構體Struct
- C#基礎之結構體講解C#結構體
- CSS 小結筆記之emCSS筆記
- CSS 小結筆記之定位CSS筆記
- CSS 小結筆記之背景CSS筆記
- CSS 小結筆記之BFCCSS筆記
- (2)caffe總結之目錄結構
- Golang中struct結構標籤(Tag)的使用GolangStruct
- rust學習六、簡單的struct結構RustStruct
- 精通C#學習筆記---C#核心程式設計結構C#筆記程式設計
- C#小筆記C#筆記
- Go 筆記之程式結構Go筆記
- CSS 小結筆記之浮動CSS筆記
- 【Golang】Go 通過結構(struct) 實現介面(interface)GolangStruct
- [swift 進階]讀書筆記-第五章:結構體和類 C5P3_結構體(struct)Swift筆記結構體Struct
- CSS 小結筆記之清除浮動CSS筆記
- CSS 小結筆記之盒子模型CSS筆記模型
- CSS 小結筆記之選擇器CSS筆記
- C語言中結構體struct的對齊問題C語言結構體Struct
- python實用小技之資料結構Python資料結構
- .Net(c#)使用 Kafka 小結C#Kafka
- JVM之記憶體結構詳解JVM記憶體
- CSS 小結筆記之圖示字型(IconFont)CSS筆記
- Android技能樹 — 網路小結(2)之TCP/UDPAndroidTCPUDP