3

In Page 112 of CHAPTER 5 GENERICS in the book - Effective Java , these sentences appears

Just what is the difference between the raw type List and the parameterized type List<Object> ...While you can pass a List<String> to a parameter of type List, you can’t pass it to a parameter of type List<Object>

I tried this

public static void getMeListOfObjs(List<Object> al){
    System.out.println(al.get(0));
}
public static void main(String[] args) {

    List<Object> al = new ArrayList<Object>();

    String mys1 = "jon";

    al.add(mys1);

    getMeListOfObjs(al);


}

It runs without any error... Was that an error in book content? I am quoting from the second edition


moving mouse by a detected object in python

The thing I want to do is like this: My webcam reads the frame continously and detect an object that I trained my program for. My program successfully detects that object.

But, I want to move the mouse the direction I moved that object My program is in python.

Please, Suggest some Ideas or any tools. Any help will be appreciated

4

5 に答える 5

8

これを試して:

public static void getMeListOfObjs(List<? extends Object> al) {
    System.out.println(al.get(0));
}

public static void main(String[] args) {

    List<String> al = new ArrayList<String>();

    String mys1 = "jon";

    al.add(mys1);

    getMeListOfObjs(al);


}

List<String>一致しないため、これはコンパイルされませんList<Object>

@darijanがワイルドカードを指摘したように?タイプ chack を、その子孫であるすべてのクラスで拡張します。

ジェネリックとワイルドカードについてもっと読むことをお勧めします

于 2013-06-17T08:28:38.710 に答える
3

あなたが提供したコードは機能します。alあなたのリストを にしたいと思いますList<String>。次に、次のようにする必要がありますgetMeListOfObjs

public static void getMeListOfObjs(List<? extends Object> al) {
    System.out.println(al.get(0));
}

public static void main(String[] args) {
    List<String> al = new ArrayList<String>();
    String mys1 = "jon";
    al.add(mys1);
    getMeListOfObjs(al);
}

違いに気づきましたか?

public static void getMeListOfObjs(List<? extends Object> al) {

ワイルドカード?は、拡張するすべてのタイプObject、つまりすべてのオブジェクトを変更します。

于 2013-06-17T08:30:50.533 に答える
1

作成した例では、List<String>. またList<Object>、オブジェクト リストに文字列を追加するだけです。したがって、基本的にオブジェクトリストを渡すため、機能します。文字列を含むオブジェクト リストに任意のオブジェクトを追加できます。String は Object を拡張するため、機能します。

于 2013-06-17T08:27:56.550 に答える
0

List<Object>あなたはaのみを通過しています。IS-Aに aStringを渡したList<Object>ことが懸念される場合は、それで問題ありません。StringObject

Stringの要素としてa を入れていても、メソッドにList<Object>a を渡しているList<Object>からです。

于 2013-06-17T08:28:33.950 に答える
0

本はあなたがこれを行うことができると言っています:

List al = new ArrayList<String>();

(ただし、Eclipseは生の型を使用しないことをお勧めします...「リストは生の型です。ジェネリック型リストへの参照はパラメーター化する必要があります」)

しかし、あなたはこれを行うことはできません:

List<Object> al = new ArrayList<String>();

コンパイラが型の不一致を認識するため、「ArrayList<String> から List<Object> に変換できません」

于 2013-06-17T08:30:00.960 に答える