1

ポリモーフィック コンストラクターを宣言する方法はありますか? (過負荷にならないようにしてください!)。

1 つのクラスと 1 つのインターフェイスのインスタンスである引数を指定したいのですが、これらの両方で新しいクラスを作成したくありません (クラスはインターフェイスを実装します)。

コンストラクターを宣言したいのですが、通常は次の構文を使用します。

class Clas {
  public Clas(Object obj) { ... }
}

しかし、これらの引数「obj」は Class1 と Interface2 のインスタンスであり、次のように想像します。

class CubeRotationThread {
  public CubeRotationThread(Cube c implements IRotable) {...}
  //...or...
  public CubeRotationThread(Cube c instanceof IRotable) {...}
}

...または、この引数に対してのみポリモーフィックを指定するその他の構文。クラスを作成したくありません: CubeRot extends Cube implement IRotable, すべてのキューブがこれらの機能を持っているわけではないからです. また、(Cube) CubeRot をキャストするという他のオプションは必要ありません。逆に、すべてのキューブを回転可能にします。おそらく IRotator は、キューブまたは他のクラスに実装することも、実装しないこともできます。

コンストラクターでチェックを使用したくありません。

if (obj instanceof Class) { myMethod(); }

新しいクラスを作りたくない

class CubeRotatorThread extends Cube implements IRotator {
}

...すべてのキューブが回転できるわけではなく、キューブだけが回転を実装しているわけではないためです。

ジェネリック クラスを使用したくない:

class CubeRotatorThread T extends Cube implements IRotator {
}

... T はクラスであり、オブジェクト/引数が必要なためです。

そして、私も作成したくありませんNotImplementsRotationException!投げ用。

多態的な引数が欲しい、本当の多態的なメソッドが欲しい

出来ますか?検索しましたが、コンストラクターでこれを行うための構文が見つかりませんでした。

他の言語 (おそらく C# または C++) の別のコード、ヘルプ ドキュメント、または定義ドキュメントで次のように見ました。

Constructor(Class1 (Class2) args);

これは私にとって奇妙です。そして、これが私が望んでいることかどうかはわかりません。

ポリモーフィック コンストラクターが存在しない場合は、Oracle が作成する必要があると思います。とても便利です。

class CubeRotationThread {
    public CubeRotationThread(Cube arg implements IRotator) {}
    public CubeRotationThread(Cube arg extends IRotator) {}
    public CubeRotationThread(Cube arg instanceof IRotator) {}
}
4

1 に答える 1

0
GENERIC METHOD
http://docs.oracle.com/javase/tutorial/java/generics/bounded.html
http://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html
http://docs.oracle.com/javase/tutorial/java/generics/boundedTypeParams.html

I FOUND IT ! :)

    public <Q extends Class1 & Iterf2 & Interf3> Return Method(Q Object) { }

Q is the private mixture class you want with Extends & implements & implements...

here is with multiple objects:


    public <Q extends Class1 & Iterf2 & Interf3, W extends Class2 & Inter1 & Inter2> Return Method(Q arg1, W arg2) { }



use commas to separate classes inside <>, and use & to inherit

use commas to inherit directli in a class declaration


Here is very complete:

    class Generic extends String implements Implements1, Implements2 {}

    class Generic <T extends Class1 & Interface1 & Interface2> extends Class2 <W extends 

    Class3 & Iterface 5> implements Interface3, Interface4 {
            public <Q extends Class2 & Interface4, R extends Class4 & Interface6> Generic(T wtf, Q arg, R arg) { }
        }
于 2012-12-02T23:09:28.887 に答える