This chapter summarizes the super, this, final, static, break, continue in Java
1. Super
Super is primarily used in inheritance, which represents the constructor that invokes the parent class.
1. If you want to invoke the overridden instance method of a parent class in a subclass method, you can use Super qualification to invoke the overridden instance method of the parent class
classB { PublicB () {} Public voidC () {System.out.println ("C in B"); }} Public classAextendsB { Public voidC () {System.out.println ("C in a"); } Public voidD () {Super. C (); } Public Static voidMain (string[] args) {A a=NewA (); A.D (); }} The output is: C in B
2. If super is used in the constructor, super is used to qualify the constructor to initialize the instance variable that the object inherits from the parent class, not the class's own instance variable
3. If the parent class defines an argument-free constructor, the subclass can arbitrarily define the constructor method, and the system automatically adds a super () to the first row of the construction method to invoke the parent class's parameterless construction method
classB { PublicB () {System.out.println ("How to construct a parent class"); }} Public classAextendsB { PublicA () {//super (); This implicitly adds a super ()System.out.println ("constructor method for subclasses"); } Public Static voidMain (string[] args) {A a=NewA (); }} The output is: How to construct a subclass of a parent class
If the parent class does not define an parameterless construction method, the child class can only define and construct methods with the same number of arguments as the parent class constructor method, and then explicitly call the super (parameter ...) of the parent class in the constructor method.
If the parent class defines both the argument and the parameterless construction method, the subclass can arbitrarily define the constructor method, and the first row of the subclass constructor method can call the parent class's constructor arbitrarily using the plus or no parameter super ().
2. This keyword
In Java, the This keyword points to the object that called the method. Have the following effect
1. Calling the properties and methods of the current class
2. Calling the constructor method
Public classpeople {PrivateString name; Private intAge ; PublicPeople (String name,intAge ) { This(); This. Name =name; This. Age =Age ; } Publicpeople () {System.out.println ("No parameter construction method"); } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } Public voidsay () {System.out.println (GetName ()+ "-----" +getage ()); } Public Static voidMain (string[] args) {people P=NewPeople ("Faye Wong", 50); P.say (); }}
3. Final keyword
1.final is called the end in Java, which means the final meaning
2.final can declare classes, methods, and properties:
Classes that use Final declaration cannot be inherited (similar to the ancient Eunuch, haha)
Methods that use Final declaration cannot be overridden
A variable with final declaration becomes a constant and cannot be modified
4. Static keyword
Static in Java, used to modify methods, member variables, code blocks, inner classes, and so on (external classes cannot use static)
1. A property declared with static is a global property
2. The method using the static declaration becomes a static method and can be called directly using the class name
3. Static members cannot access non-static members directly, even static inner classes, and cannot access non-static objects of external classes
So we can declare a class-level variable as static. A static variable belongs to a class, not to an object or instance created by a class. Static is often used in conjunction with final to decorate a constant resource that can be used by all objects together.
5. Break keyword
Break is often used in loops to end a loop, when a condition is triggered, a break is executed, the program no longer loops whether the loop is executed, jumps out of the loop, executes the code below the loop
Break completely terminates the loop
6. Continue key Words
Continue is used to jump out of a loop, and when a condition is met, it jumps out of the current loop and executes the next loop.
Continue just jumps out of the loop, doesn't stop.
Java Notes Collation (vii), Java several keywords