今天練習虛方法與多態,順便想在子類中使用父類的構造函數,沒有寫過類似的用法,也出了Bug,就上網找了資料寫了這篇來當筆記。閱讀更多»
標籤: Class
C#-Delegate
C# 中的委託(Delegate)類似於 C 或 C++ 中函數的指針。委託(Delegate) 是存有對某個方法的引用的一種引用類型變量。引用可在運行時被改變。閱讀更多»
C#類結構建議排列
類內部成員架構建議順序:
類 { //public 為外界能獲取成員 public 字段(欄位) public 屬性 public 方法 //private或protected 為內部私有成員 private 字段(欄位) private 屬性 private 方法 protected 字段(欄位) protected 屬性 protected 方法 }
C#單例
單例(Singleton Pattern)是一種設計模式,功能是確保一個類最多同時只存在一個實例(對象),並易於訪問或調用。
C#方法參數
定義方法時,在參數列表中定義的參數叫做形式參數(形參);調用方法時,在參數列表中傳遞的參數叫做實際參數(實參)。
Unity3d-C#可視化自定義class
using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor; [System.Serializable] public class People { public GameObject person; public string name; public int age; public float height; public Vector3 location; public int[] itemarray = new int[0]; } public class PeopleList : MonoBehaviour { public List<People> people_list = new List<People>(); // Use this for initialization void AddNew() { people_list.Add(new People()); } // Update is called once per frame void Remove(int index) { people_list.RemoveAt(index); } } [CustomEditor(typeof(PeopleList))] public class PeopleListEditor : Editor { PeopleList N; SerializedObject GetTarget; SerializedProperty TheNewList; int ListSize; void OnEnable() { N = (PeopleList)target; GetTarget = new SerializedObject(N); TheNewList = GetTarget.FindProperty("people_list");// Find the List in our script and create a refrence of it } public override void OnInspectorGUI() { GetTarget.Update(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.LabelField("Define the list size with a number"); ListSize = TheNewList.arraySize; ListSize = EditorGUILayout.IntField("List Size", ListSize); if (ListSize != TheNewList.arraySize) { while (ListSize > TheNewList.arraySize) { TheNewList.InsertArrayElementAtIndex(TheNewList.arraySize); } while (ListSize < TheNewList.arraySize) { TheNewList.DeleteArrayElementAtIndex(TheNewList.arraySize - 1); } } EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); //Or add a new item to the List<> with a button EditorGUILayout.LabelField("Add a new item with a button"); if (GUILayout.Button("Add New")) { N.people_list.Add(new People()); } EditorGUILayout.Space(); EditorGUILayout.Space(); for (int i = 0; i < TheNewList.arraySize; i++) { SerializedProperty TheNewListRef = TheNewList.GetArrayElementAtIndex(i); SerializedProperty Person = TheNewListRef.FindPropertyRelative("person"); SerializedProperty Name = TheNewListRef.FindPropertyRelative("name"); SerializedProperty Age = TheNewListRef.FindPropertyRelative("age"); SerializedProperty Height = TheNewListRef.FindPropertyRelative("height"); SerializedProperty Location = TheNewListRef.FindPropertyRelative("location"); SerializedProperty Itemarray = TheNewListRef.FindPropertyRelative("itemarray"); EditorGUILayout.LabelField("List (" + i.ToString() + ")"); Person.objectReferenceValue = EditorGUILayout.ObjectField("Person Object", Person.objectReferenceValue, typeof(GameObject), true); Name.stringValue = EditorGUILayout.TextField("Person Name", Name.stringValue); Age.intValue = EditorGUILayout.IntField("Person Age", Age.intValue); Height.floatValue = EditorGUILayout.FloatField("Person Height", Height.floatValue); Location.vector3Value = EditorGUILayout.Vector3Field("Person Location", Location.vector3Value); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.LabelField("Item Array"); if (GUILayout.Button("Add New Item", GUILayout.MaxWidth(130), GUILayout.MaxHeight(20))) { Itemarray.InsertArrayElementAtIndex(Itemarray.arraySize); Itemarray.GetArrayElementAtIndex(Itemarray.arraySize - 1).intValue = 0; } for (int a = 0; a < Itemarray.arraySize; a++) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Item Index (" + a.ToString() + ")", GUILayout.MaxWidth(120)); Itemarray.GetArrayElementAtIndex(a).intValue = EditorGUILayout.IntField("", Itemarray.GetArrayElementAtIndex(a).intValue, GUILayout.MaxWidth(100)); if (GUILayout.Button("-", GUILayout.MaxWidth(15), GUILayout.MaxHeight(15))) { Itemarray.DeleteArrayElementAtIndex(a); } EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); //Remove this index from the List EditorGUILayout.LabelField("Remove an index from the List<> with a button"); if (GUILayout.Button("Remove This Index (" + i.ToString() + ")")) { TheNewList.DeleteArrayElementAtIndex(i); } EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); } GetTarget.ApplyModifiedProperties(); } }
C#-Set與Get基本範例(2)
本篇是將Set、Get方法與enum做搭配的範例。閱讀更多»
C#-Set與Get基本範例
本篇為class中Set與Get基本範例。閱讀更多»
C#初探-類別(Class)、欄位(Filed)、方法(Method)(1)
本篇主要初步了解「類別」、「欄位」、「方法」的特性與結構。閱讀更多»
類別和結構 (C# 程式設計手冊)
類別 (Class) 和結構 (Struct) 是 .NET Framework 一般類型系統中的兩個基本建構。這兩個本質上都是資料結構 (Structure),封裝了一整組資料和行為以組成邏輯單元。資料和行為是類別或結構的「成員」(Member),各自包含自己的方法、屬性和事件等,本主題稍後會列出。閱讀更多»