[Core Java] 02 Java object-oriented foundation

Package

Class definition

class Type{
    
    
    public String _var; // 成员属性
    public Type(...){
    
     // 构造方法
        // ...
    }
    public void method)(...){
    
     // 成员方法
        // ...
    }
}

Member variable initialization:

class Type {
    
    
    private int field = 10;
}

Or you can use the return value of a method:

class Type {
    
    
    private int field = addObj();
    // ...
    private static int addObj() {
    
    
        // ...
        
    }
    // ...
}

If no explicit initialization is performed, member variables will be assigned default values:, 0 false nullthis is the main difference between member variables and local variables. Local variables will not be assigned default values ​​and must be explicitly initialized before they can be used.

Class access rights

In Java, access permissions such as public, private, and protected must be modified directly, rather than declared classified in C++.

class Type{
    
    
    public int field;
    public void method(){
    
    
        //...
	}
}

There are four ways to modify permissions in Java:

Modifier Access range
public All accessible
protected Accessible within the package and all subclasses
(default) Accessible in the package
private Only this type of access

Construction method

Overload

The overload mechanism of the construction method is basically the same as that of C++, so I won't go into details.

Delegation structure

Use thiskeywords to call other construction methods in the construction method.

class Type {
    
    
    // ...
    Type() {
    
    
        this(/* params */);
    }
    // ...
}

In C++, the delegate construction must be written in the initialization list.

Static domain/class domain

There are static methods and static variables in C++, which can be directly accessed through the class name.

In most object-oriented programming languages, this concept is referred to as a class domain , and the Java usage staticmodification just follows the name C++.

public static void main();
private static int field;

(Static) initialization block

Initialization block

When constructing the object, the code in the initialization block will be called first:

It can be used to construct objects, but it is generally not done (unless the construction code is to be reused), and it is necessary to avoid reading member variables initialized later.

class Type {
    
    
    {
    
    
 		// ...       
    }
}

Static initialization block

When the class is first loaded (regardless of the method), the static initialization block is directly called.

Similar to the static domain constructor.

class Type {
    
    
    static {
    
    
 		// ...       
    }
}

Object destruction

finalizeThe method of the object will be called before the object is cleaned up during the Java garbage collection period , and some finishing work can be done in this method.

A better method is to provide a closemethod for finishing, and the caller decides when to call, because we cannot determine finalizewhen the method will be called.

package

Overview

Packages are used to organize classes, to have a hierarchical structure between classes, and to reduce class name conflicts (this is the main purpose).

The package structure is similar (in fact, exactly the same) file organization structure:

A file path home/dev/class1.javacorresponds to the package --> home.dev.class1.

In fact, most people in the organization structure of the package, commonly used reverse domain name, for example, the author may be so named: top.gaolihai.

This approach comes from Sun’s suggestion:

To ensure the absolute uniqueness of the package name, Sun recommends that the company’s Internet domain name (which is obviously unique) be used as the package name in reverse order. And use different sub-packages for different projects. For example, horstmann.com is a domain name registered by one of the authors of this book. The reverse form is com.horstmann. This package can be further divided into sub-packages, such as com.horstmann.corejava.

—— Core Java I (P131)

Add the class to the package

Write at the beginning of the file:

package /* ... */;

E.g:

package top.gaolihai.test;

Then the class should be placed in the ./top/goalihai/testdirectory, used when compiling, and when javac top/gaolihai/test/Src.javacalling the interpreter java top.gaolihai.test.Src.

When we do not specify the package of the class file, the class will be located in a default package (no name), sharing a package scope with other classes that do not specify a package.

Import class

To access public classes in different packages, we can specify the complete package path before the class name:

java.util.LinkedHashMap<String, Integer> map = new java.util.LinkedHashMap(); 

Or use import:

Import this class:

import java.util.LinkedHashMap

Or import all classes under the package:

import java.util.*

You can use it directly at this time:

LinkedHashMap<String, int> map = new LinkedHashMap(); 

The scope of the class is in the entire package, so if it is in the same package as a certain class, it can be used directly without importing it.

The import statement is not similar to #include in C/C++, but more like using in C/C++. Correspondingly, packages are similar to namespaces in C/C++.

Import static domain

importIt can also be used to import all the static fields and static methods of a class (or import specific ones), for example, import Mathall the static fields and static methods under:

import java.lang.Math.*;

At this time, you can use it directly, instead of using Math:

round(/* val */);

Classpath

When calling third-party libraries (jar files, java files), we need to tell the compiler where these classes are.

Use the -cp(short for classpath) parameter when compiling, and then specify one or more .jarfiles or directories and separate them with :or ;.

java -cp {
    
    route};{
    
    route};...

For .javafiles, you can directly specify the parent directory, for .jarfiles, you must specify a specific file name or use wildcards *, for example ./lib/*.

For more details, see Java Core Technology Volume I --> P137

Guess you like

Origin blog.csdn.net/qq_16181837/article/details/112294806