The note of learning ASP. NET [16] about classes and objects and Encapsulation

Source: Internet
Author: User
In many test codes, many procedures are written in the main function to demonstrate our functions. However, we know that our program needs to use classes, use object-oriented methods for better design. Class is the basis of our development program. Class is the blueprint of an object and describes the common attributes and functions (methods) of the object ). A metaphor in many teaching courses is very vivid. if the object is a house, it is the design drawing of the house. We can use this design drawing to build many identical houses, this is the relationship between classes and objects. A class consists of attributes and methods. Class can contain static or instance attributes and methods.1. Attributes declared as static are shared by all objects of this class, that is, each object points to the same value. 2. By default, the attributes of the Instance type are independent of a single object, that is, the same instance attribute of each object can have different values. 3. to call a declared static method, you do not need to use the new keyword to explicitly create an object. You can directly call it by class, A declared static method can be regarded as a service provided by a class. For example, directory. createdirectory (); this method is used to create folders on our computer. This method is a static method. We do not have any existing folders for our operations. We just use the directory class to create folders for us. In addition, we need to note that the static method can be called directly by the class, and we did not create the instance-type attributes defined in those classes when calling the static method, therefore, we cannot use non-static attributes in the static method. 4. The default instance type method must be called by a specific object, that is, the new object must be called by a specific object after the new keyword is used. The following code can be better understood. Using system; using system. collections. generic; using system. text; namespace classesandobjects {class mycustomer {// attributes of the Instance type of the customer name. The default value is private string _ customername = defaultcustomname; private string _ customerid; // customer ID instance attribute public static string defaultcustomname = "unknow"; // default customer name static type attribute // class mycustomer constructor, this API is automatically called when a class is created. It is the same as the class name and does not need to define the return value. You can add
// Parameters. You can provide multiple constructors with different parameter types or different number of parameters.
// When creating an object, the type and number of input parameters are used to determine which constructor to use, that is, the overload. Public mycustomer (string customerid) {_ customerid = customerid;} // The following customerid and customername are not common methods. They are actually attributes of our class _ customname and
/// _ Mid encapsulation if the attributes of our class can be directly accessed by the outside world and its value can be set at will, it is very dangerous for our program. For example, if the user sets the customer's name to a number "123", it is obvious that the name is a number, therefore, we can use encapsulation to // restrict users' operations on the attributes of our class. The encapsulation contains the get and set keywords. For the encapsulation of _ customerid, We only provide get, // This _ customerid is actually a read-only attribute for the outside world, customername provides the simplest encapsulation of _ customname //. In actual use, we can judge the value in the set. Values are assigned only when they comply with certain rules.
//'S _ mmname attribute. Public String customerid
{
Get {return _ customerid ;}
} Public String customername
{
Get {return _ customername ;}
Set {_ customername = value ;}
} Static void main (string [] ARGs) {mycustomer Custa = new mycustomer ("aaaa"); // create an object, mycustomer custb = new mycustomer ("BBBB"); console. writeline ("Custa's mermerid is {0}", Custa. customerid); // output: Custa's customerid is AAAA console. writeline ("custb's mermerid is {0}", custb. customerid); // output: custb's customerid is BBBB // output: Custa's customername is unknow console. writeline ("C Usta's mermername is {0} ", Custa. customername); // output: custb's customername is unknow console. writeline ("custb's mermername is {0}", custb. customername); console. writeline (); // value the _ mermername attribute of the Custa object to Custa. customername = "Custa's name"; // here, you can call the class name to change the defaultcustomname value of the static attribute. Mycustomer. defaultcustomname = "Arvin"; // The default customer name of the created object will be "Arvin" mycustomer custc = new mycustomer ("CCCC") set in the previous line "); // output: Custa's mermername is Custa's name console. writeline ("Custa's mermername is {0}", Custa. customername); // output: custb's customername is unknow console. writeline ("custb's mermername is {0}", custb. customername); // output: custc's customername is Arvin console. writeline ("custc's mermername is {0}", custc. customername );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.