任意の型からJavaのプリミティブ型への型キャストを定義したいと思います。任意のタイプから他の任意のタイプへのキャストを定義することは可能ですか?Javaでの型キャストの定義
public class Foo{
//methods, constructor etc for this class
...
//make it possible to cast an object of type Foo to an integer
}
//example of how an object of type foo would be cast to an integer
public class Bar(){
public static void main(String[] args){
Foo foo1 = new Foo();
int int1 = (int)foo1;
System.out.println(int1+"");
}
}
これは可能ではありませんが、おそらく誰かがこれを修正することができます。 – AHungerArtist
クラスに同じ変数がある限り、キャストのような構造体が存在することはありますか? –
@LeeScottいいえ、Javaは 'ClassCastException'をスローします – Dave