开发者

Objects to arrays?

开发者 https://www.devze.com 2023-03-08 03:02 出处:网络
I need to make the following (X and Y are only examples): I have an X class. In that class I have objects, what I made. I have an another (Y) class, what extends the X class and implements Cloneable.

I need to make the following (X and Y are only examples):

I have an X class. In that class I have objects, what I made. I have an another (Y) class, what extends the X class and implements Cloneable. In the X class, which objects I made, need to put them in an array. Then I need to clone it to the Y class. How can I do it?

Here is the code:

(X) class

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);
        Syst开发者_StackOverflow中文版em.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

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


You're looking to deep clone something that isn't Cloneable, perhaps the answer to this question will help you: Deep clone utility recomendation

On a side note: Is a Garage a Vehicle? If not (I think not), then you should also re-think your design. Think about composition instead of inheritance.


I'm pretty sure you have this the wrong way around. Vehicle needs to be Cloneable and Garage (which shouldn't extend vehicle) doesn't.

To clone a list of classes (objects), you first need to store the object somewhere. You could store the objects in the constructor of the vehicle class as a static data member

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

And then access this list to clone them in the garage. This seems a little messy though.

Alternatively, you could store the list in the main method and then pass that list to a method in the garage.


Not sure if we share the same idea of cloneing - if you clone an instance of Y then the clone has the same (or equal) properties as the original instance - no extra arrays.

It's easier if you use a different technique to create a clone of a Y instance: the copy constructor. It goes like this:

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);
  }
}

Now if you want to clone a Garage, you simply do:

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

Note - my example is quite simple, I don't clone the internal properties (no deep cloning), only references.


Note - in real world, a Garage is a container for Vehicles. That's what you try to model? Here's a starter:

 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;
   }

   // ...
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消