Today’s sharing started, please give us more advice ~
An overview of object orientation
Object oriented programming is a kind of programming thought which conforms to the habit of human thinking. Unlike process – oriented, object – oriented has its own unique advantages. To take a common example, if we want to get a wooden bed, process-oriented we may need to buy wood, design drawings, and finally cut it. But for object oriented, we just need to find a furniture store and buy a bed.
Object orientation is also one of the characteristics of the Java language. This object-oriented language has: 1, encapsulation, 2, inheritance, 3, polymorphism
Two. Three characteristics
1. Encapsulation (private) :
Encapsulation is the privatization of some details and the provision of methods for the outside world to access their properties (if not, then encapsulation is meaningless)Copy the code
Features: The class modified by private has the lowest permissions and is the most secure, and its security and easy maintenance is significant.
Such as:
`public class Fengzhuang { private int chang; // member attribute private int kuan; private int di; private int gao; Public Fengzhuang() {// No argument constructor} public Fengzhuang(int chang, int kuan, int di, int gao) {// With argument constructor this.chang = chang; this.kuan = kuan; this.di = di; this.gao = gao; } public int getChang() { return chang; } public void setChang(int chang) { this.chang = chang; } public int getKuan() { return kuan; } public void setKuan(int kuan) { this.kuan = kuan; } public int getDi() { return di; } public void setDi(int di) { this.di = di; } public int getGao() { return gao; } public void setGao(int gao) { this.gao = gao; } public void mj(int chang,int kuan,int di,int gao){ int sum=chang*kuan; int sum2=di*gao/2; System.out.println(" rectangle area is "+sum+" triangle area is "+sum2); }} `
Copy the code
In this example, encapsulate the area of a rectangle and a triangle as a method
test
public class Fengzhuangtest {
public static void main(String[] args) {
Fengzhuang FZ = new Fengzhuang();
FZ.mj(5.4.6.7); ; }}Copy the code
About packaging:
Method and private modified class are encapsulated form, when you encounter this only need to transform the number (length and width, bottom and height) of the case, can be encapsulated as a method, easy to use.
2. Extends:
Inheritance mainly describes the relationship between classes. Through inheritance, functions of existing classes are extended based on the definition of existing classes. (In Java, inheritance is used between subclasses and their parents)
1. Define a parent class (animal)2. Define a subclass (cat)Among them, as an animal, cats can inherit the characteristics of eating food in animals, and cats have the characteristics of catching mice.
3. Take a test
As you can see from this simple example, inheritance is implemented through the extends keyword. The cat class does not define an eating property, but the eating property can be inherited to improve code reuse.
On inheritance:
1. A subclass cannot selectively inherit from its parent class. If it chooses to inherit, it inherits everything except private attributes and methods in the parent class.
2. A subclass is an extension of a parent class and can have its own attributes and methods
3. Java is single inheritance, but can be multiple inheritance (that is, a subclass can only inherit from a parent class, but a parent class can have multiple subclasses. In layman’s terms, your dad can have more than one child, but those children only have one father.)
3. The polymorphism:
Polymorphism allows different objects to make different responses to the same message. It means that after attributes and methods defined in one class are inherited by other classes, they can have different data types or behave differently, which makes the same attribute and method have different semantics in different classes.
Conditions for realizing polymorphisms:
In order to achieve polymorphism, three necessary conditions must be met: inheritance, overwriting, and reference of the parent class to the subclass object.
Upward transition:
Superclass type variable name = new subclass type
This is usually the case when the parent class is completely satisfied and we don’t need to add new attributes
Downward transformation:
Subclass type variable name = subclass type variable of the parent class type
This situation is usually used when the parent class does not meet the requirements and requires methods specific to the child class
Such as:
1. Define a parent class (all animals eat)2. Define two subclasses (cat, dog) In this case, it’s also eating, in the case of the cat it’s eating fish; For dogs, it’s bone eating. That’s polymorphism.
3. Take a test Automatic boxing: Automatically converts base data types to wrapper class types
Automatic unpacking: Automatically converts wrapper class types to base data types
summary
Mention of object oriented, naturally will think of process oriented, process oriented is to analyze the steps needed to solve the problem, and then use the function to implement these steps one by one, when using a call can be. Start by focusing on its three main features and understanding its basic concepts.
Today’s share is over, please forgive and give advice, thank you!