🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
**1.com.nobb.bean.User** ~~~ public class User { private Integer id; private String name; private Car car; public User(){ System.out.println("调用了无参构造"); } public User(Integer id){ System.out.println("public User(Integer id)"); this.id = id; } public User(String name){ System.out.println("public User(String name)"); this.name = name; } public User(Integer id,String name){ System.out.println("public User(Integer id, String name)"); this.id = id; this.name = name; } public User(String name,Integer id){ System.out.println("public User(String name, Integer id)"); this.id = id; this.name = name; } public User(String name,Long id){ System.out.println("public User(String name,Long id)"); this.id = id.intValue(); this.name = name; } public User(Integer id,String name,Car car){ System.out.println("public User(Integer id, String name, Car car)"); this.id = id; this.name = name; this.car = car; } ~~~ **2.xml配置** ~~~ <!--用空参构造创建User对象--> <bean name="user1" class="com.nobb.bean.User"></bean> <bean name="user2" class="com.nobb.bean.User"> <!--调用public User(Integer id)--> <constructor-arg name="id" value="1"/> </bean> <bean name="user3" class="com.nobb.bean.User"> <!--调用public User(String name)--> <constructor-arg name="name" value="nobb"/> </bean> <bean name="user4" class="com.nobb.bean.User"> <!--public User(Integer id, String name)--> <constructor-arg name="id" value="1" index="0"/> <constructor-arg name="name" value="nobb" index="1"/> </bean> <bean name="user5" class="com.nobb.bean.User"> <!--public User(String name, Integer id)--> <constructor-arg name="id" value="2" type="java.lang.Integer"/> <constructor-arg name="name" value="test" index="0"/> </bean> <bean name="user6" class="com.nobb.bean.User"> <!--public User(String name,Long id)--> <constructor-arg name="id" value="2" type="java.lang.Long"/> <constructor-arg name="name" value="test" index="0"/> </bean> <bean name="car" class="com.nobb.bean.Car"> <property name="name" value="马萨拉蒂"/> <property name="color" value="橙色"/> </bean> <bean name="user7" class="com.nobb.bean.User"> <constructor-arg name="id" value="1"/> <constructor-arg name="name" value="小王"/> <constructor-arg name="car" ref="car"/> </bean> ~~~ **3.关键词总结** constructor-arg :构造参数 name:和变量定义的名称一致 type:变量的type类型 index:变量的顺序,从0开始 value:普通类型的值 ref:引用类型的值