- Java as an object-oriented language with three major features (object-oriented core): Encapsulation inheritance polymorphism
- Encapsulation: It's packing, everything is packed.
- Inheritance: Subclasses can inherit from parent class
- File name: Student.java
1 public class Student { 2 public String name; 3 public int age; 4 Public void study () { 6 System.out.println ("Student Learning" 7 8 }
- The common method Student (Student) is defined in the above and defines a common behavior-general class learning (study)
- Student as a constructor, constructors cannot be inherited, so there is no constructor override
- File name: Unstudent.java
1 Public classUnstudentextendsstudent{2 Public voidStudy () {3System.out.println ("Learning Methods for college students");4 }5 Public voidPlayGame () {6SYSTEM.OUT.PRINTLN ("College students play DotA");7 }8}
- In the above, the general method of college students (unstudent) was defined and inherited (extends) General method students (Study)
- At the same time, it inherits the premise of the parent class (Study) in the subclass (Unstudent), which defines a method that is the same as the parent class (method name, parameter, return type) ("Public void Study () {
”)。
- Subclasses can define a new behavior (Class) "PlayGame".
- File name: Test.java
1 Public classTest {2 Public Static voidMain (string[] args) {3 //Automatic Transformation4Unstudent US =Newunstudent ();5Student US1 =Newunstudent ();6Student US2 =NewStudent ();7 8 us.study ();9 us1.study ();Ten us2.study (); One A us.playgame (); - us1.playgame (); - us2.playgame (); the } -}
- In the main function above, the university student (unstudent) "Us" is constructed once, and the method is overloaded. "Us" will have the common method unstudent all the attributes, and the method overrides will overwrite the definition of the parent class's original class.
- "US1" has all student properties, but the property that is overridden by unstudent will be the contents of the Unstudent rewrite, the original student within the class will be overwritten, unstudent exists but student does not exist in the class "US1" will not be called.
- "US2" is the authentic student class.
-
- There are two ways of doing this:
- Automatic transformation
Definition: Create the object of the subclass and then automatically transform to the type of the parent class
Format: The parent class class name Object name = new Subclass class name ();
- The transformed object type is the transformed type, the method that executes if the subclass overrides the method of the parent class, then executes the override, and if there is no override, it executes the parent class's own
If there is no automatic transformation process, then the method of execution is your own method
- Because of features such as automatic transformation and method rewriting and inheritance, the Java language presents polymorphism.
- Multiple objects of the same type, executing the same method, performing a different process (there are differences)
- Why is there a polymorphic phenomenon???
- Automatic Transformation + method rewriting
- Auto-Transformation + method overrides will result in inheritance and will therefore be polymorphic
- There are only three ways to classify the Java language:
- Construction method: public class name () {}
- Common method: Public return type method name (parameter) {}
- Abstract method: Public abstract return type method name (parameter);
- There are also three categories of Java language classes:
- Normal class: public class Name {}
- There can only be ordinary methods and construction methods, there must be no abstract method
- Can create objects
- The property can be a constant (static final), or it can be a variable
- Abstract class: Public abstract class Name {}
- There are three types of methods that can exist
- Cannot create object
- The property can be a constant (static final), or it can be a variable
- Interface: Public interface class name () {}
- There can only be abstract methods, common methods and construction methods do not exist
- Cannot create object
- property must be constant (static final)
- What is the use of interfaces and abstract classes?
- The role of interfaces and abstract classes is used when the parent class is inherited, and the abstract method is used to instruct the subclass to rewrite (implement) the
- Key Sub: Implements
- Format:
- public class subclass class name implements interface name {
Overriding all the abstract methods of the parent class
}
- Interface is nothing more than a leader, but a guide to development
- File name: Teacher.java
1 Public InterfaceTeacher {2 Public Static FinalString name = "AA";3 Public Static FinalString job = "AA";4 Public Static Final intAge = 20;5 6 //Abstract Methods7 Public Abstract voidplay1 ();8 Public Abstract voidplay2 ();9 Public Abstract voidplay3 ();Ten Public Abstract voidPlay4 (); One Public Abstract voidplay5 (); A}
- File name: Unteacehr.java
1 Public Abstract class Implements teacher{2 3 }
- In the above two files, the "Teacher" class as the interface Class (keyword interface represents the interface class
- has three constants and cannot be changed.
- There are abstract methods that cannot be implemented and defined.
"Code Note" Java Basics: Java methods and classes