C # learning notes (8) -- Defining class members,
1. Member Definition
Class Myclass {public int MyInt ;}
You can use the readonly keyword to indicate that this field can only be assigned during the execution of the constructor, or the value is assigned by the initialization statement.
The grace of the static member allows you to access it by defining its class (MyClass. MyInt)
2. Definition Method
class Myclass { public int MyInt; public string GetString() { return "Here is a string!"; } }
Private int myInt;
public int MyIntProp { get { return myInt; } set { } }
In this case, the myInt field is private and cannot be accessed by external members, but the get and set fields can be modified externally, provided that the attributes are common.
Set is a value assignment function, but set can be set in different ways through a series of operations. You can also add error warnings and so on.
Then, just like get and set, you can add a series of qualified keywords in front. For example
protected set { myInt = value;}
4. A demo
Create a MyClass. cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Exercise{ class MyClass { public readonly string Name; private int intVal; public int Val { get { return intVal; } set { if(value>=0&&value<=10) { intVal = value; } else { throw (new ArgumentOutOfRangeException("Val", value, "Val must be assigned a value between 0 and 10.")); } } } public override string ToString() { return "Name:" + Name + "\nVal:" + Val; } public MyClass(string newName) { Name = newName; intVal=0; } }}
Add in Main. cs
#region Using directivesusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;#endregionnamespace Exercise{ class Program { static void Main(string[] args) { Console.WriteLine("Creating object myObj..."); MyClass myobj = new MyClass("My Object"); Console.WriteLine("myObj created."); for(int i=-1;i<=0;i++) { try { Console.WriteLine("\nAttemp to assign {0} to myObj.val...", i); myobj.Val = i; Console.WriteLine("Value {0} assigned to myObj.Val.", myobj.Val); } catch(Exception e) { Console.WriteLine("Exception {0} thrown.", e.GetType().FullName); Console.WriteLine("Message:\n\"{0}\"", e.Message); } } Console.WriteLine("\nOutputting myobj.Tostring()..."); Console.WriteLine(myobj.ToString()); Console.WriteLine("myobj.ToString() Output."); Console.ReadKey(); } }}
Ii. High-level issues
1. Hide the base class Method
(1) When a non-abstract member is inherited from the base class, the implementation code is inherited. If the inherited members are virtualized, you can use override to overwrite the code. The code of the base class can be hidden no matter whether the inherited members are virtual or not.
public class MyBaseClass { public void DoSometing() { //Base implementation } } public class MyDeriveClass:MyBaseClass { public void DoSometing() { //Derived class implementation, hides base implementation } }
Although this code is running normally, there is still a waring, which can remind us whether we intend to hide this member. If you really want to hide this member, you can use the new keyword to explicitly indicate the intent.
public class MyDeriveClass:MyBaseClass { new public void DoSometing() { //Derived class implementation, hides base implementation } }
(2) If you override the methods in the base class, the base class methods in the derived class will be replaced, even through the base class type, the situation is the same.
public class MyBaseClass { public virtual void DoSometing() { Console.WriteLine("FUCK"); //Base implementation } } public class MyDeriveClass:MyBaseClass { public override void DoSometing() { Console.WriteLine("FUCK you!"); //Derived class implementation, hides base implementation } }
MyDeriveClass myObj = new MyDerivedClass();MyBaseClass myBaseObj;myBaseObj = myObj;myBaseObj.DoSomething();
Obviously, the operation result is FUCK you!
(3) You can also use methods that hide the base class to implement the functions that can be implemented by the write operation.
2. Call the override or hidden base class Method
(1) importance:
A. To hide the inherited public members of a derived class, you can still access its functions in the class.
B. Add the implementation code to the inherited Virtual Member, instead of replacing it with the new Execution Code.
(2) base keywords
You can use the hidden methods in the base class to use the base keyword.
base.DoSomething();
(3) this keyword
A. It can be used inside the class to reference the instance of the object.
B. Pass the reference of the current object instance to a method.
C. restrict local members.
return this.someData
3. nested Type Definition
class MyClass { public class myNestClass { public int nestedFlassField; } }
If the classes defined internally are common, they can be used externally, but a qualified name must be added, for example
MyClass.myNestedClass myobj = new MyClass..myNestedClass();
If the nested class declaration is private, or other access levels that are not compatible with the code that executes the instantiation, they cannot be accessed externally.