Object-oriented issues
- Object-oriented Benefits
- Whether learning a technology requires fun, popular guidance
Object-oriented what is an object
Everything in the world can be defined as an object.
What is a class
A class is the abstraction of an object by pulling out its public properties and methods to form a set, that is, a class is an abstraction that describes an object's characteristics.
What is an instance
An instance is an object, as we are, an instance of human abstraction.
What is a construction method
Literally, a construction method is used to construct an object, and when the object is being created, the object is initialized by a constructor method.
Characteristics of the construction method:
- Same name as Class
- No return value
- When undefined, a default constructor method is created
What is method overloading
Method overloads are the same method that can be used to show different behavior depending on the type of your parameter
person { system.println ("Nothing can feed me eat (system. Out. println ("I'm eating fruit ... }
The method overloads provide the ability to extend functions, using only the following:
- Method name is the same
- Parameter type or number of parameters is different
Object-oriented three-feature encapsulation
Advantages:
- Loose coupling
- To hide the internal implementation of a class
- Exposes only the necessary interfaces to the outside, preventing the user from using illegal operations to alter the internal structure of the class object
In general, when designing a function, we always encapsulate all the properties and behaviors that are similar to that feature in a class, so that the behavior inside the function is not dependent on other classes, and when someone else uses your functionality, they just need to know how to call your interface to get the information they want. No need to bother about what the internal implementation is doing.
For the inside of the class, the general developer only provides the necessary interface to access its internal properties, and does not allow you to directly manipulate its interior, which is beneficial to ensure that the user is in accordance with the design logic of the designer to control the use, to avoid the illegal use of the resulting error.
Inherited
Characteristics:
- Subclasses have properties and methods that are not private to the parent class
- Subclasses have their own properties and methods that can extend the new behavior
- Subclasses can also override behavior from the parent class
Advantages
- Implementing code Reuse
- Convenient design and strict registration structure system (tree)
An object's inheritance is a ' is-a ' relationship, and if two objects A and B can be described as ' B is a ', then B can inherit a.
The inheritance relationship cannot be simply understood as the relationship between the father and the son, the inheritance relationship is actually a special relationship, like the cat and the mammal, the cat is a special abstraction of mammals, because it has the characteristics of mammals, but also has its own unique personality, such as it will catch mice, catch fish, These are not the common characteristics of mammals.
Of course, in the design of the class, do not inherit to inherit, too much use of inheritance will make the entire class hierarchy becomes very complex, so that the coupling between classes and classes become stronger, so in the use of inheritance, it is necessary to clarify the relationship between objects and objects, in the absence of the need to choose inheritance in case of the use of assembly.
Of course whether to use inheritance, this also depends on the situation, when two classes have a ' is-a ' relationship, you can use inheritance, when two classes have a ' has-a ' relationship, you can use the assembly.
Polymorphic
Polymorphism is also called dynamic binding, which means that the system behaves differently depending on the runtime type of the object at run time.
Characteristics:
- The subclass appears as the parent class
- Subclasses do it their own way at work
- When a subclass appears as a parent, unique properties and behaviors are not available
Static bindings are the opposite of dynamic bindings, and C programming uses static bindings. Static binding is done at the compile time, that is, your program at compile time, he has to the various methods of the call address has been bound, so at the time of operation, the program will directly according to your method address to invoke the specific method.
In the case of object-oriented programming polymorphism, dynamic binding is used, so how does dynamic binding work? When the program runs, how do you determine its runtime type to invoke the corresponding method?
Then explain it later.
Abstract class
Characteristics:
Interface
Characteristics:
- Interface cannot be instantiated
- Fields are public static final type by default
- Only abstract methods can be defined
The difference between an interface and an abstract class
For beginners, many people in the study, there must be such a doubt, interface and abstract class what is the difference in the end, when we design the program, when should be used abstract class, when to use the interface?
So first of all, the difference between the use of the two features:
- Abstract classes can define abstract methods, or they can define basic methods (that is, their own methods are implemented for methods), whereas interfaces can only define abstract methods
- A class can inherit only one abstract class, but it can implement multiple interfaces
- Subclasses can choose to implement only members of a partial abstract class (which is also an abstract class), but must implement all members of the interface
So let's talk about their differences in design thinking right now:
- A class is an abstraction of an object; an abstract class is an abstraction of a class; An interface is an abstraction of a behavior.
- If the behavior spans different types of objects (such as airplanes, birds, not the same class of objects, but all have flight behavior), consider using an interface; if it is for some similar objects (for example, cats and dogs belong to Animals), consider abstracting abstract classes to inherit
- From a design standpoint, abstract classes are abstractions of the public part of a child class, and interfaces do not know the existence of subclasses, but rather define a set of behaviors beforehand.
In other words, interfaces, abstract classes, classes and even objects are the result of abstraction at different levels and angles, and their generality is abstraction. Abstract classes are usually a generalization of a subclass of existing generality, by extracting the common attributes and behaviors among subclasses, and thus abstracting a generalization, whereas an interface is a pre-defined set of related behaviors, and as to who will implement this behavior, the developer decides And all I have to do is tell the implementation class of the interface what kind of behavior you have.
About Object-oriented