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.


Put your menu in a div. Instead of doing a foreach on each element, animate the main div.

Delay should not be causing your issue as all that does is wait to begin the animation timing, but having that many animations going at once is starting several internal timers instead of just one, which could lead to hiccups.

4

3 に答える 3

3

Java には演算子のオーバーロードがありません。したがって、構文は次のとおりです。

vector.get(0).generate();

ただし、 があり、 にはメソッドがないため、これはまったく機能Vector<Object>Objectませんgenerate

[補足:vector事実上非推奨です。ArrayListおそらく代わりに使用する必要があります。]

于 2012-04-05T22:58:38.380 に答える
2

vector.get(0)オブジェクトを取得するために使用する必要があります。

また、それObjectは宣言しないことに注意してください。そのため、オブジェクトをジェネリックgenerate()としてキャストまたは指定する必要があります。

于 2012-04-05T22:58:50.070 に答える
0

がある場合Vector<Object>、すべての取得メソッドが を返すObjectため、明示的にダウンキャストしない限り、サブクラス メソッドを呼び出すことはできません。Vector<YourClass>ベクトルから取得する参照が型YourClassであり、それらをダウンキャストする必要がないように、代わりに使用する必要があります。

于 2012-04-05T22:59:28.513 に答える