ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 包装类 ~~~java package com.dodoke.demo1; public class Person { private String name; private Integer age; private Integer sex;//0表示女,1表示男 private String birthday; private Double height; private Double weight; private Boolean isMarry;//true 结婚 false 未婚 public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public Double getHeight() { return height; } public void setHeight(Double height) { this.height = height; } public Double getWeight() { return weight; } public void setWeight(Double weight) { this.weight = weight; } public Boolean getIsMarry() { return isMarry; } public void setIsMarry(Boolean isMarry) { this.isMarry = isMarry; } public Person() { super(); } public Person(String name, Integer age, Integer sex, String birthday, Double height, Double weight, Boolean isMarry) { super(); this.name = name; this.age = age; this.sex = sex; this.birthday = birthday; this.height = height; this.weight = weight; this.isMarry = isMarry; } } ~~~ ~~~java package com.dodoke.demo1; public class Test { public static void main(String[] args) { //实例化person对象 Person person = new Person(); //在java中,使用基本数据类型作为成员变量的时候,都会存在默认值。在很多时候这些值都是有特定的意义的,所以不能随便设置值 //因为存在自动装箱和自动拆箱,直接设置和获取没有任何问题 person.setName("陆文鑫"); person.setAge(27); person.setBirthday("yyyy-MM-dd"); person.setSex(1); person.setHeight(180.0); person.setWeight(80.0); person.setIsMarry(true); System.out.println(person.getName()); System.out.println(person.getAge()); System.out.println(person.getSex()); System.out.println(person.getBirthday()); System.out.println(person.getHeight()); System.out.println(person.getWeight()); System.out.println(person.getIsMarry()); /** * 从基本数据类型转变为引用数据类型 */ //从构造器创建 int a = 10; Integer a1 = new Integer(10);//使用基本数据类型参数 Integer a2 = new Integer("10");//使用字符串参数 //自动装箱 -基本数据类型的变量可以直接转化为对应包装类类型的变量 Integer a3 = a; System.out.println("a:" + a +",a1:" + a1 + ",a2:" + a2 + ",a3:" + a3); /** * 从引用数据类型转变为基本数据类型 */ //对象.xxxValue(); Boolean b = true; boolean b1 = b.booleanValue(); int b2 = a1.intValue(); //自动拆箱-引用数据类型(包装类)可以直接转化为对应基本数据类型变量 boolean b3 = b; int b4 = a2; System.out.println("b:" + b +",b1:" + b1 + ",b2:" + b2 + ",b3:" + b3 + ",b4:" + b4); } } ~~~