If you want to access the methods or attributes of a class, you must first instantiate the class, and then use the object of the class to add a. Number for access. For example:
There is a user class and a class that processes passwords (encryption and decryption. After a user instance is not generated, the password must be encrypted and decrypted.
Using system; using system. collections. generic; using system. text; using system. security. cryptography; using system. io; namespace yys. csharpstudy. mainconsole. static {// <summary> /// user class /// </Summary> public class user {// key private string key = "20120719" used for encryption and decryption "; // The Private string ivalue = "12345678"; private string username; private string userencryptpassword; private string userdecryptpassword; /// <summary> /// user name /// </Summary> Public String username {get {return username ;}/// <summary> // user password, encrypted password /// </Summary> Public String userencryptpassword {get {return userencryptpassword ;}/// <summary> // user password, decrypted password /// </Summary> Public String userdecryptpassword {get {Des = new des (); this. userdecryptpassword = des. decrypt (userencryptpassword, key, ivalue); Return userdecryptpassword ;}} /// <summary> /// constructor /// </Summary> /// <Param name = "username"> </param> /// <Param name = "userpassword"> </param> public user (string username, string userpassword) {This. username = username; Des = new des (); this. userencryptpassword = des. encrypt (userpassword, key, ivalue );}} /// <summary> /// password processing class /// </Summary> public class des {// <summary> /// encrypted string /// </ summary> Public String encrypt (string sourcestring, string key, string IV) {try {byte [] btkey = encoding. utf8.getbytes (key); byte [] btiv = encoding. utf8.getbytes (IV); descryptoserviceprovider des = new descryptoserviceprovider (); Using (memorystream MS = new memorystream () {byte [] indata = encoding. utf8.getbytes (sourcestring); try {using (cryptostream cs = new cryptostream (MS, Des. createencryptor (btkey, btiv), cryptostreammode. write) {CS. write (indata, 0, indata. length); CS. flushfinalblock ();} return convert. tobase64string (Ms. toarray ();} catch {return sourcestring ;}} catch {} return sourcestring ;} /// <summary> /// decrypt the string /// </Summary> Public String decrypt (string encryptedstring, string key, string IV) {byte [] btkey = encoding. utf8.getbytes (key); byte [] btiv = encoding. utf8.getbytes (IV); descryptoserviceprovider des = new descryptoserviceprovider (); Using (memorystream MS = new memorystream () {byte [] indata = convert. frombase64string (encryptedstring); try {using (cryptostream cs = new cryptostream (MS, Des. createdecryptor (btkey, btiv), cryptostreammode. write) {CS. write (indata, 0, indata. length); CS. flushfinalblock ();} return encoding. utf8.getstring (Ms. toarray () ;}catch {return encryptedstring ;}}}}}
Call:
Class program {static void main (string [] ARGs) {user = new user ("yangyoushan", "000000"); console. writeline (string. format ("username: {0}", user. username); console. writeline (string. format ("encrypted password: {0}", user. userencryptpassword); console. writeline (string. format ("plaintext password: {0}", user. userdecryptpassword); console. readkey ();}}
Result:
There are two problems in the Code implemented by these two classes.
1. For each instantiated user, run
DES des = new DES(); this.userEncryptPassword = des.Encrypt(userPassword, key, ivalue);
That is, a des instance must be instantiated every time. This is not good. The instantiation of DES is only for calling its method, but it is not convenient to instantiate each call method, and it also increases the memory consumption.
2.
// Key private string key = "20120719" used for encryption and decryption; // The Private string ivalue = "12345678" Vector used for encryption and decryption ";
These two variables are used by each user instance and will not change. However, every time a user is instantiated, space must be allocated, which also consumes memory and is not reasonable in the object-oriented approach.
In this case, it is best to share the two methods of DES, And you can directly call them without instantiating them. For example, all math methods (math. Abs (1 );). The other is to set the key and ivalue variables in the user to public, and can also be accessed directly, and only one memory space is allocated. When the user is instantiated, no additional allocation is required.
This requires static keywords. Static means that Members are shared by a class. That is to say, the declared static member does not belong to the object of a specific class and belongs to all objects of this class. All class members can be declared as static and can be declared as static fields, static attributes, or static methods. However, here we need to differentiate between const and static. Const means that constants cannot change values while running the program. Static means that values can be changed at run time and change in one place, in other places, the accessed values are changed.
In this way, you can use static to optimize the above Code, as shown below:
Using system; using system. collections. generic; using system. text; using system. security. cryptography; using system. io; namespace yys. csharpstudy. mainconsole. static {// <summary> /// user class /// </Summary> public class user {// Key Private Static string key = "20120719" used for encryption and decryption "; // The Private Static string ivalue = "12345678"; private string username; private string userencryptpassword; private string userdecryptpassword; /// <summary> /// user name /// </Summary> Public String username {get {return username ;}/// <summary> // user password, encrypted password /// </Summary> Public String userencryptpassword {get {return userencryptpassword ;}/// <summary> // user password, the decrypted password /// </Summary> Public String userdecryptpassword {get {// use the static method and static field this. userdecryptpassword = des. decrypt (userencryptpassword, key, ivalue); Return userdecryptpassword ;}} /// <summary> /// constructor /// </Summary> /// <Param name = "username"> </param> /// <Param name = "userpassword"> </param> public user (string username, string userpassword) {This. username = username; this. userencryptpassword = des. encrypt (userpassword, key, ivalue );}} /// <summary> /// password processing class /// </Summary> public class des {// <summary> /// encrypted string /// </ summary> Public static string encrypt (string sourcestring, string key, string IV) {try {byte [] btkey = encoding. utf8.getbytes (key); byte [] btiv = encoding. utf8.getbytes (IV); descryptoserviceprovider des = new descryptoserviceprovider (); Using (memorystream MS = new memorystream () {byte [] indata = encoding. utf8.getbytes (sourcestring); try {using (cryptostream cs = new cryptostream (MS, Des. createencryptor (btkey, btiv), cryptostreammode. write) {CS. write (indata, 0, indata. length); CS. flushfinalblock ();} return convert. tobase64string (Ms. toarray ();} catch {return sourcestring ;}} catch {} return sourcestring ;} /// <summary> /// decrypt the string /// </Summary> Public static string decrypt (string encryptedstring, string key, string IV) {byte [] btkey = encoding. utf8.getbytes (key); byte [] btiv = encoding. utf8.getbytes (IV); descryptoserviceprovider des = new descryptoserviceprovider (); Using (memorystream MS = new memorystream () {byte [] indata = convert. frombase64string (encryptedstring); try {using (cryptostream cs = new cryptostream (MS, Des. createdecryptor (btkey, btiv), cryptostreammode. write) {CS. write (indata, 0, indata. length); CS. flushfinalblock ();} return encoding. utf8.getstring (Ms. toarray () ;}catch {return encryptedstring ;}}}}}
Running result:
However, pay attention to one problem. Generally, you can access static attributes or static methods outside the method. However, if a static method needs to access an attribute or method outside the method, the accessed attribute and method must also be static. Generally, attributes or methods can be used only after being instantiated. In static mode, the memory space is allocated directly during compilation, therefore, it is not allowed to call other properties or methods in a static method. Only static properties or methods can be called at the same time.
Code download: http://download.csdn.net/detail/yysyangyangyangshan/4438713