开发者

Java Generics example- I dont get this?

开发者 https://www.devze.com 2023-03-28 19:21 出处:网络
Say I have a class called Car and within another class (the name doesn\'t matter) 开发者_开发百科I want to create a generic vector, which can accommodate type Car. However, I obviously want this vecto

Say I have a class called Car and within another class (the name doesn't matter) 开发者_开发百科I want to create a generic vector, which can accommodate type Car. However, I obviously want this vector to use generics and so be something like Vector<E> myVector etc. Assume the class Car has a method getCarMake().

If I had the following:

    public class myClass<E> {

        Vector<E> myVector = new Vector<E>();

        for(i=0; i<myVector.size(); i++){
           Car tempCar = myVector.get(i);
       sysout(tempCar.getCarMake());
    }

when I change Car tempCar to E tempCar, it wont allow me to call tempCar.getCarMake() without performing a cast of type Car on tempCar?? Surely there is away around this, or it wouldnt be very generic?

I wanted to have:

for(i=0; i<myVector.size(); i++){
   E tempCar = myVector.get(i);
   sysout(tempCar.getCarMake());
}


You can simply create your Vector like this

Vector<Car> myVector = new Vector<Car>();

By the way, if you're using a recent Java (younger than about 8 years) (stupid me, this is a generics question, of course you are) it'd be better to use ArrayList (wrap it in Collections.synchronizedList if you need thread-safety, but I don't think you do).

Update: I see you've edited the question. The answer here is simply to change your class declaration to

public class myClass<E extends Car> {


The point of generics is, that you can write a class that can control the kind of objects you put into it. With a Vector generics allow you to control the members, you can put into it. It also makes your code nicer, because you can write code like your first example.

Your second example makes no sense. If you have defined, that your Vector can only hold Cars, why would you not address them as such?


How about this ?

Vehicle Container is what you really want.

public class VehicleContainer<T extends Vehicle> {

    private Vector <T> vehicleList = new Vector<T>();

    public void enlistVehicles() {
        for (int i=0;i<vehicleList.size();i++) {
            T vehicle = vehicleList.get(i);
            System.out.println(vehicle.getMake());
        }
    }

Vehicle is the interface.

public interface Vehicle {

    public String getMake();
}

and Car is the concrete Implementation.

public class Car implements Vehicle {
    public String getMake() {
        return "Audi";
    }
}


The generic type E has no meaning without declaring it in the context of either a class or a method. Assuming you had a method that was defined like so --

public void <E extends Car> aMethod() {

Then you can do this inside that method.

Vector<E> carVector = new Vector<E>();
for(i=0; i<myVector.size(); i++){
   E tempCar = myVector.get(i);
   sysout(tempCar.getCarMake());
}


You are over-thinking your scenario. You defined what you want to do - create a collection that accepts only Car objects. Generics is the Java feature that is going to allow you to do this. Aka, having a collection that accepts only Cars and having a collection that uses Generics is not mutually exclusive, infact they are mutually dependent!

Without Generics you would create a Vector like so:

Vector vehicleVector = new Vector();

But of course, the collection created would promiscously accept objects of all kinds - rabbits, cars, bikes, bananas ...

But from Java 5 forward we get Generics, which is somewhat misleadingly named, but what it lets you do is enforce, at compile time, a set of object type constraints on other objects. So your code morphs into:

Vector<Car> vehicleVector = new Vector<Car>();

And now you don't have to worry about all the other pesky object types sneaking into your Car collection! Plus, you get to write less code! The magic that makes this possible is Generics. In other words, you already accomplished what you wanted to do :)

0

精彩评论

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

关注公众号