Java类型转化
先随意写两个类
public class User {
private long id;
private String name="zs";
private String password;
//提供无参、有参构造方法。setter,getter方法。覆写toString方法。
}
public class Student {
private Long id;
private String name;
private String password;
//提供无参、有参构造方法。setter,getter方法。覆写toString方法。
}
1、类型转化测试
public class ClassCastTest {
public static void main(String[] args) {
User user = new User(1L,"zs","1234567");
Student student = (Student)user;//编译报错
}
}
public class ClassCastTest {
public static void main(String[] args) {
Object object = new User(1L,"zs","1234567");//小转大,自动隐式转化
User user = (User)object;//大转小,强制转化
System.out.println(user);//User{id=1, name='zs', password='1234567'}
}
}
public class ClassCastTest {
public static void main(String[] args) {
Object object = new User(1L,"zs","1234567");
Student student = (Student)object;//java.lang.ClassCastException
}
}
2、instanceof关键字
public class ClassCastTest {
public static void main(String[] args) {
Object object = new User(1L,"zs","1234567");
cast(object);//User{id=1, name='zs', password='1234567'}
User user = new User();
cast(user);//User{id=0, name='null', password='null'}
Object object2 = new Student(1L,"xxs","666");
cast(object2);//Student{id=1, name='xxs', password='666'}
cast(new Student());//Student{id=null, name='null', password='null'}
cast("12345");//不属于User或者Student类型
}
private static void cast(Object object){
if (object instanceof User){
User user = (User)object;
System.out.println(user);
}else if(object instanceof Student){
Student student = (Student)object;
System.out.println(student);
}else {
System.out.println("不属于User或者Student类型");
}
}
}
3、Class对象比较
public class ClassCastTest {
public static void main(String[] args) {
Object object = new User(1L,"zs","1234567");
cast(object);//User{id=1, name='zs', password='1234567'}
User user = new User();
cast(user);//User{id=0, name='null', password='null'}
Object object2 = new Student(1L,"xxs","666");
cast(object2);//Student{id=1, name='xxs', password='666'}
cast(new Student());//Student{id=null, name='null', password='null'}
cast("12345");//不属于User或者Student类型
}
private static void cast(Object object){
if (object.getClass()==User.class){
User user = (User)object;
System.out.println(user);
}else if(object.getClass()==Student.class){
Student student = (Student)object;
System.out.println(student);
}else {
System.out.println("不属于User或者Student类型");
}
}
}
public class ClassCastTest {
public static void main(String[] args) {
Object object = new User(1L,"zs","1234567");
cast(object);//User{id=1, name='zs', password='1234567'}
User user = new User();
cast(user);//User{id=0, name='null', password='null'}
Object object2 = new Student(1L,"xxs","666");
cast(object2);//Student{id=1, name='xxs', password='666'}
cast(new Student());//Student{id=null, name='null', password='null'}
cast("12345");//不属于User或者Student类型
}
private static void cast(Object object){
if(User.class.equals(object.getClass())){
User user = (User)object;
System.out.println(user);
}else if(Student.class.equals(object.getClass())){
Student student = (Student)object;
System.out.println(student);
}else {
System.out.println("不属于User或者Student类型");
}
}
}