星期四, 6月 16, 2011

20110616 Java 練習小記


package chapter12.lab3;
//實做介面


//宣告介面
interface iVehicle
{
//給予抽象的方法
void show();
}


class Car implements iVehicle
{
private int num;
private double gas;

public Car(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化車號"+num+"油量為"+gas);
}

public void show()
{
System.out.println("車號是"+num);
System.out.println("油量為"+gas);
}

}


class Plane implements iVehicle
{
private int flight;

public Plane(int f)
{
flight = f;
System.out.println("初始化"+flight+"班次的飛機");
}

public void show()
{
System.out.println("飛機班次是"+flight);
}
}




public class Sample3 {


public static void main(String[] args) {
//產生一個陣列
iVehicle ivc[];
ivc = new iVehicle[2];

ivc[0] = new Car(1234, 20.5);
ivc[1] = new Plane(232);

for (int i=0; i<ivc.length; i++)
{
ivc[i].show();
}


}


}


package chapter12.lab4;
//實做2個以上的介面

//交通工具介面
interface iVehicle
{
void vshow();
}

//材料介面
interface iMaterial
{
void mshow();
}

class Car implements iVehicle, iMaterial
{
private int num;
private double gas;
public Car(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化汽車, 車號為"+num+"油量為"+gas);
}
public void vshow()
{
System.out.println("車號是"+num);
System.out.println("油量為"+gas);
}
public void mshow()
{
System.out.println("汽車材質為鐵");
}
}

public class Sample4 {

public static void main(String[] args) {
Car car1 = new Car(1234, 20.5);
car1.vshow();
car1.mshow();

}

}


package chapter13.lab1;
//練習檔案的切割

public class Car {
private int num;
private double gas;
public Car()
{
num = 0;
gas = 0;
System.out.println("初始化汽車車號為"+num+"油量為"+gas);
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("將車號設定為"+num+"油量設定為"+gas);
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("油量為"+gas);
}

}


package chapter13.lab1;

public class Sample1 {

public static void main(String[] args) {
Car car1 = new Car();
car1.setCar(1234, 20.5);
car1.show();

}

}


package chapter13.lab5.pb;
//跨套件存取
public class Sample5 {

public static void main(String[] args) {
//如果要使用不同的套件,這邊要將套件的完整路徑列出來
chapter13.lab5.pc.Car car1 = new chapter13.lab5.pc.Car();
car1.show();

}

}


package chapter13.lab5.pc;

public class Car {
private int num;
private double gas;
public Car()
{
num = 0;
gas = 0;
System.out.println("初始化汽車,車號為"+num+"油量為"+gas);
}
public void setCar(int n, double g)
{
num =n;
gas = g;
System.out.println("設定車號為"+num+"油量為"+gas);
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("油量為"+gas);
}

}


package chapter13.lab6.pb;
// 使用 import 描述匯入

//匯入 chapter13.lab5.pc.Car的類別
import chapter13.lab5.pc.Car;

public class Sample6 {

public static void main(String[] args) {
//有使用匯入的敘述, 就不用列出所有的名稱, 使用 Car 就可以
Car car1 = new Car();
car1.show();
car1.setCar(1234, 20.5);
car1.show();

}

}


package chapter13.lab7.pa.sub;
//建立套件的階層

class Car
{
private int num;
private double gas;
public Car()
{
num = 0;
gas = 0;
System.out.println("初始化汽車");
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("設定車號為"+num+"油量為"+gas);
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("油量為"+gas);
}
}

public class Sample7 {

public static void main(String[] args) {
Car car1 = new Car();
car1.show();

}

}

沒有留言: