Java keywords include the usage of this and super.

Source: Internet
Author: User

Generally, I will take the test from the following aspects: whether it is for C ++, C #, or Java

First, we will examine the keywords and labeled constants.

Then there are questions about classes and interfaces.

Then there are questions about array, Io, and socket.

Then we will examine the various SQL statements of the database.

Then there is sorting, including fast sorting, insert sorting, and select sorting

Then there is the test of the design model.

Java is used as an example to illustrate some of your own problems:

Access control:

Private

Protected

Public

Class, method, and variable Modifier

Abstract declaration Abstraction

Class

Extends extension and inheritance

Final ultimate, unchangeable

Implements implementation

Interface

Native local

New, create

Static

Strict and accurate strictfp

Synchronized thread, synchronous

Transient is short

Volatile is easy to lose

Program Control statement

Break bounce cycle

Continue

Return

Do run

While Loop

If

Else and vice versa

For Loop

Instanceof instance

Switch

Case: return the result in the switch.

Default

Error Handling

Catch exception

Run any finally exceptions

Throw an exception object

Throws declares that an exception may be thrown.

Try catch exceptions

Package-related

Import

Package

Basic Type

Boolean

Byte type

Char character type

Double double,

Float floating point

Int integer

Long Integer

Short short integer

Null

True

False

Variable reference

Super parent class, superclass

This class

Void no return value

No const or Goto, but these two words are reserved words rather than keywords. Reserved Words are reserved by the system and will not be used by the system itself.

No friendly or sizeof !!!

Identifier variables, classes, and methods all need a certain name. We call this name an identifier. java has certain limitations on identifiers. first, all identifiers must start with a letter (case sensitive ). the underscore (_) or dollar sign $; followed by a number (0-9), all uppercase letters from a -- Z. lowercase letters and underscores (_) of a -- Z _. the dollar sign $ is composed of all ASCII codes before the hexadecimal 0x0. The third note is that the system reserved keywords cannot be used as the identifier.

Java keywords: This, super usage Summary

I. This

The Java keyword "this" can only be used in methods and methods. After an object is created, the Java Virtual Machine (JVM) assigns a pointer to the object to reference itself. The pointer name is this. Therefore, this can only be used in non-static methods in the class. This is definitely not allowed in static methods and static code blocks, this is a clear explanation in the article "Java keyword static and final usage summary. In addition, this is only associated with a specific object, but not the class. Different objects of the same class have different this. Here is a comprehensive example of using this to illustrate the problem:

Package org. leizhimin;

Public class test6 {

Private int number;

Private string username;

Private string password;

Private int x = 100;

Public test6 (int n ){

Number = N; // This can also be written as: This. Number = N;

}

Public test6 (int I, string username, string password ){

// The member variables have the same name as the parameter. The member variables are blocked and accessed using the "this. member variable" method.

This. Username = username;

This. Password = password;

}

// The default constructor without Parameters

Public test6 (){

This (0, "unknown", "null"); // call another constructor using this

}

Public test6 (string name ){

This (1, name, "null"); // call another constructor using this

}

Public static void main (string ARGs []) {

Test6 T1 = new test6 ();

Test6 t2 = new test6 ("Tourist ");

T1.outinfo (T1 );

T2.outinfo (T2 );

}

Private void outinfo (test6 t ){

System. Out. println ("-----------");

System. Out. println (T. number );

System. Out. println (T. username );

System. Out. println (T. Password );

F (); // This can be written as: This. F ();

}

Private void F (){

// The local variable has the same name as the member variable. The member variable is blocked and accessed using the "this. member variable" method.

Int X;

X = This. x ++;

System. Out. println (X );

System. Out. println (this. X );

}

// Return the reference of the current instance

Private test6 getself (){

Return this;

}

}

The running result is as follows:

-----------

0

Unknown

Null

100

101

-----------

0

Visitors

Null

100

101

Looking at the example above, we will explain under what circumstances the use of this:

First, call another constructor using this (parameter list). This is only used in the constructor of the class and cannot be used in other places.

2. When the name of a function parameter or local variable in the function is the same as that of a member variable, the member variable is blocked. To access the member variable, you must use "this.

To reference member variables. Of course, without the same name, you can directly use the name of the member variable instead of this.

Third, when the function needs to reference the current object of the class to which the function belongs, use this directly.

In fact, these usage summaries come from a more in-depth understanding of the phrase "this is a pointer to the object itself". It is easy to forget and easily remember.

Make a mistake. Please understand!

Ii. Super

The key of super is similar to this. It is a blocked member variable or member method or becomes visible, or used to reference the blocked member variables and member members.

Method.

However, super is used in subclass to access the blocked members in the direct parent class. Note that it is a direct parent class (that is, the nearest superclass above the class ). The following is a comprehensive application

In the super example, there are two classes: one father class and one sub-class son of Father class. These two classes fully demonstrate the usage of super. Here is the code:

Package org. leizhimin;

Public class father {

Public String v = "father ";

Public String x = "output the father class public member variable X !!! ";

 

Public father (){

System. Out. println ("Father constructor is called! ");

}

Public father (string v ){

This. V = "Father class with parameter constructor! Running .";

}

Public void outinfo (){

System. Out. println ("Father outinfo method called ");

}

Public static void main (string [] ARGs ){

// Todo automatically generates method stubs

}

}

Package org. leizhimin;

Public class son extends father {

Public String v = "son ";

Public son (){

Super (); // call the constructor of the superclass, which can only be placed in the first row.

System. Out. println ("the son construction method without parameters is called! ");

// Super (); // The error must be placed at the top of the constructor body.

}

Public son (string Str ){

Super (STR );

System. Out. println ("son has a parameter constructor called! ");

}

// Overwrite the superclass member method outinfo ()

Public void outinfo (){

System. Out. println ("Son outinfo () method called ");

}

Public void test (){

String v = "Hahaha! "; // The local variable v overwrites the member variable V and the super variable v.

System. Out. println ("------ 1 -----");

System. Out. println (V); // output the local variable v.

System. Out. println (this. V); // output (subclass) member variable v

System. Out. println (Super. V); // output the superclass member variable v

System. Out. println ("------ 2 -----");

System. Out. println (x); // output the superclass member variable V, inherited by the subclass.

System. Out. println (Super. X); // output the superclass member variable v

System. Out. println ("------ 3 -----");

Outinfo (); // call the outinfo () method of the subclass

This. outinfo (); // call the outinfo () method of the subclass.

Super. outinfo (); // call the outinfo () method of the parent class

}

Public static void main (string [] ARGs ){

New son (). Test ();

}

}

Sub-class son running result:

The father constructor is called!

Son's non-parameter constructor is called!

------ 1 -----

Hahahaha!

Son

Father

------ 2 -----

Output The Public member variable X of the father class !!!

Output The Public member variable X of the father class !!!

------ 3 -----

Son's outinfo () method is called

Son's outinfo () method is called

Father outinfo method called

 

Note: In this example, only the usage of super is described. In actual design, the class is generally private as much as possible.

Through the example above, the usage of super is summarized below:

1. To call the constructor of the parent class in the subclass constructor, use the "super (parameter list)" method to call the constructor. The parameter is not required. Note that the "super (parameter list)" statement can only be used in the first row of the subclass construction method body.

2. When the local variable in the subclass method or the member variable of the subclass has the same name as the parent member variable, that is, when the subclass local variable overwrites the parent member variable, use "super. the member variable name to reference the parent class member variable. Of course, if the parent class member variables are not overwritten, you can also use "Super. member variable name" to reference the parent class member variables, but this is unnecessary.

Third, when the member methods of the subclass override the member methods of the parent class, that is, the subclass and the parent class have the same method definition (but the method body can be different). At this time, use "super. method (Parameter List) to access the parent class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.