NutzCN Logo
问答 org.nutz.lang.born.ConstructorCastingBorning怎么用
发布于 3092天前 作者 enilu 1871 次浏览 复制 上一个帖子 下一个帖子
标签:

看名字猜测应该是根据构造对象和范型参数来生成一个对象,不知道是不是这样子。
感觉跟ConstructorBorning应该差不多,但是ConstructorCastingBorning怎么用,范型相关参数如何传入,没看明白,
下面是ConstructorBorning的测试方法:

        Constructor constructor = School.class.getConstructor(String.class,String.class);
        ConstructorBorning a = new ConstructorBorning(constructor);
        Object b = a.born("北京大学","北京市海淀区");
        System.out.println(Json.toJson(b));
4 回复

说具体的构造方法

能传的只有构造方法参数中的泛型。

来自炫酷的 NutzCN

哦,born方法只能传构造参数中的范型类型哈

完整代码贴这里,有什么问题的话,兽兽请指出

public class ConstructorCastingBorningTest extends TestCase {

    public void testBorn() throws Exception {

        Constructor constructor = School.class.getConstructor(String.class,String.class);
        ConstructorBorning borning = new ConstructorBorning(constructor);
        Object school = borning.born("北京大学","北京市海淀区");
        System.out.println(Json.toJson(school));

        Constructor constructor2 = User.class.getConstructor(School.class);
        ConstructorCastingBorning borning2 = new ConstructorCastingBorning(constructor2);
        User user = (User) borning2.born(school);
        user.setUsername("zhangsan");
        System.out.println(Json.toJson(user));
    }
}
class User<T extends  School> {
    private String username;
    private T school;
    public User(T school){
        this.school =  school;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public T getSchool() {
        return school;
    }
    public void setSchool(T school) {
        this.school = school;
    }
}

class School {
    private String name;
    private String addr;
    public School(String name,String addr){
        this.name = name;
        this.addr = addr;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddr() {
        return addr;
    }
    public void setAddr(String addr) {
        this.addr = addr;
    }
}

所以你的User类并不需要传入泛型,爱传啥传啥

添加回复
请先登陆
回到顶部