3, object-oriented and simple application of WinForm (class of preliminary understanding)

Source: Internet
Author: User

What is a class?

A class is a basic unit of object-oriented programming, and a class typically consists of two members: Fields and methods-variables and functions.

Cases:

// definition of a field or variable  Public int Age ; // definition of a method or function  Public int ADD (int A,int  b) {  // Function statement  return  An expression;}

What is a constructor function?

The constructor of a class is called automatically when an object is created with a class as a template.
The constructor of a class is the same as the class name, but there is no return value, which is equivalent to re-giving the class an initialized method.

Cases:

class a{  // Class A constructor public    A ()  {    //  The contents of the constructor, in WinForm, the function that initializes the entire form is written, and then the other content that needs to be initialized   }}


Difficulties: reference types and value types

A variable of a value type is immediately available after it is defined, such as int i, which can be used.

After a variable of a reference type is defined, the object must also be created with the new keyword before it can be used. That is, the type used by the object.

In object-oriented, there is a very difficult question to understand: what does it mean for two object variables to be assigned to each other ?

We know that reference types are allocated in the managed heap, so if you assign two object variables to each other, it's actually the equivalent of pointing to the same object.

Cases:

1 // each time you new an object, it will be a fresh object: 2 New A (); 3 New A (); 4 Console.WriteLine (A1==A2); // false Output Result 5 // and when the two are assigned to each other, they point to the same object 6 a1 = = A2; 7 Console.WriteLine (A1==A2); // output is True

However, if you are a variable of a value type, they compare the contents of the variable to the object, because its memory area is the thread stack (understanding).

Access rights for class members

Mainly divided into three categories of protect/private/public.

Private: Can only be used by its own instance method.

Protect: is a protected, external class cannot access it, but its subclasses can access it.

Public: Publicly available, externally accessible.

3, object-oriented and simple application of WinForm (class of preliminary understanding)

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.