packageyubin;publicclassHuman{String name;int age, height, weight;publicHuman(Stringstr){ name = str;}// end of constructor(String)publicvoidsetValue(inta,inth,intw){ age = a; height = h; weight = w;}// end of setValue(int,int)}// end of class Human
package tina;
public class Human {
String name;
int age,height;
public Human(String str){
name = str;
}// end of constructor(String)
public void setValue(int a,int h){
age = a;
height = h;
}// end of setValue()
}//end of class Human
package run;
public class Test {
public static void main(String[] args) {
tina.Human h1 = new tina.Human("小婷");
h1.setValue(18, 162);
yubin.Human h2 = new yubin.Human("小木");
h2.setValue(22, 178, 63);
}// end of main(String[])
}// end of class Test
package 套件名稱;
package run;
public class Test {
public static void main(String[] args) {
tina.Human h1 = new tina.Human("小婷");
h1.setValue(18, 162);
yubin.Human h2 = new yubin.Human("小木");
h2.setValue(22, 178, 63);
}// end of main(String[])
}// end of class Test
public class Human{
// code...
}
套件名稱.類別名稱
套件名稱1.套件名稱2.套件名稱3.套件名稱4.套件名稱n.類別名稱
package run;
import tina.Human;
public class Test {
public static void main(String[] args) {
Human h1 = new Human("小婷");
h1.setValue(18, 162);
Human h2 = new Human("小木");
h2.setValue(22, 178);
}// end of main(String[])
}// end of class Test
import 完整類別路徑;
import tina.*; // 表示把tina套件中『所有類別』import進來
package run;
import tina.Human;
public class Test {
public static void main(String[] args) {
Human h1 = new Human("小婷");
h1.setValue(18, 162);
Human h2 = new Human("小木");
h2.setValue(22, 178);
yubin.Human h3 = new yubin.Human("阿杉");
h3.setValue(10, 100, 100);
// 物件h3 雖然也叫Human但跟h1、h2是完全不一樣的東西
}// end of main(String[])
}// end of class Test
package 自身套件;
import 目標套件.目標類別;
class 類別名{
// code...
}
System.out.println("Hello Java");
import java.lang.*; // Java會自動幫你加上這行(你看不到),不寫也沒關係
class Test {
public static void main(String[] args) {
java.lang.System.out.println("Hello Java"); //完整類別路徑
System.out.println("Hello Java"); // 一般寫法
}// end of main(String[])
}// end of class Test