The Java keyword this

Keyword this

1, this keyword:

Meaning: the current object

(1) if it appears in the constructor: that the object being created

(2) If the members of approach: that the object of this method is invoked

2, this usage:

(1) this. Properties

When a local variable with the same name as a member variable, it may be preceded by "this." Member variables for the difference

(2) this. Method

Members of the current object method call, may be omitted altogether "this."

(. 3) this () or this (argument list)

this () calls this constructor with no arguments represented class

this (argument list) is invoked with a parameter configuration of the present class

this () or this (argument list) or not, or must appear in the first line of the constructor

Sample code:

public class Student{
    private String name;
    private int score;
    
    public Student(){
        
    }
    public Student(String name){
        this.name = name;
    }
    public Student(String name, int score){
        this(name);
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setScore(int score){
        this.score = score;
    }
    public int getScore(){
        return score;
    }
}

3, the difference between the member variables and local variables?

Different locations (1) Statement

Member variables: the outer class method

Local variables: the method or the code

① methodical form attended Table

② A method of local variables in the body

Local variables of the code blocks ③

(2) different from the storage location in memory runtime

Member variables: Heap

Local variables: Stack

(3) modifier

Member variables: There are many modifiers, such as: rights modifier

Local variables: You can not add permissions modifier, can only add that final

(4) the initialization

Member variable: there are defaults

Local variables: no default, you must manually initialize

(5) Life Cycle

Member variables: With the creation of the object is created, with the demise of the object to be recovered and that the total death of the object with the students. Each object is independent.

Local variables: method called when assignment ends, there is no way to run. Each method calls, are independent

package

1, the role of the package:

(1) class to avoid duplicate names

Once you have the package, the full name of the class becomes: package name of the class

Numerous (2) Classification and Organization Management

For example: java.lang package, java.util package, java.io package .....

(3) can control some types of members or visible range

If the permission or members of a certain type of modification of the default, then it is limited to the use of this package

2, the statement syntax package:

package package name;

note:

(1) must be the first line of code in the source file

(2) a source file can have only one

3, naming packages norms and habits:

(1) All words are lowercase, use between each word. Segmentation

(2) the company's practice of using domain inverted

For example: org.eclipse.xxx;

4, the use of other type of package:

Prerequisite: permission modifier class or member is to use> default

Full Name (1) type of use

例如:java.util.Scanner input = new java.util.Scanner(System.in);

After (2) use the import statement, using the code name Jane

5, import statements

import package class name;. 
import packets *;. 

Note: When using two different packages of the same name of the class, for example: a java.sql.Date and java.util.Date. 
A full name, a short use

Sample code:

import java.util.Scanner;

public class Test{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
    }
}

 

 

Guess you like

Origin blog.csdn.net/Brevity6/article/details/90702493