Learn from each other C # object-oriented preparation <2> 〉,

Source: Internet
Author: User

Learn from each other C # object-oriented preparation <2> 〉,
Object-oriented (OOP → Object-Oriented Programming)

1. What is object-oriented?

An analysis method.
2. Object-oriented features:

Encapsulation (easy to implement/reuse/modify hidden code), inheritance, and polymorphism.
3. What is a class? What is an object? What is the difference between a class and an object? How to Write a car?

Class is a mold. It creates a mold for an object and is abstract.
Class is a type of data, user-defined data type

Class composition: field, attribute, method, constructor, etc.
The object is specific and is a specific instance of the class.

Objects have attributes (features) and methods (Actions)
Case: Create a student class, teacher class, and initialize internal fields of the class through the constructor
4. this current object
5. Relationship between classes and objects: classes are used to describe things.

Objects are the entities of such things.

A descriptive object describes the features (fields) and behaviors (methods) of an object)
For example:
Both instructor Yang and Liu Bo have names, gender, and age.
Instructor Yang has lectures and students have learning behaviors.

6. Differences between classes and objects:

Class is an abstract concept, just a template, for example: "person" Class

An object is a specific entity that you can see and feel.

"People" have the following attributes: name, gender, and age.
Zhang San is an object. His name attribute value: Zhang San gender attribute value: male age attribute value: 20 Zhang San will speak


An object can be called an Instance of a class)
Classes only occupy one block of memory, and each object occupies one block of memory.

7. Class Definition:

Definition Syntax:
[Access modifier] class name
{
Member;
......
}
Class can contain the definition and method of variables.

8. Class instantiation:

Class instantiation, with the keyword new
Syntax: class instance name = new class ();
Class member access:
Instance name. Attribute Instance name. Method Name ();
Fields, methods, and attributes (described later) can all be called the class Member. They all need to define the access level. The purpose of the access level is to control where members can be accessed, so as to "encapsulate" the object.
Access modifier: public private (internal protected)

9. Attributes

Public field modification.
Attribute definition. get; set;
The attribute is used to protect the corresponding fields and ensure that the read and assign values of fields meet the requirements.
Attributes can be divided into: read/write, read-only, write-only
Variables that allow external access must be declared as attributes.
Attribute nature-there are two methods.

10. Class Constructor

Constructor initializes the field attributes of an object.
Constructor is a special method used to create an object. The method name is the same as the class name, and there is no return value, even void is not required.
The constructor can have parameters. When a new object is added, the function parameters can be passed.
If no constructor is specified, the class has a default no-argument constructor. If a constructor is specified, no default constructor is available. If no constructor is required, you need to write the constructor by yourself.
Constructors can be overloaded, that is, there are multiple constructors with different parameters.
Recognition class diagram

Class Diagram and general class diagram in 11. c #

12. encapsulation:

Q: What is encapsulation?
The remote control is amazing when it comes out. You can click a button to change the TV station, switch the volume, and turn off the TV. The remote controller does not need to know how it is implemented (you only need to pay for it)
Similar to remote control, object-oriented encapsulation encapsulates the state and behavior of things in a class. People who use classes do not need to know how the class is implemented internally, you only need to call the attributes and method implementation functions. Just like you use remote control, you don't need to know how the TV is controlled, as long as you know that the button can be changed
Classes and objects are the embodiment of encapsulation.
Attributes encapsulate fields, methods, and fields into an object, encapsulate a bunch of single business code into a method, encapsulate a function into several classes, and have the same functions. the code is encapsulated into a program set (dll, exe ), and provides unified access interfaces. (Attribute name, method name, etc .)

13. This keyword

This:
1. As the object of the current class, you can call a member in the class. This. Member (call Member, yourself)
2. Call other constructors of this class. : This () (call the constructor yourself)

14. namespace-virtual Folder system

Namespace (namespace) is used to solve the problem of duplicate class names. It can be seen as a "class folder ".
If the code and the used class are in a namespace, no using is required.
There are two methods to call classes in different namespaces:
Write the full name namespace. Class Name
Using references the namespace before calling

Note:

1. The parameters declared in the parameter list of the method are opened up for storage in the stack during the call.

2. When a variable is a member variable of a class, the variable follows the Class Object in heap memory, but when the object reference is disconnected, and other garbage collectors.

3. When a variable is a local variable of a method, the variable exists in the stack memory when the method is called, and is destroyed immediately after the method is executed.

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.