程式碼
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.print("這是編號 " + num + " 油量 " + gas + " 的汽車");
}
public void mShow()
{
System.out.print("材質是鋼鐵");
}
public void move(){
System.out.print("在路上奔跑");
}
public String toString()
{
return "這是編號 " + num + " 油量 " + gas + " 的汽車 材質是鋼鐵";
}
}
class Plane implements iVehicle,iMaterial
{
private int flight;
public Plane(int f)
{
flight = f;
System.out.println("生產了編號 " + flight + " 的飛機");
}
public void vShow()
{
System.out.print("這是編號 " + flight + " 的飛機");
}
public void mShow()
{
System.out.print("材料是鋁合金");
}
public void move()
{
System.out.print("在天空飛行");
}
public String toString()
{
return "這是編號 " + flight + " 的飛機 材料是鋁合金";
}
}
class C12P297
{
public static void main(String[] args)
{
Car[] icar = new Car[3];
for(int i = 0; i < icar.length; i++){
icar[i] = new Car(i+1, 2000.0);
}
Plane[] iplane = new Plane[3];
for(int i = 0; i < iplane.length; i++){
iplane[i] = new Plane(i+1);
}
System.out.println("\n查詢生產成果");
for(int i = 0; i < icar.length; i++){
System.out.print("序號 " + i + " ");
icar[i].vShow();
icar[i].mShow();
System.out.print(" 移動方式 ");
icar[i].move();
System.out.print("\n");
System.out.println("\t" + icar[i]);
System.out.println("\t### " + icar[i].getClass() + " ### ");
}
for(int i = 0; i < iplane.length; i++){
System.out.print("序號 " + i + " ");
iplane[i].vShow();
iplane[i].mShow();
System.out.print(" 移動方式 ");
iplane[i].move();
System.out.print("\n");
System.out.println("\t" + iplane[i]);
System.out.println("\t### " + iplane[i].getClass() + " ### ");
}
}
}
No comments:
Post a Comment