C #: Object-oriented

Source: Internet
Author: User

I. Differences and relationships between classes and objects

Ii. Category

1. Class Declaration

[Access modifier] class name {member ;.....}

For example:

Class student // declare a student Class {public string name; // attribute public char sex; public int age; public void sayHello () // method {Console. writeline ();}}

2. class member

1) field: for example, private string name;
(2) attribute: In layman's terms, it is to define a public variable with the get and set methods. This variable is used to protect a private variable, such as the name field above, you can right-click the encapsulation in the refactoring and then get appears.

For example

Private string _ name; // field public string Name // attribute {get {return _ name;} // readable set {_ name = value;} // writable}

(3) class methods

A. Common method
[Modifier] return type method name (parameter list)
{
Method body
}

B. Constructor
(A) constructor is A special method. Its name is the same as the class name and there is no return value. Even void can be omitted without writing.
(B) After we define a class, if we do not write the constructor method, the system will automatically add a default constructor without parameters, we can't see nothing in this constructor, but once we write a constructor, the default constructor without parameters added by the system will not be added to us.
(C) Advantages of constructor:
A. When assigning values to multiple attributes, you do not need to repeatedly write instance names.
B. You must assign a value to an attribute when creating an object.
C. initialize the read-only attribute when creating an object

D) The constructor can have parameters. When a new object is created, you can pass the parameters.

For example

Public Student (string name, char sex, int age) {this. name = name; this, Sex = sex; this. age = age;} static void Main (string [] args) {Student s = new Student ("James", "female", 18 );}

(E) constructor can be overloaded, that is, there are multiple constructor methods with different parameters. For example, the following code:

char sex; int chinese;int math;int english;public Student(string name, char sex, int age){    this. name=name;    this.age=age;    this.sex=sex;} public Student(string name,char sex,int age,int chinese,int math,int eglish)        {            this.name=name;            this.sex=sex;            this.age=age;            this.chinese=chinese;            this.math=math;            this.eglish=eglish;        }

3. access modifier for Class Members

Public: Any other code in the same assembly or other assembly that references the Assembly can access this type or member.

Private; Members of this class are accessed in this class. When defining variables or methods in the class, if no access modifier is written, the default access modifier is private.

Protected; can be accessed only by the class and the code in the derived class of this class

Internal; the Code or gas subclass code in the same assembly can be accessed. Other Assembly codes cannot.

Default access modifier

1): The default access type of the class or structure is internal.

2): all the members of the class are private by default.

3): The default Interface access character is internal.

4): the default access modifier for interface members is public, and other access modifiers cannot be used.

5): namespace. The Enumeration type members are public by default, and cannot be other access modifiers.

6): Delegate. The default value is internal.

7): All types in the namespace or at the top of the compilation unit. The default value is internal, which can be changed to public.

Iv. Static and non-static data members

1. the method or attribute of the class modified by Static is called Static data. Because only one memory space is stored in one class and shared among all object instances of the class, it does not rely on specific objects, so it is accessed directly through the class name, instead of accessing through the class object instance

2. Methods or attributes not modified by static, called non-static data, must be instantiated first, and then accessed by instance name, method name, Instance name, and attribute name.

5. this keyword
This keyword is used to represent the object itself. this keyword can be used to solve the problem of identical names.

string name;cha sex;int age;  public Student(string name,char sex,int age)        {            this.name=name;            this.age=age;            this.sex=sex;        }

The preceding Code parameter name and data member name are exactly the same. You can use the this keyword to identify the parameter and data member. In this case, this keyword points to the current object, therefore, this is the data member of the class, but this is the parameter.

Related Article

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.