What is object-oriented programming

Source: Internet
Author: User

What is object-oriented programming
Object-oriented programming is a fairly new way of creating computer applications, and he solves many of the problems that traditional programming techniques bring. The programming method described earlier, called functional (or procedural) programming, often leads to a single application where all functions are contained within several blocks of code. With OOP, many blocks of code are often used, each with a specific function, each module being independent, or even the other modules being completely independent. This modular approach provides a very large variety and greatly increases the chances of code reuse.
What is an object
An object is a constituent part of an OOP application. These components encapsulate parts of the application, which can be a process data, or some more abstract entity, simply saying the object is very similar to the structure type discussed earlier in this book, including variable members and function types. The variables that he contains make up the data stored in the object, and the functions that it contains can access the object's functions. A slightly complex object may contain no data, but only functions, representing a process
Objects in C # are created from types, just like the previous variables. Type of object in OOP there is a special name "class" that instantiates an object using the class's definition, which means creating an instance of the class. Instances and objects of a class represent the same meaning
class = = Abrasive
Object = = Product
This chapter uses the generic Modeling language (Universal Modeling lanauage, UML) syntax to study classes and objects
Properties and Fields
Properties and fields can access the data contained in the object. This object data can be used to differentiate between objects, because different objects of the same class store constant values in attributes and fields. The different data contained in the object is collectively known as the state of the object, assuming that an object class represents a cup of coffee, called Cupofcoffice. When instantiating this type of object (that is, when creating objects of this class). You must provide a state that is meaningful to the class, at which point you can use properties and fields so that the code can go with the coffee brand to be used by the object, whether the coffee has milk or sugar cubes, and so on.
Fields and properties can be entered, so you can store information in fields and properties, as string variables, int variables, and so on. However, attributes are different from fields because properties do not have direct access to the data, and an object (class instance) allows the user to not consider the details of the data, without having to represent it in a one-to-one way in the attribute
public readonly string Name; Field
public int Val//define Properties
{
Get
{
return intval; A get block must have a return value for the property type. You can return the value of the field directly
}
Set
{
if (value >= 0 && value <=)//value User-supplied property values
Intval = value;
Else
Throw (New ArgumentOutOfRangeException ("Val", Value, "Val must be assigned a value between 0 and 10.")); /Throw exception
}
}


If you use a field in a Cupofcoffice instance to represent the number of sugar cubes, users can place their favorite values in that field (int value range) In general, it is better to provide the property in the access state than to provide the field, because this can better control the entire process, Read-write access to attributes can also be explicitly defined by an object (instantiated Class). Some properties are read-only and can only be viewed with their values and cannot be changed (at least not directly). This is usually an effective technique for reading several states at the same time Cupofcoffice has a read-only attribute description, in which he returns a string that represents the state of an instance (object) of the class, and in addition to read and write access to the property, you can also specify another access for the field and property to become accessible. This accessibility determines what code can access the write members, they can be used for all code (public), and can be used only for code in the class (Private priv ...), or even more miscellaneous patterns, in common cases is to set the field to private through the Union public properties to access them, The code in this class can directly access the code stored in the field to prevent the user from placing invalid content in it
Method
Methods this term is used to represent functions in an object, which are the same as other functions, using the same methods of returning values and parameters. Methods are used to access the functionality of an object, as is the case with fields and properties, and can be public or private, restricting access to external code as needed. They often affect their operations by using object states, which are collectively referred to as the state of objects in different data, and access private members, such as private fields, when needed

Each variable type is a class
Variable type = = Class ~ instantiation
Variable = = Object

<string>. Length, and so on. Object instance names and properties and methods
object instance name. Property or method


Object
Each object has a well-defined life cycle, with two important states in addition to the normal state being used.
··· Construction phase: The period during which an object is initially instantiated, which is called the initialization phase, and is completed by the constructor.
··· destructor phase: When you delete an object, you often do some cleanup work, such as freeing up memory, which is done by the destructor.


---constructors
The initialization process for an object is done automatically. We don't need to look for a memory space that is appropriate for storing new objects, but sometimes some extra work is needed to store the objects. For example, initialize the data stored by the object. Constructors are functions that are used to initialize data.
All objects have a default constructor, which has no arguments to the class with the same name, a class can contain several constructors with parameters, and become non-default constructors, which the code can use to instantiate objects in many ways. For example, give the initial value of the data stored in the object. In C #, constructors are invoked with the new keyword. For example:
Cupofcoffice Mycup = new Cupofcoffice ();
Instantiate a Cupofcoffice object with its default constructor
Cupofcoffice Mycup = new Cupofcoffice ("BlueMountain");
Instantiating an object through a non-default constructor
constructors, like fields and properties, can be public or private, code outside of a class cannot instantiate an object with a private constructor, but must use a public constructor, which allows the user of the class to use a non-default constructor
Some classes do not have public constructors, and external code cannot instantiate them, but these classes are not completely useless.

Static and Instance class members
Members, such as properties, methods, and fields, are unique to an object instance, and in addition to static members (shared members), such as static methods, static properties, static fields.
Static members can be shared among the strengths of the classes, so they can be thought of as global objects of the class such as
Console.writerline ();
Convert.ToString ();
The method is static. There is no need to instantiate at all. If you try to instantiate the console or convert, the operation fails because the constructor for this write class is not publicly accessible.

What is object-oriented programming

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.