随笔 - 30, 文章 - 0, 评论 - 27, 引用 - 0
数据加载中……

默认的构造方法必须为public吗

声明:下面代码为仿照视频所有,因为笔误原因,发现了个问题,原因不明,哪位朋友看后帮忙解释一下,谢谢
 1import java.lang.reflect.Field;
 2import java.lang.reflect.Method;
 3
 4public class ReflctTester {
 5    
 6    public Object copy(Object object) throws Exception{
 7        Class<?> classType = object.getClass();
 8        System.out.println("Class :"+ classType.getName());
 9        //默认构造方法必须为public,不明白为什么换作friendly,protected却不行,而显示的构造方法却可以
10        Object objectCopy = classType.getConstructor(new Class[]{}).newInstance(new Object[]{});
11        Field fields[] = classType.getDeclaredFields();
12        for(int i=0;i<fields.length;i++){
13            Field field = fields[i];
14            String fieldName = field.getName();
15            String firstLetter = fieldName.substring(0,1).toUpperCase();
16            String getMethodName = "get"+firstLetter+fieldName.substring(1);
17            String setMethodName = "set"+firstLetter+fieldName.substring(1);
18            Method getMethod = classType.getMethod(getMethodName,new Class[]{});
19            Method setMethod = classType.getMethod(setMethodName, new Class[]{field.getType()});
20            Object value = getMethod.invoke(object, new Object[]{});
21            System.out.println(fieldName+":" + value);
22            setMethod.invoke(objectCopy,new Object[]{value});
23        }

24        return objectCopy;
25    }

26    
27    public static void main(String[] args) throws Exception {
28        Customer customer = new Customer("Tom"21);
29        customer.setId(new Long(1));
30        Customer customerCopy = (Customer) new ReflctTester().copy(customer);
31        System.out.println("Copy information:" + customerCopy.getId() + " " + customerCopy.getName() + " " +customerCopy.getAge());
32    }

33
34}

35
36class Customer{
37    private Long id;
38    private String name;
39    private int age;
40    protected Customer(){}
41    public Customer(String name,int age){
42        this.name = name;
43        this.age = age;
44    }

45    public int getAge() {
46        return age;
47    }

48    public void setAge(int age) {
49        this.age = age;
50    }

51    public Long getId() {
52        return id;
53    }

54    public void setId(Long id) {
55        this.id = id;
56    }

57    public String getName() {
58        return name;
59    }

60    public void setName(String name) {
61        this.name = name;
62    }

63}

上面的例子中如果默认的构造方法不为public则报
Exception in thread "main" java.lang.NoSuchMethodException: <init>()异常
问一个朋友得知:<init>()这个就是默认构造方法的签名
而<cinit>() 是类的初始化方法签名,第一次加载类时报错

posted on 2008-03-03 21:32 石业海 阅读(2339) 评论(1)  编辑  收藏 所属分类: JAVA基础

评论

# re: 默认的构造方法必须为public吗  回复  更多评论   

可以将代码中的第10行的classType.getConstructor(.....)改成:
classType.getDeclaredConstructor(.....);

Constructor<T> getConstructor(Class... parameterTypes)
返回一个 Constructor 对象,它反映此 Class 对象所表示的类的指定public构造方法。

Constructor<T> getDeclaredConstructor(Class... parameterTypes)
返回一个 Constructor 对象,该对象反映此 Class 对象所表示的类或接口的指定构造方法。
2008-03-26 19:41 | Ms.zhang

只有注册用户登录后才能发表评论。


网站导航: