23. It's time to learn C # object-oriented stuff,

Source: Internet
Author: User

23. It's time to learn C # object-oriented stuff,

In our previous code, we have been exposed to class and class methods of C. Our code is mainly placed in the Main method of the Program class. We have also created our own methods. We also use methods such as writeline of the Console. However, we still don't know exactly what a class is. Next I will introduce the class as well as object-oriented programming.

 

Almost everything in C # is related to classes. The program we write is composed of different classes. Each class can have different attributes and methods. How to design and use classes? This is what object-oriented programming will help us solve.

 

First, let's take a look at the most basic concepts in C # object-oriented programming.

1. object-oriented and process-oriented

Process-oriented programming is a very popular programming methodology before the emergence of object-oriented programming. (The software industry has changed rapidly. Something that was very popular in the past has not been long before as new things keep coming out .)

"Process-oriented" is to analyze the steps required to solve the problem, and then use functions to implement these steps Step by step. You can call these steps one by one.

"Object-oriented" refers to the decomposition of problematic transactions into various objects. The purpose of object creation is not to complete a step, it is used to describe the actions of a thing in the process of solving the problem as well as the interactions between things.

For example:

How to pack elephants into the refrigerator?

Process-oriented:
Three procedures are required to pack elephants into the refrigerator. These three processes can correspond to three functions.
1) open the refrigerator door
2) Put the elephant in
3) Close the refrigerator door
After these steps are completed in turn, the elephants can be loaded into the refrigerator. Object-oriented:
We analyzed how to put an elephant in the refrigerator and analyzed two objects: an elephant and a refrigerator.
The refrigerator requires three actions, namely, three methods. The second action is to interact with the elephant.
1) refrigerator. open the door for me. Refrigerator. Open Door ()
2) refrigerator. You can pack the elephant in for me. Refrigerator. Pack (ELEPHANT)
3) Close the door for me in the refrigerator. Refrigerator. Close ()
By doing these actions in turn, the elephant can be loaded into the refrigerator.

Process-oriented is actually the most practical way of thinking. Even the object-oriented method also contains the process-oriented idea. process-oriented is a basic method. it takes into account the actual implementation. the general process orientation is to step up and refine from top to bottom. therefore, the most important aspect of process orientation is the modular approach. compared with the process-oriented model, the object-oriented approach mainly targets objects, including attributes and behaviors. I tried to explain what is object-oriented and what is process-oriented. I wonder if you have figured it out. But I must give you some advice.
As a programmer, you can't find any objects in the face-to-face process.

You have to think about it.

2. Class class is the basic unit of C # object-oriented programming. A class can contain two types of members: fields and methods.

1) Fields of the Class represent the data (variables) processed in the class );

2) class methods represent the processing process of the data or are used to implement a specific function. The code in the method usually needs to access the data stored by the field.

Simply put, fields are variables, and methods are functions.

2.1 class definition

C # Use the class keyword to define the class:

class MyFirstClass{}

  

Here, MyFirstClass is the name of this class. 2.2 class members

The class definition also provides the definition of all the Members in the class, including fields, methods, and attributes.

All members have their own access level. Here we will first learn two simple and frequently used access level modifiers:


Public -- members can be accessed by any code.
Private -- members can only be accessed by code in the class (if no keyword is used, this keyword is used by default ).

If you are interested in other access levels, you can search for them online. If you cannot find it, contact me.

2.2.1 Field

Fields are defined using the standard variable declaration format and the modifier described earlier (initialization can be performed), for example:

class MyFirstClass{public int MyInt;}

 



class MyFirstClass{public int MyInt = 10;}

 

 

2.2.2 Method


The method is declared using the standard Function Format and Accessability modifier. For example:

Class MyFirstClass {public string GetName () {return "Thinking Jiang's basic C # programming basics ";}}

  

3. Object

In C # programming, objects are class instances. It sounds a little abstract. For example.

We have a Car-like property, such as the brand color of the Car inside. My car, your car is an object of this car class. The brand colors inside have specific values. For example, my car is an object, its brand is Mercedes (my dream), and the color is black. Your car is another object. Its brand is BYD and its color is silver.

In the C # program, how does one create an object? Suppose we already have a class named Car. The following statement: Car myCar = new Car (); // create an object named myCar and create an object named myCar. We call "new" the operator for creating objects in Java, or the new operator for short. Its Syntax format is: ClassName objectName = new ClassName (); where, ClassName -- class name. ObjectName -- Name of the object to be created. New -- creates an object operator. The object myCar has all the attributes and methods of the Car class. I can assign values to its attributes or operate the methods defined in the car class. Let's start with a specific example. You create a project. The solution name is Sloution23, the project name is Exercise23, and the project type is console program. Right-click the project Exercise23 and choose add> class. Enter the name of the new class as Car. After this class is created, click Car. cs and add the following code to it. Click Program. cs and add the following code to it: Running result Exercise    

1. Read this code, add comments to each statement, and explain its functions.

2. In the main method, create a new car class named yourCar in the same way as myCar. Assign values to all fields of yourCar. Output the corresponding value of this object in the dos window, and call the CalculateCurrentPrice () method to output the current value of the car.

3. Modify the Car class and add some new fields and methods. Modify the Main method to access these fields and methods.

4. Modify the public access delimiter of the Car class fields and methods to private. Run the program and see what the result is? What is the reason?

 

 

Pay-as-you-go answers to questions C # encountered during the course of getting started.

Zero-basic learning C # beginners, most problems can be solved through self-study or the Internet.

HoweverThere are still some problems that are not so easy to solve and become a stumbling block to your learning. In the end, it may even cause you to lose your learning experience. Generally, when you get started with C # programming, you will encounter more than 20 to 30 problems that you cannot easily solve.

What do you need most at this time?

Yes! A professional teacher will help you!

If you have any questions during this course, you can ask the author for help via qq or email.

Only 30 RMBYou can get three professional answers from the author of this tutorial. So you only need less than 300 yuan to successfully complete the C # entry, and then open the door to the software industry.

 

The payment method is very simple:

Scan the following QR code. Leave your QQ number or email address in the payment message.

 

Education is not cheap, but reliable! Truly smart people know how to spend money to make professional people serve themselves and improve their efficiency.

 

Instructor qq: 2040227296

Instructor email: 2040227296@qq.com

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.