Dark Horse programmer _ object-oriented Basics

Source: Internet
Author: User

1. Classes and objects

Object-oriented technology, also known as oop. As mentioned above, it is a programming idea. Since it is an idea, it applies to any language that supports object-oriented programming. Although different object-oriented languages have their own characteristics, they are still inseparable.

1. What is a class?

First, I want to understand several concepts. What is a class? This is actually easy to understand. In reality, we often say that things are clustered and divided by groups. The classes here are similar in concept to the ones I want to explain. They are all things in reality that share common attributes and characteristics. Like humans, dogs, cats, and so on, they are generally abstract descriptions. A type of thing does not refer to a specific person, person, dog, or cat, this is a general term for them.

Definition Format:

Class <Class Name> {

// Member in the class

}

Class Name definition, using the Pascal naming method. The class is defined by the class keyword.

2. What is an object?

After learning about the class, the object is easy to understand. It is a specific instance generated by the class. For example, the human class is a collective name of humans, and the object generated by the class of employing people refers to a specific person. This object has all attributes of the class of people, because the object is a specific instance of the class.

The Code demonstrates how to generate objects of another class in one class:

// Define a class first

Class person {

// Member in the class

}

Class program {

Static void main (string [] ARGs ){

Person <Object Name>; // declares the class variables, but does not instantiate them.

<Object Name> = new person (); // call the constructor of the class null parameter and instantiate it.

}

}

Class instantiation, that is, the object of the generated class, is called differently. Class instantiation uses the New Keyword to call the class constructor. Because of my person class, no constructor is written, during compilation, the compiler will automatically add a constructor with null parameters. If you write the constructor, it will not be added and you will use your constructor.

The two lines of code can also be abbreviated as one line, depending on the needs;

3. Relationship between classes and objects

A class can generate countless objects, and an object can only be generated by one class. A class can contain multiple instances, but an example must correspond to only one of its classes;

 

2. attributes, fields, and methods

These things are all members of the class. At the beginning, I was very confused. Attribute often thought it was a variable, but later I realized that it was used to maintain fields.

1. What is a field?

A field is used to describe the features of a class. For example, a person in a class has common features such as name, age, and Gender. We can use fields to define these features.

Definition:

<Permission modifier> <static modifier> <field type> <field Name>;

During the definition, you can also initialize it directly. Private Fields use the camper method. The Pascal method is used in total, but most of them are private. The permission is private by default. Static modifier, which can be selectively added;

The field can also be modified by the readonly keyword. It can only initialize fields in constructors and definitions.

2. Attributes

In my opinion, the attribute is the field modifier. Because Object-oriented has a feature, that is, fields, the required permissions, should be private as much as possible, and do not allow casual modification, and attributes are provided for us, a green channel, you can use it to modify the value of a field.

Definition:

Public<Return value type> <attribute name>{

Set {

<Field> = value;

}

Get {

Return <field>;

}

}

This is a standard property definition method. The two fields are the same field, which looks a bit like a method, but there is no parameter list, which is the biggest difference. And its permissions must be non-private. Otherwise, it will be lost and its meaning will exist. If one of them is omitted, it will become a read-only or write-only attribute;

Automatic property Definition Format:

Public <attribute name> {set; get ;}

To create an automatic attribute, you do not need to create the field to be modified. Instead, you can directly create the attribute. Other compilers will complete this process during compilation. However, there is a restriction that read-only or write-only attributes cannot be defined;

3. Method

The method is the function we mentioned earlier, but it is called differently. It is a member of the class.

 

3. In-depth Constructor

The constructor can also be overloaded. Just like the normal function overload, the function name is the same and the parameter list is different. The overload of constructors can provide us with methods for different initialization class instances. In addition, the constructor also mentioned above. If no constructor is written, the compiler will add it to us. If it is written, you will use your constructor. If you only write one constructor with several parameters, the class can only pass the fixed parameters during initialization, which will be quite limited, we all write a constructor with null parameters and several constructor with parameters;

In addition, there are static constructors and static classes.

1. What is a static constructor?

It is a little different from the definition of constructor. Although it is also in the class, it is defined as follows:

Static {

// Static constructor code block

}

Its role is mainly initialization. Static variables in the class give priority to objects. When loading classes and generating objects, static constructors are called first;

2. Static class

As the name suggests, a class modified by static has a feature that all its members must be static and cannot be instantiated. Since it cannot be instantiated, no constructor is required, but it can have a static constructor.

 

4. Static members

As mentioned earlier, static members. Such as static functions and variables. I just said, its role does not explain why.

Concept

Static is relative to dynamic, which means static. Once a static member is defined in a class, you need to think that it is bound to the class. No matter how many objects are generated by this class, when using static members of this class, they all use this Member in this class. Instead of static members in a single object. Because static members belong to a class and do not belong to a specific object, as long as they are the objects generated by the class, they can access static members in the class;

 

5. Object-oriented features

Object-oriented has three key features: encapsulation, inheritance and polymorphism, and important features such as abstract classes and interfaces;

1. Encapsulation

As mentioned above in the standard format of the definition class members, the permission modifier is used. The permission modifier is one of the most important manifestations of encapsulation. Encapsulation means that, on the basis of ensuring the normal operation of the program, try to reduce the access permissions of members in the small class.

Permission Modifier

PRIVATE: The minimum permission (default permission). The Members defined by this permission can only be accessed in this class;

Protected: It can only be accessed by the derived classes of this class. What is a derived class;

Internal: Internal permission, which can be accessed by classes in a project;

Public: The maximum permission, which can be accessed anywhere. It is usually used in attributes;

2. Inheritance

Inheritance is extremely important in object orientation. It can minimize repeated code for us. It means that a class can inherit its members from a class. Just as the son inherits Lao Tzu's heritage.

3. Polymorphism

Polymorphism is based on inheritance and can make programs more flexible. It means that a variable can have multiple states, not only accessing itself, but also accessing its base class;

4. abstract class

Abstract refers to being vague and describing a thing, which is very general. Abstract classes are like this. In reality, some things are not so specific. Therefore, it is necessary to abstract classes in programming. For example, a car class is very abstract. It can also be derived from a car or train class .., A very abstract thing like this, and is born to be used to inherit classes, called abstract classes.

Features: the class can contain abstract members or non-Abstract members. After a class inherits it, if it does not overwrite the abstract members in it, the class will be abstract. The class is not abstract only when all abstract members are covered;

5. Interface

An interface is a protocol that provides a standard for our programs. Just like a USB interface, it is a data transmission protocol. As long as you comply with my transmission protocol, USB flash drives, card readers, and mobile phones, you can connect to your computer. This is of great significance to the later maintenance of the program. If the program needs to be upgraded, we do not need to delete the rewrite code. We can add an Extended Interface Based on the original code, seamless program upgrade.

Most of this article is a conceptual Summary of the text. Let's take a look at it first. The next article is more about code demonstration.

Dark Horse programmer _ object-oriented Basics

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.