In C #, instantiation is the process of creating an object. The key word "new" is used to create the object.
In the C # Video, an example is to change the object password. Let's use this example to understand how to create and instantiate a class.
1. Declare a class that can judge whether the password is correct and change the password, and define related methods in the class.
Class Authentic // declare a class that determines whether the PassWord is correct and can change the PassWord {private string PassWord = "zhouzhou"; // define the PassWord public bool IsPasswordCorrect (string userPassword) in the class) // declare the correct method for determining the PassWord in the class {return (PassWord = userPassword )? True: false ;//? : Ternary operator to determine whether it is true} public bool ChangePassWord (string oldPassWord, string newPassWord) // declare the method for changing the PassWord in the class {if (oldPassWord = PassWord) {PassWord = newPassWord; return true;} else return false ;}
2. instantiate the class as follows:
Class Program {static void Main (string [] args) {Authentic simon = new Authentic (); // simon is the name of the authentic instance. Remember to add the bool done brackets after the class; done = simon. changePassWord ("zhouzhou", "zhoujiangxiao"); if (done = true) Console. writeLine ("Password Changed"); else Console. writeLine ("Password Change failed! ");}}
In this example, it is easy to understand that class instantiation is:
Class Name object name = new Class Name ()
3. The access modifier limits class access. :
Programming is very basic, but it is indeed very important.