The thirteenth day of Java basic learning (package mechanism, permission modifier, Jar package, template mode)

1. Package mechanism

1. Package: The package in java is equivalent to the windows folder.

2. The role of packages
① Solve the problem of repeated class name conflicts
② Facilitate the release of software versions

3. Define the format of the package: package package name;
package name naming convention: the package name is all lowercase.

cmd compilation:
javac -d specifies the storage path of the class file java source file
javac -d . java source file (under the current path)

4. Matters needing attention in the
package statement ① The package statement must be located in the first statement in the java file.
② If a package statement is added to a class, then the complete class name of the class is: package name.class name
③ A java file can only have one package statement.

5. With the package, the access between classes must be written with the package name every time?
Solution: sum provides a package import statement to let us solve this problem.
① The role of the package statement: simplify writing. (Misunderstanding: Import a class into memory)
② The format of the package import statement: import package name.class name; (import a class in the xxx package)

6. Details to be paid attention to in the package guide statement:
① Multiple package guide statements can appear in a java file.
② "*" is the wildcard of the package import statement. Can match any class name.
③ import aa.*; will not act on the subpackages under the aa package.
Recommended use: import packagename.classname; because the use of * wildcards will lead to unclear structure.

7. When to use the import statement
① The two classes that access each other are not under the same package. In this case, the package import statement needs to be used.
② java.lang is imported by default, and we do not need to import it ourselves.

2. Permission modifiers

Permission Modifier: It is to control the scope visibility of the member being modified.

[large to small] public protected default (default default) private
the same class true true true true
the same package true true true true
child parent class true true false false
different packages true false false false

Note: Only public and protected can be accessed under different packages, and protected must be accessed under inheritance relationship.

3. Jar package

1. Make a jar package: you need to use the jdk development tool (jar.exe)

2. Usage of jar
① Use format: jar cvf jar file name class file or folder
② Notes for jar package:
◆ After a program finishes jar, it must specify the entry class on the manifest file, the format is Main -Class: Package name.Class name
◆ Double-clicking the jar package only works for programs with a graphical interface, but not for programs in the console.

3. The role of the jar file:
① It is convenient for users to quickly run a project.
② Provide tool classes in the form of jar packages for others to use.
If you use the classes in the jar package, you must first set the classpath path.

package qq;
import javax.swing.*;
class Demo13.1{
    public static void main(String[] args){
        System.out.println("QQ程序..");
        JFrame frame = new JFrame("QQ程序");
        frame.setSize(400,500);
        frame.setVisible(true); //设置窗口可见。
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

4. Template mode

1. Template mode: Some of the steps to solve a certain kind of thing are fixed, and some are subject to change, so at this time we can provide a template code for this kind of thing to improve efficiency.

2. Steps of template mode
① Write a solution to solve one of these things first.
② Analyze the code, extract the code that will change and separate it into a method, and describe the method as an abstract method.
③ Use final to modify template methods to prevent others from overriding your template methods.

abstract class MyRuntime{   
    public final void getTime(){
        long startTime = System.currentTimeMillis();//记录开始的时间
        code();
        long endTime = System.currentTimeMillis(); //记录结束的时间.
        System.out.println("运行时间 :"+ (endTime-startTime));
    }
    public abstract void code();
}
class Demo13.2 extends MyRuntime{
    public static void main(String[] args){
        Demo13.2 d = new Demo13.2();
        d.getTime();
    }   
    //code方法内部就写要计算运行时间的代码
    public void code(){
        int i = 0;
        while(i<100){
            System.out.println("i="+i);
            i++;
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325946835&siteId=291194637