oo (object oriented)
Object-oriented is a kind of thought, is based on the process, that is, object-oriented is to implement the function through objects, the function encapsulated into the object, let the object to achieve specific details; The idea is to use the data as the first, and the method or algorithm as the second, which is an optimization of the data, It is more convenient to operate and simplifies the process.
Object-oriented has three major characteristics: encapsulation, inheritance, polymorphism, wherein the encapsulation refers to the hidden object properties and implementation details, only provide public access, so that the specific changes are isolated, easy to use, improve reusability and security. For inheritance, there is a certain relationship between the two things, then the inheriting class can get some properties and methods from the inherited class, which improves the reusability of the code. Inheritance is a precondition for polymorphism. Polymorphism means that the reference to the parent class or interface points to the subclass object, which improves the extensibility of the program, which means that as long as the same interface or class is implemented or inherited, the corresponding method in the parent class can be used to improve the program extensibility, but the polymorphism is a bit of a disadvantage that the parent class reference cannot directly access member properties in the subclass
Three major features
1. Encapsulation
The so-called encapsulation, that is, to encapsulate objective things into abstract classes, and classes can be their own data and methods only let the trusted class or object operation, to the untrusted information hiding. Encapsulation is one of the object-oriented features and is the main feature of object and class concepts.
Simply put, a class is a logical entity that encapsulates the data and the code that operates the data. Within an object, some code or some data can be private and cannot be accessed by the outside world. In this way, objects provide different levels of protection to internal data to prevent unintended changes to unrelated parts of the program or to use the private parts of the object incorrectly.
- Premise: Encapsulation of unique non-static member properties and member methods, external operation interface
- Advantages: Isolation of changes, ease of use, increased reusability, increased security
- Cons: Hide content that does not need to be provided externally; Hide properties, provide public methods to access them
2. Inheritance
Inheritance refers to a method that allows an object of one type to obtain the properties of another type of object. It supports the concept of classification by class. Inheritance refers to the ability to use all the functionality of an existing class and to extend these capabilities without rewriting the original class. A new class created through inheritance is called a subclass or derived class, and the inherited class is called the base class, parent class, or superclass. The process of inheritance is from the general to the special process.
To implement inheritance, it can be implemented through inheritance (inheritance) and combination (composition). The concept of inheritance is implemented in two categories: implementation inheritance and interface inheritance. Implementation inheritance refers to the ability to directly use the properties and methods of the base class without additional coding; Interface inheritance is the ability to use only the names of properties and methods, but subclasses must provide the implementation;
The override of member methods in the inheritance relationship of a class follows certain rules:
- The current parent function is not satisfied with the current requirement when you need to define a subclass and replicate the parent class method, at which time the replication ( When a subclass method has the same name as a parent class method, the compiler determines that the behavior is a replication of the parent method, and the argument list is the same as the parent class, but the method body needs to be redefined, and it is important to note that the access The permission description needs to be greater than or equal to the method that is being replicated in the parent class, What you need to declare is that the overrides are only on the method, there is no overwrite on the member variable
-
- Prerequisites: Class inheritance, interface inheritance
- Advantage: Subclasses can reuse parent class members for improved program flexibility
- Cons: Cannot be more inherited, it is difficult to improve scalability
3. Polymorphic
The so-called polymorphic means that the same method of a class instance has different manifestations in different situations. Polymorphic mechanisms enable objects with different internal structures to share the same external interface. This means that although specific actions are different for different objects, they can be called in the same way through a common class. rewriting produces polymorphic but overloaded and does not produce polymorphism because the JVM provides the language attributes on the surface, but the underlying JVM produces different function names when overloaded.
Polymorphism is generally divided into: polymorphism at compile time (static polymorphism) and runtime polymorphism (dynamic polymorphism), the properties of members in polymorphic states are static bindings, the method belongs to dynamic binding, Fu F = new Zi ();
- For member variables: When a property of the same name exists in the child parent class, only the property in the parent class is called when the (polymorphic call) uses the F call, even if the parent class does not have the attribute in it, and the property in the subclass is not called, that is, the member property does not have a polymorphic attribute, even if the subclass is overwritten Only the member variables corresponding to the reference type of the object are called, compile-time and run-time based on the properties in the reference type
- For member functions: When a function polymorphic call with the same name appears in the child parent class, F looks for the method in the owning class, and if there is no corresponding method in the owning class, the corresponding method in the reference type is found (compile-time according to the method in the reference type, and the runtime according to the method in the owning class)
- For static members: subclasses inherit static members from the parent class, whether overwrite or not, F will only look for the desired static member in the parent class when the polymorphic call is made, because the static members belong to the class and object-independent, so such calls are only relevant to the application type (compilation and run are only related to the reference type)
According to the conclusions of Cardelli and Wegner, polymorphism can be subdivided into the following 4 categories:
- parameter polymorphism: Using parametric templates, by giving different types of parameters, a structure has multiple types. function templates and class templates in the C + + language belong to parameter polymorphism. The parameter polymorphism is called the static polymorphism, it executes fast, the exception is few, the call at compile time already determined. Parameter polymorphism is a kind of polymorphism which is widely used and is called the purest polymorphism.
- contains polymorphism: It exists in many languages, the most common example being sub-typed, That is, one type is a subtype of another type. Typically, run-time type checking is required, which is dynamic polymorphism. The basis for containing polymorphism is virtual functions. A virtual function is a relationship that introduces a derived concept that is used to represent a member function between a base class and a derived class.
- overloaded polymorphism: The same name represents different meanings in different contexts. Typical examples are operator overloading and function overloading, which are statically polymorphic.
- forced polymorphism: The compiler uses semantic manipulation to force the type of the operand to be transformed, To meet the requirements of a function or operator. Most of the basic types of operators in the programming language, when the different types of data mixed operation, the compiler will generally be forced polymorphic. Programmers can also display the action of forcing polymorphism. such as Int+double, the compilation system will generally convert int to double, and then perform the double+double operation, where the int->double conversion, the implementation of the mandatory polymorphism, can be implicit, can also be explicitly converted. Forced polymorphism belongs to static polymorphism.
- Premise: Inheriting a parent class or implementing an interface, overriding method, parent class, or interface reference to a child class object
- Advantages: Greatly improve the scalability of the program
- Disadvantage: A parent class reference cannot take a subclass member property directly, and a parent class cannot call a method that is unique to a subclass
javase--Object-oriented