Object Oriented Programming (OOP)
Classes and objects
A class is a template that describes the behavior and state of a class of objects. An object is an instance of a class. It has corresponding state and behavior. A dog is an animal that simulates reality and encapsulates data and code. Each dog can be said to be an object with its corresponding characteristics (attributes) and behaviors (methods), such as the color and name of the dog, and behaviors such as barking, swimming, eating, etc
To sum up: Class is to describe a class with the same characteristics (attributes) and behavior (methods) the characteristics of the object of the set of a set of objects in the class said as the attributes of a class, such as the color of the dog, the name, types, such as huskies and pug dog, but maybe they name or color object behavior in class is a class of methods, such as dogs are some behavior can call, So “will call” is the method of a class and here are some real world examples to help you understand classes and objects
class | object |
---|---|
people | The han Meimei that is taking an exam, the student xiaoming that has a class, the Zhang SAN that hits king |
animal | A fighting dog, a sleeping cat, a fish in the water |
# # interface | |
What is an interface? | |
Java and Patterns gives a good example: we can unplug the electric fan and plug in the charging plug of our computer. That is to say, these electrical appliances are pluggable. They are pluggable because they have plugs matching the socket. | |
So the interface is really the socket, and the plugable build is the appliance that we use, and the interface is the key to making the build plugable | |
What is the interface for? | |
1: Interaction specification between classes | |
2: Define the interface between function consumers and function providers | |
Why interfaces? | |
1: Isolate changes | |
2: The interface is the guarantee of pluggability | |
# # abstract classes | |
What do abstract classes do: Contain change and immutability | |
Abstract classes only provide a partial implementation of a type, and can have both abstract and concrete methods. Abstract methods can be said to be the changing part, while concrete methods are the invariable part | |
## Three OOP features | |
# # # encapsulation | |
Encapsulation can be thought of as a protective barrier that prevents the code and data of this class from being randomly accessed by code defined by an external class. Access to the code and data of this class requires interface control, and encapsulation makes the code easier to maintain and understand | |
Advantages of encapsulation: | |
1: Encapsulation reduces coupling | |
2: The code inside the encapsulated class can be modified at will | |
3: Hide implementation details and call the interface directly | |
# # # to inherit | |
Inheritance is a cornerstone of the Java object-oriented programming technology, inheritance is a subclass inherits the characteristics and behavior of the parent class, making a subclass object (instance) have instance fields and methods of the parent class, or subclass from the parent class method, makes the subclass has the same behavior, the parent class or subclass to cover the behavior of the parent class, to implement your own behavior | |
Inherited features: | |
1: A subclass has non-private attributes and methods of its parent class | |
2: Subclasses can have their own properties and methods, that is, subclasses can extend the parent class | |
3: Subclasses can implement methods of their parent class in their own way | |
For example, class A inherits class B, and class B inherits class C. Therefore, according to the relationship, class C is the parent of class B, and class B is the parent of class A | |
5: Improved the coupling between classes | |
# # # polymorphism | |
Polymorphism is the same behavior with different manifestations, the same interface, different instances of implementation may appear different results | |
What does polymorphism offer us: a way to achieve change | |
Advantages of polymorphism: | |
1: Decouple types | |
2: replaceable | |
3: can be expanded | |
4: Flexible and simple | |
## OOP reuse form | |
Relationships between classes: | |
1: inheritance | |
2: hold | |
Class 2 holds an instance of class 1 | |
Class 2 uses class 1 | |