Java Object-Oriented - Keyword (package, import, this)

Java is a computer language keywords previously defined, have special meaning to an identifier, sometimes called reserved words, there is special significance of variables. Java keywords are for Java compiler special meaning, they are used to represent a data type, or shows the structure of a program like keyword can not be used as variable names, method names, class name, package name and parameters. This blog describes the specific use package, import, this keyword.


table of Contents:

☍ package Keyword

☍ import keywords

☍ this keyword


☍ package Keyword

The package keyword overview

package statement as the first statement Java source files of the package specified in the file where the class definition. (Default if the statement is designated as the unnamed package)

package keyword format

Registration sub-top package package name.;

example:

//目录  pack1/pack2/Test.java
//声明Test.java类所在的包
package pack1.pack2;
public class Test{
    ......
}

☃ package directory corresponding to the file system, Package statement with "." Level to indicate the package (directory), the directory path corresponds to the '/'

Naming ☃ package is usually identified as the word lowercase English words. Usually where the company or organization's domain inversion: com.nyist.xxx

Role package package

☃ package to help manage large software systems: the function similar classes into the same package, widely used in the MVC design pattern

☃ package can contain classes and sub-packages, divide the project level, easy to manage

☃ problem-solving class naming conflicts

☃ control access

JDK in the main pack

java.lang:

Java language contains some core classes, such as String, Math, Integer, System and
Thread, providing common functions

java.net:

Classes and interfaces with the network comprises performing related operations

java.io:

Comprising providing a variety of input / output functions of the class

java.util:

It contains utility classes, characteristics of the system as defined, the set of frame-based interface, using the date
-related functions calendar

java.text:

It contains some java format related classes

java.sql:

JDBC database contains java programming for related classes / interfaces

java.awt-:

Comprising a plurality of abstract classes constituting Window Toolkit (abstract window toolkits) These classes are used to build and manage applications a graphical user interface (GUI).


☍ import keywords

import keyword overview

Using Java classes defined in different packages, import statements required to introduce the specified class hierarchy required package
or all of classes (. *). The import statement tells the compiler where to find classes.

import keyword format

 . import package name class name;

note:

➥ use import explicitly import the package specified in the class or interface source file

➥ declaration statements and declarations between the package of the class

If you need to import multiple ➥ class or interface, a plurality of parallel import statement can explicitly

➥ can use similar java.util. * Manner, disposable import all the class or interface package util

➥ If the class or interface is introduced, or in the current packet under java.lang package, this may be omitted import statement

➥ If the same name in different packages in the code class. Then you need to use the full class name of the class specified in a way which is called class, such as com.nyist.www.Person p = new com.nyist.www.Person (); Person under the category of use com.nyist.www package

If the class already imported ➥ under java.x package. So if you need to use x pack subpackages under that kind of thing, still need to import

➥ use import static combinations: calling a class or static property or method on the interface


☍ this keyword

this keyword overview

Java this keyword are keywords used, the method can be used to point to the current instance of any object, may be called the current method of pointing object, or the current required when using a reference type object, this keyword more difficult to understand, and its role its meaning is very close

it internally method, i.e. the method belongs to an object reference

It is used in the internal structure, showing the object of this configuration is initializing

Properties ☃ this class can call, methods and constructors

Call this keyword

☃ when an object within the method need to use this method calls, use this

Specifically: We can use this to distinguish between attributes and local variables.

E.g:

class Person{
    private String name;
    private String game;
    public Person(){
        
    }
    public Person(String name){
        //构造器中初始化时使用this.属性为变量赋值,this指向正在初始化的对象
        this.name = name;
    }
    public Person(String name,String game){
        // this可以作为一个类中构造器相互调用的特殊格式
        this(name);
        this.game = game;
    }
    public void setName(String name){
        //当传入的形参与成员变量同名时,用this.属性来给变量赋值
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setGame(String game){
        this.game = game;
    }
    public String getGame(){
        return game;
    }
    public void likeGame(){
        System.out.println(name + "喜欢玩" + game);
    }
    public void getInfo(){
        System.out.println("Name:" + name);
        //this指向调用likeGame()方法的对象
        //如Person p = new Person("Tom","Basketball");
        //p.getInfo();  这时的this就是指向p
        this.likeGame();
    }
    public void getLover(Person p){
        // this指向当前对象,p指向传入的Person对象
        System.out.println(this.name + " like " + p.name);
    }
}

➥ in any method or configuration, a member variable or member if using methods of the current class can be added in front of this, to enhance readability of the program. But usually we are used to this omission.

When participating ➥ shaped member variables of the same name, if required or used in the method of the constructor member variable, this must be added to indicate that the variable is a member variable of the class

➥ When using this method to access the properties and, if not found in this class, looks from the parent class

➥ this format can be used as a special class constructor call each other

note:

➥ can use "the this (parameter list)" manner class constructor calls this constructor class other overloaded

➥ clear: the constructor can not be "this (parameter list)" method call itself constructor

➥ "this (parameter list)" must be declared in the first line of the constructor of the class

➥ In one class constructor, a statement can only "the this (parameter list)."


This blog and CSDN blog (ཌ ་.Asio Jun ་. ད) simultaneous release

Guess you like

Origin www.cnblogs.com/asio/p/12408138.html