Java 學習記錄56 — Packages

張小雄
3 min readApr 20, 2021

今天要學的是 Packages

如果我們沒有指定 Package

只打了這樣

Node node = null;

系統就會報錯

Cannot resolve symbol ‘Node’

因為同名的太多 系

import org.w3c.dom.Node;
import sun.tools.tree.Node;
public class Main {
public static void main(String[] args) {
sun.tools.tree.Node node = null;
org.w3c.dom.Node node321 = null;
}
}

系統不知道你要用那個

我們就要在上方 import

import jdk.nashorn.internal.ir.Node;

也可以在前面指定

org.w3c.dom.Node node = null;

這樣不用 import 系統也能接受

但是不能 import 兩個同名

import org.w3c.dom.Node;
import sun.tools.tree.Node;
public class Main {
public static void main(String[] args) {
sun.tools.tree.Node node = null;
org.w3c.dom.Node node321 = null;
}
}

系統就會在 line2 報錯

‘org.w3c.dom.Node’ is already defined in a single-type import

public class Main {
public static void main(String[] args) {
sun.tools.tree.Node node = null;
org.w3c.dom.Node node321 = null;
}
}

雖然移除掉 import intellij 沒有提示報錯

但一執行就錯誤

java: package sun.tools.tree does not exist

導出寫好的 package

點 project structure(左上 file 選單跟右上角圖片都有) → 左側點選第五個的 Arifacts → 點選旁邊上方 + 號 →

點選 jar 第二個 from modules with dependencies → 確認是否為要導出的 package ,若有指定第二行 Main Class 還能選輸出位置也可不指定,確認好點 ok →

之後點上方 Build → Artifacts → build 就完成導出啦

沒指定位置的話 就是在 out 裡面

如:java-the-complete-java-developer-course/test/out/artifacts/test_jar

如果要用命令行執行 可以到那個位置後

java -jar 檔案名

就可以在視窗內如 Terminal 執行程序了

導入 package

一樣打開 project structure 點選第三個的 Libraries 按旁邊 + 導入即可

小挑戰:

Creamain() method of your new project.te a suitably named package containing a class called Series

with the following static methods:

nSum(int n) returns the sum of all numbers from 0 to n. The first 10 numbers are:

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55.

factorial(int n) returns the product of all numbers from 1 to n

i.e. 1 * 2 * 3 * 4 … * (n — 1) * n.

The first 10 factorials are:

0, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800.

fibonacci(n) returns the nth Fibonacci number. These are defined as:

f(0) = 0

f(1) = 1

f(n) = f(n-1) + f(n-2)

(so f(2) is also 1. The first 10 fibonacci numbers are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.

When you have tested your functions, delete the Main class and

produce a jar file.

Create a new project and add your Series library, then test the

three methods in the

解法參考:

Series.java

package com.mylibrary;
public class Series {
public static int nSum(int n) {
// int num = 0;
// for (int i = 1; i <= n; i++) {
// num += i - 1;
// }
// return num;
if (n == 0) {
return 0;
} else
return n + nSum(n - 1);
} public static int factorial(int n) {
// int num = 1;
// for (int i = 1; i <= n; i++) {
// num *= i;
// }
// return num;
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else
return n * factorial(n - 1);
}
public static int fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}}

MainSeries.java

import com.mylibrary.Series;public class MainSeries {
public static void main(String[] args) {
for (int i = 0; i <= 10; i++) {
System.out.println(Series.nSum(i));
}
System.out.println("===============");
for (int i = 0; i <= 10; i++) {
System.out.println(Series.factorial(i));
}
System.out.println("===============");
for (int i = 0; i <= 10; i++) {
System.out.println(Series.fibonacci(i));
}
}
}

Tim Version:

package com.timbuchalka.mylibrary;/**
* Created by dev on 5/11/2015.
*/

public class Series {
public static long nSum(int n) {
return (n * (n + 1)) /2;
}
public static long factorial(int n) {
if(n == 0) {
return 0;
}
long fact = 1;
for (int i=1; i <= n; i++) {
fact *= i;
}
return fact;
}
public static long fibonacci(int n) {
if(n == 0) {
return 0;
} else if(n == 1) {
return 1;
}
long nMinus1 = 1;
long nMinus2 = 0;
long fib = 0;
for(int i= 1; i<n; i++) {
fib = (nMinus2 + nMinus1);
nMinus2 = nMinus1;
nMinus1 = fib;
}
return fib;
}
}

--

--