Key points to understand Java object Orientation:
Class, object
The class? First take an example: Xiao Li designed a car design, and then handed over to the production workshop to produce cars, black, red, white… Here, the car design is the class, the production shop is the New constructor (most objects come out of new), and the produced car is the object. Java programming is essentially the process of building classes.
Objects? Everything is an object. In the universe, such as plants, animals and human beings, each individual has his own role and ability. This requires objects to be highly cohesive and poorly coupled (simply understood as the human brain, which is responsible for thinking, imagining, and remembering, but not breathing, detoxifying, and digesting food, thus ensuring its independence and efficiency). Objects have two types of west and west states (properties) and actions (methods), as shown in the following code:
Student.java
1 public class Student {2 String name; // name 3 int age; // Age 4 String classNo; // class 5 String hobby; Public void show(){8 system.out.println (name + "\n age: "+ age + "\n read:" + 9 classNo + "\n hobby: " + hobby); 11 10}}Copy the code
InitialStudent.java
1 public class InitialStudent { 2 public static void main(String args[]){ 3 Student student = new Student(); // create object 4 student.name = "student "; // assign 5 student.age = 10; 6 student. ClassNo = "S1 "; 7 student. Hobby = "basketball "; 8 student. The show (); // Call method 9}}Copy the code
Encapsulation, inheritance, polymorphism
Encapsulation? To use a vulgar metaphor, I have a lot of data stored on my portable hard drive, but my roommates often borrow and use mine (formatting? Install the system? Save movies?) , this gives me this owner very big inconvenience, I declare with him, this hard disk is my private, can’t you have to use in accordance with the way I allow! Objects, too, cannot arbitrarily access internal data, otherwise it will cause “cross infection”. So we need encapsulation: privatizing properties and providing public methods to access private properties.
Adult.java
1 public class Adult { 2 private int age; 3 public int getAge() { 4 return age; 5} 6 public void setAge(int age) {7 if (age < 18) {8 system.out.println (" error! Minimum age should be 18!" ); 9 this.age = 18; 10} 11 else{12 this.age = age; 13 } 14 } 15 16 public String toString() { 17 return " [age=" + age + "]"; 18}} 19Copy the code
TestAdult.java
1 public class TestAdult {2 public static void main(String[] args) {3 Adult p1 = new Adult(); 4 p1. SetAge (20); 5 system.out.println (p1.toString()); // Added data access restrictions to ensure maintainability 6} 7}Copy the code
Inheritance? The manager thought the car design drawing designed by Xiao Li was very good, but now new energy is advocated, so the engine needs to be changed to electric engine. So Chen easily changed the gasoline engine part of the drawings into the engine engine, and then reworked the relationship to complete the new drawings! That’s what inheritance does: it uses the definition of an existing class as a foundation to create a new class, which can either have the characteristics of its parent or derive more of them. This makes it easy to reuse old code and can greatly reduce development time.
Person.java
1 class Person {// define human 2 public String mName; Public int mAge; 5 public void dining() {6 system.out.println (" eat...") ); 8} 9 9 class Student extends Person {11 public float mGrade; Public void examination() {14 system.out.println (" 一 次 一 次...") ); 17 17 class Teacher extends Person {19 public float mSalary; 21 public void lection() {22 system.out.println ("... ); 23} // The method of class 24}Copy the code
TestPerson.java
1 public class TestPerson { 2 public static void main(String[] args) { 3 Student std = new Student(); // instantiate the student object 4 std.mName = "lidong "; 5 std.mAge = 18; 6 std.dining(); // Assign names and ages to members of the parent class. // Call the eating method, which accesses members of the parent class 7 std.examination(); 8 9 Teacher Teacher = new Teacher(); // instantiate teacher object 10 teacher.mName = "zhao Zhongxiong "; 11 teacher.mAge = 72; 12 teacher.dining(); // Assign names and ages to members of the parent class. 13 teacher.prelection(); // Call the test method, access the member of the subclass 14} 15}Copy the code
Polymorphism? Procedure defined in the reference variable is pointing to the specific type and referenced by the variable from method calls are not sure when programming, but during the program is run to determine, that is, a reference variable STLL can point to which class instance object, the reference variable from method calls the method to realize exactly is which class, must can decide during the program is running. Because the program is running to identify a specific class, so, don’t need to modify the source code, can make reference variables bound to the different class implements, leading to the reference call specific methods change, namely do not modify the program code can change the program of the specific binding in the runtime code, let the program can select multiple running state, this is polymorphism. Polymorphism enhances the flexibility and extensibility of software. Xiao Li likes listening to birds sing.
Xiao Li: Birds outside the window, sing me a song.
1. What do you mean?
2. What do you mean?
3. A bird?
Bird: bird sing ~ ~ ~ ~ ~ ()
Xiao Li: The bird sings well. What kind of bird are you?
Birds: bird shape ()
Xiao li :(if the blue word above is defined as 3, it is a parrot) ha ha! You’re a parrot!
Therefore, the process of polymorphism is essentially an abstract instruction, which makes a group of individuals with the same behavior but different contents work together.