栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java类型转化

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java类型转化

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类型");
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/318433.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号