C # basic review 03,

Source: Internet
Author: User

C # basic review 03,

Continue to update... The next step is the object-oriented knowledge.

1. Object-oriented: Concept: using object-oriented programming can make programs more scalable and easy to maintain;

We describe a person's characteristics and behaviors in real life.
When describing an object in programming, we describe the attributes and methods of this object.

Objects are not abstract. They must be visible and tangible.

We further Abstract Some objects with the same attributes and methods and extract the concept of "class.
Class is the model that determines the attributes and methods that the object will have.

2. class:

Syntax:

 

[Access modifier] class name
{
Member;
......
}
Access modifier: public, public, and public.
Class: the key word of the declared class.
Class Name: To comply with Pascal naming rules
Member:
Fields, attributes, and methods.

 

Summary:
1) when writing a class, we need to create a new class file in the project.
2) in the class, the default access modifier is private, which indicates private and can only be accessed within the current class.
3) in the class, we can now write three types of members, fields, attributes, and methods.
4) after we have written a class, we need to create the class object called class instantiation.
5) classes do not occupy memory, but objects occupy memory.
6) custom classes belong to the reference type. Our objects are stored in the heap of the memory.

 

3. attributes:

1) We require that all fields in the class be marked as private, but after being marked as private, fields in the class cannot be accessed from outside.
2) There is no way to process the illegal value of the field
The attribute solves the preceding two problems:
Syntax:
[Access modifier] attribute type attribute name
{
Get {}
Set {}
}
Fields are protected and the values and settings of fields are limited.
1) determine when assigning values, that is, in the set Method
2) determine the value, that is, in the get method.

There are both get and set attributes, which are called readable and writable attributes.
Only get is called a read-only attribute, which means that we can only assign values to the attribute instead of assigning values to the attribute.
Only set is called "Write-only attribute", which means that we can only assign values to attributes, but not to attributes.

 

4: Meaning of the this keyword:

1) represents the object of the current class.
2) Call the constructor of this class.

 

4.1: member variables and local variables

Local variable: all the members in the method are local variables.
Member variables: variables in the class
The local variable scope is the current method, and the member variable scope is the current class. (The above "this" indicates this)

 

Summary:

Field: Data Storage
Attribute: protection field. Two methods are used: get and set.
Method: describe the behavior of this object
The relationship between a field and a property: the field is the property of a woman, that is, a man... You know)

 

5. constructor: constructor is a special method used to initialize objects.

Eg: [public] class Person

{

}

[Public] Person ()

{

}

There are two special features:
1) The constructor name is the same as the class name.
2) The constructor does not return values. Not even void.

Note:

If you do not specify any other constructor in the class, a constructor without parameters is created by default.
After you write a new constructor. Whether the newly written constructor has parameters or no parameters
Kill the default non-parameter constructor.

 

5.1 meaning of the new Keyword:

1) Open up space in the heap
2) create an object in the heap Space
3) Call the object's constructor for initialization

4) Hide the members of the parent class

 

6. Differences between static and non-static

1) members marked by static are called static members, and those not marked by static are called instance members or non-static members.
2) in a non-static class, both static members and non-static members can appear.
3) non-static members must use objects for calling, while static members must use class names for calling.
4) Why can't objects call static members?
Static members have opened up space in the memory as the program is loaded.
5) only static members are allowed in static methods, but static members or non-static members can be used in non-static methods.
6) All members of the static class must be static members.
7) Static classes cannot be created because they have no practical significance.

 

6.1: Why should I use static classes?

1) when this class is used as a tool class, you can consider writing it as a static class.
Console con = new Console ();
Con. WriteLine ();
Console. WriteLine ();
2) Static members share resources throughout the project.

 

 

7. Access Modifier

Public: public and accessible anywhere.
Private: private. It can only be accessed within the current class. If this class is exclusive, it cannot be accessed.
Internal: indicates that access can only be performed within the current project. If this project is released, the access fails.

(It is enough to use these three in total with five access modifiers)

 

8. Special strings

String is a very special type for us. There are two special reasons:
1) the string is non-mutable. Every time we revalue and splice the string, a new instance will be generated in the memory.
String s1 = "zhangsan ";
S1 = "Li Si ";
2) The string can be viewed as a char-Type Read-Only array.


8.1: String common methods:
ToCharArray (): converts a string to a char array.
New string (char []): converts a char array to a string.
Length: returns the number of characters in a string.
ToUpper (): converts a string to uppercase.
ToLower (): converts a string to lowercase.
Equals (): Compares strings. The case sensitivity of strings can be ignored.
Split (): we can cut out all the characters we don't want in the string.

 

9. Three features of object-oriented architecture:

9.1 encapsulation: encapsulate the repeated code in the program into a method. (You may have understood this)

9.2 inheritance: inheritance can solve the redundant code between classes.

We encapsulate all the members in a class separately. Let other classes inherit this class.

For example, if two classes are written: Person and Student, and the Student class inherits the Person class, the Student class is called a subclass or a derived class.The Person class is called the parent class or the base class.

The subclass inherits the attributes and methods of the parent class, and does not inherit the private fields of the parent class.

Note: The subclass does not inherit the constructor of the parent class. However, the subclass calls the constructor of the parent class without parameters by default.

Why do subclasses call constructors without parameters in the parent class?
1) We may initialize the parent class members in the constructor with no parameters in the parent class.
2) because child classes need to use members of the parent class, they need to create objects of the parent class.

(If we write a new constructor in the parent class, the default non-parameter constructor of the parent class will be killed.) solution:

1) re-write a non-parameter constructor in the parent class.
2) The displayed calling parent class has a parameter constructor. Use the keyword base

Base is different from this: base represents the reference of the parent class; this represents the object of this class.

Note: Child classes can use members of the parent class, but the parent class can only use its own members.

9.3. Two inherited features:

1) uniqueness: a subclass can only inherit from one parent class.
2) passed: the members in the parent class can be passed down from generation to generation.

 

 

10. Lee's conversion:

Two sentences:

1. Subclass can be assigned to the parent class (if a parent class is required as a parameter, we can pass a subclass)

Eg:

Class Person
{
Public void PersonSayHello ()
{
Console. WriteLine ("I am a parent class ");
}
}

Class Student: Person
{
Public void StudentSayHello ()
{
Console. WriteLine ("I am a student ");
}
}

Class Teacher: Person
{
Public void TeacherSayHello ()
{
Console. WriteLine ("I am a teacher ");
}
}

 

Student s = new Student ();

Person p = new Student ();

 

2. If the parent class contains a subclass object, you can convert the strong value of the parent class to the corresponding subclass object.

Eg:

Student s = (Student) p;

S. StudentSayHello ();

 

10.1, is and as: is and as both represent type conversion.

Is: if the conversion is successful, true is returned; otherwise, false is returned.

Eg:

Person p = new Student ();

If (p is Student)

{

(Student) p). StudentSayHello ();

}

Else
{
Console. WriteLine ("Conversion failed ");
}


As: If the conversion is successful, the corresponding object is returned; otherwise, null is returned.

Eg:

Person p = new Student ();

Student s = p as Student;
S. StudentSayHello ();
Console. ReadKey ();

 

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.