자바에서 임의의 타입에서 원시 타입으로 타입 캐스트를 정의하고 싶습니다. 한 임의 유형에서 다른 임의 유형으로 형변환을 정의 할 수 있습니까?자바에서 타입 캐스트 정의하기
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