2011-05-25 69 views
1

我需要做以下(X和Y只是例子):數組對象?

我有一個X類。在那個班裏我有對象,我做了什麼。我有另一個(Y)類,它擴展了X類並實現了Cloneable。在X類中,我創建了哪些對象,需要將它們放入數組中。然後我需要將它克隆到Y類。我該怎麼做?

下面是代碼:

(X)級

import java.util.Scanner; 

class Vehicle { 
    static int speed; 
    double direction; 
    protected String ownerName; 
    static int ID_number = 0; 
    int initID_number; 
    int ID; 
    Scanner s = new Scanner (System.in); 
    private final static String TURN_LEFT = "left"; 
    private final static String TURN_RIGHT = "right"; 

    Vehicle() { 
     ID = ID_number++; 
    } 

    public Vehicle(String initOwner) { 
     ownerName = initOwner; 
    } 

    public static void highestID() { 
     System.out.println("The highest ID is: " + ID_number); 
    } 

    public static void changeSpeed(int speedNow) { 
     speed = speedNow; 
    } 

    public static void stop(){ 
     speed = 0; 
    } 

    public static void turn(int degree) { 
     System.out.println("The car turn in "+ degree); 
    } 

    public static void turn(String turn) { 
     if (turn == TURN_LEFT) { 
      System.out.println("The car turn left"); 
     } 
     else if (turn == TURN_RIGHT) { 
      System.out.println("The car turn right"); 
     } 
    } 

    public static void main(String[] args) { 
     Vehicle Alfa = new Vehicle(); 
     Alfa.speed = 120; 
     Alfa.direction = 12.11; 
     Alfa.ownerName = "Alfa"; 
     Alfa.initID_number = 1; 

     Vehicle BMW = new Vehicle(); 
     BMW.speed = 100; 
     BMW.direction = 12.1411; 
     BMW.ownerName = "BMW"; 
     BMW.initID_number = 2; 

     Vehicle Chrysler = new Vehicle(); 
     Chrysler.speed = 90; 
     Chrysler.direction = 131.1210; 
     Chrysler.ownerName = "Chyrsler"; 
     Chrysler.initID_number = 3; 

     System.out.println("Speed: " + Alfa.speed); 
     System.out.println("direction: " + Alfa.direction); 
     System.out.println("ownerName: " + Alfa.ownerName); 
     System.out.println("ID_number: " + Alfa.ID); 
     System.out.println("initID_Number: " + Alfa.initID_number); 
     System.out.println(); 

     System.out.println("Speed: " + BMW.speed); 
     System.out.println("direction: " + BMW.direction); 
     System.out.println("ownerName: " + BMW.ownerName); 
     System.out.println("ID_number: " + BMW.ID); 
     System.out.println("initID_Number: " + BMW.initID_number); 
     System.out.println(); 

     System.out.println("Speed: " + Chrysler.speed); 
     System.out.println("direction: " + Chrysler.direction); 
     System.out.println("ownerName: " + Chrysler.ownerName); 
     System.out.println("ID_number: " + Chrysler.ID); 
     System.out.println("initID_Number: " + Chrysler.initID_number); 

     Vehicle Dacia = new Vehicle("Dacia"); 
     System.out.println(); 
     System.out.println("ownerName: " + Dacia.ownerName); 

     highestID();      

     changeSpeed(50); 
     stop(); 
     turn(50); 
     turn("right"); 

     } 
      } 

(Y)類

class Garage extends Vehicle implements Cloneable { 
    // need to make here the clone of the Vehicle's class array 
}  
+5

您需要向我們展示了代碼。 – 2011-05-25 14:22:10

+0

看來Garage不需要擴展Vehicle,但它應該包含一系列的車輛。 Garage.turn()應該做什麼?:\ – amit 2011-05-25 14:32:44

+2

爲什麼'Vehiche'類中的所有內容都是靜態的?你確實知道,當你設置'BMW.speed = 100'時,它設置車輛的靜態速度變量,然後當你設置'Chrysler.speed = 90'時,它會設置相同的靜態變量,重寫你爲寶馬設置的內容。 – 2011-05-25 14:33:34

回答

2

您正在尋找深克隆的東西是不可複製的,也許是這個問題的答案將幫助您:Deep clone utility recomendation

在一個側面說明:是一個Garage一個Vehicle ?如果沒有(我認爲不),那麼你也應該重新考慮你的設計。考慮composition而不是繼承。

1

不知道我們分享cloneing同樣的想法 - 如果你克隆的Y實例然後克隆具有相同的(或相等)性質的原始實例 - 沒有額外的陣列。拷貝構造函數:

如果使用不同的技術來創建一個Y實例的一個副本很容易。它是這樣的:

public class Vehicle { 
    static int speed; 
    double direction; 
    protected String ownerName; 
    // ... 

    Vehicle(Vehicle other) { 
    speed = other.speed; 
    ownerName = other.ownerName; 
    // ... 
    } 
} 

public class Garage extends Vehicle { // A Garage is-a Vehicle ?? .. so what. 

    Garage(Garage other) { 
    super(other); 
    } 
} 

現在,如果你想克隆一個Garage,你根本:

Garage original = getOriginal(); 
Garage myClone = new Garage(original); 

- 我的例子很簡單,我不克隆內部屬性(沒有深度克隆),只有引用。


- 在現實世界中,一個GarageVehicles一個容器。那是你試圖建模的嗎?這裏有一個首發:

public class Garage { 
    private Vehicle[] spaces = new Vehicle[10]; // like a garage for max 10 vehicles 

    public boolean parkVehicle(int slot, Vehicle vehicle) { 
    if (slot < 0 || slot >= spaces.length) return false; 
    if (vehicle == null || spaces[slot] != null) return false; 
    spaces[slot] = Vehicle; 
    return true; 
    } 

    // ... 
} 
1

我敢肯定,你有這樣的周圍走錯了路。車輛需要克隆和車庫(不應該擴展車輛)不。

克隆的類(對象)的列表,你首先需要存儲對象的地方。您可以將對象存儲在車輛類的構造函數中作爲靜態數據成員

protected static List<Vehicle> vehicles = new ArrayList<Vehicle>(); 
... 
public Vehicle(...) { 
    vehicles.add(this); 
} 

然後訪問此列表以在車庫中克隆它們。這似乎有點混亂。

或者,你可以在列表存儲在主方法,然後通過該列表在車庫的方法。