2

I am trying to use a vector to hold my classes. These classes inherit methods from another class and have their own methods as well. What I am having trouble with is using the vector with objects to call the methods from the class within the object. I thought it would be something like:

public static void vSort(Vector<Object> vector) {
vector[0].generate();
}

with generate being a custom method i created with the student class within the object.

A better example

public class Method {
protected String name;

public void method() {
// some code
}
}
public class Student extends Method {
protected String last;

public void setUp() {
// some code
}
}
public class Main {
public static void main(String[] args) 
{
Vector<Object> vector = new Vector<Object>();
Student stu = new Student(); // pretend this generates something

vector.add(stu);

}

The problem i am running into is there are many classes like student that build on Method. If i cant use Object that is fine with me but i need to access the code within the Method class.


Full disclosure: I have not used Turbine.

Having said that, I think it might solve your problem for you or at least show you how to solve it.

They have a Unity Nuget package here: http://nuget.org/packages/MvcTurbine.Unity

And you can find more detail on their codeplex site here: http://mvcturbine.codeplex.com/

Hope that helps.

4

3 回答 3

3

Java doesn't have operator overloads. So the syntax is:

vector.get(0).generate();

However, this won't work at all in your case, because you have a Vector<Object>, and an Object doesn't have a generate method.

[Tangential note: vector is de facto deprecated; you should probably use ArrayList instead.]

于 2012-04-05T22:58:38.380 回答
2

you should use vector.get(0) to retrieve your object.

Also note, that Object does not declare generate() - so you are going to need to cast or specify your object as the generic type.

于 2012-04-05T22:58:50.070 回答
0

当你有 aVector<Object>时,所有的检索方法都会返回Object,所以除非你明确地向下转换,否则你不能调用子类方法。您应该Vector<YourClass>改用,这样您从向量中得到的引用是类型的YourClass,您不必向下转换它们。

于 2012-04-05T22:59:28.513 回答