Object-oriented basic advanced 01 and object-oriented advanced 01

Source: Internet
Author: User

Object-oriented basic advanced 01 and object-oriented advanced 01

Object-Oriented Programming is now a big environment of today's programming, so it is necessary to review the object-oriented knowledge.

I. Class and Object

1: Software System

Input {User Interface} Operation {business logic}

Store {database} output {interface or other files}

2: understanding of Objects

Objects are common things around you, mainly understanding the differences between objects;

Important factors for object differentiation:

Static features (attributes, indicating what the object looks like ")

Dynamic Features (methods, indicating what the object "can do ")

3: process-oriented and object-oriented

Process-oriented: it refers to the process, steps, and processes that focus more on everything. Focus on small details

Object-oriented: It is something with some features and actions, focusing on the final result.

4: Understand Object-Oriented Programming

* Its core is the object.

* Objects are real-world entities, and object-oriented programming simulates these entities into computer programs to implement relevant functions.

* Compile various objects as needed and establish certain relationships between them;

5: encapsulation Concept

* It is to put some small objects together and then present them as a new object. encapsulation makes the interior of an object hidden, making it safer;

* Put the attributes and methods in a class, and the class can be regarded as a black box. Some common methods and attributes can only be seen outside. <Modularity and Data Hiding>

* Defines the use of operation methods to close data to the class, forming a class with data as the core and method as the shell.

6: Concepts of Classes

* An object is an instance of a class. It extracts the common features of the object. These common attributes and methods are organized in a unit, and the class is implemented;

* A class is a set of objects with the same attributes and common methods (actions ).

* Class attributes: static features owned by objects or entities, which are called class attributes in a class;

* Class method: A class method that represents the dynamic features of an object or entity;

7: differences between categories and objects <relationship between bicycles and bicycle design drawings>

* A class is a conceptual model in which attributes and methods of objects are defined;

* An object is an object;

8: class template Definition

Access modifier class Name

{

Define fields;

Define attributes;

Define the method section;

}

* Define the Class Name: Noun. The first letter is the upper-case Passal naming method;

* Compile fields and attributes of a class; * compile methods and interfaces of the class; [unit templates for completing a function independently] (learn to understand the composition of the class and learn to apply it here)

9: field: it is called a member variable (global variable );

{Fields and attributes are used to describe static features of the class} the variables defined in the method are called local variables. The scopes of the two variables are different;

10: Object creation process

Static void Main (string [] ardes) {// defines the FIELD Private int studentId; Private string studentName; // defines the attribute Public int StudentID {get {return studentId ;} set {studentId = value ;}} Public string StudentName {get {return studentName ;}set {studentName = value }}// obtain information {definition method} Public string GetStudent () {String info = string. format ("Name: {0}, student ID: {1}", studentName, studentId); Return info ;}}

11: object calling

// Create the object Student objStudent = new Student (); // assign an object attribute value to objStudent. studentId = 1001; objStudent. studentName = "A Hui"; // display the object attributes (call method) String info = objStudent. getstudent (); // class name. method Name Console. writeline (info); Console. readline ();

2. Fields and attributes

1: access modifier

Purpose: restrict the accessible range of object attributes or methods (acting on the internal and external aspects of the Class)

Type:

Private (private: externally invisible, inaccessible, accessible only in this class, default)

Public (Public: externally visible and accessible)

Protect (protected: only accessed in this class and subclass)

Intemal (only accessed in this project)

2: If public is removed from the method, the system automatically changes to private. The private modifier is not accessible to the outside world. If it is accessed, an error is reported and the operation (.) cannot automatically occur;

3: field (member variable of the Class)

Fields are generally modified using private (our private property), which is usually used only by different internal methods of the class;

4: attribute (description of static features) {what is and what is unique}

Privatize a field. The attribute is "public". get (); set (); assign an external value to a private field through value;

Attribute is the entry for external access to private fields, and the attribute itself does not store any data; get, set in the attribute;

Get is the value of the field returned for reading, and is assigned externally by set;

5: Use properties to avoid illegal data {Add if, else to get, set for judgment}

6: You can set the attribute to read-only. {No set method is available and cannot assign a value to it}

7: There is no attribute corresponding to the private field. You can add the required business code to the attribute, such as calling a method. {the property processing capability is strong}

8: Field and attribute comparison

Fields (member variables) are used for data interaction within the class. fields are generally private (private). You can assign values to them or obtain the values of fields, when you need to provide external data, encapsulate fields as attributes and do not use common fields;

Attribute data is generally provided to the outside world. It mainly describes the static features of data objects. It is generally public and can be set to read-only. Only attributes are written to improve data security. You can also add business logic internally to avoid illegal data;

Iii. Class methods and constructor Methods

1: A method represents the operations that a class or object can perform, that is, the dynamic features of an object. In a class, the field and attribute values are transmitted through methods;

Classification: instance method, static method, constructor method, abstract method, virtual method;

2: Functions of the Method

* Specifies the main functions of the class;

* The method is encapsulated in the class, so you do not need to understand the internal details during external calls, reflecting its "high cohesion" and "low coupling" features;

3: Set of errors of methods in the writing class

* If you forget the returned value, the system prompts that not all path codes return values;

* The type defined by the return value is different from that defined by the return value;

* The variable is out of scope during use.

4: The call of the constructor takes precedence over other methods. The parameter transfer is the same as the general method transfer;

The call here is because every time we use an object, we will use a New one. Here we call the default constructor in the class. Here we can implement the overload, when you want to initialize it.

5: constructor can be initialized. Using a constructor with parameters can avoid the trouble of initializing a single object attribute. Uniform initialization in constructor can make the program more beautiful;

6. construct multiple constructor Methods

Use {: this (stcould, stuName)} to call the constructor's own method;

7. Differences between constructor and instance methods

Constructor:

* Used for object initialization. A class contains at least one constructor;

* The call cannot be displayed. It can only be called when an object is created using new;

* No return value; no parentheses;

* The constructor name must be the same as the class name;

Instance method:

* Used to indicate what an object can do. A class does not have any instance methods. The dynamic features of an object;

* Only call: Object Name. method name can be displayed.

* Return values can be returned, which must be expressed in void;

* The method name must be meaningful and cannot be the same as the class name;

8: Object Initiator

The intermediate statement blocks must be separated by commas;

* Only attribute values can be initialized;

* It can only be used by the caller, but the initialization of the constructor must be written in the class. <CTOR>

9: redefinition of methods (3)

10: this keyword

* This indicates the object of the current class, used to access the member variables or methods of this class;

* Use this to distinguish between member variables and local variables;

There is also a base keyword. When our class inherits the word class, we use base to directly use the parent class.

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.