xxx can not access a member of class xxxx with modifiers ""
2015-09-07 09:13
706 查看
今天做java反射测试,出现此错误,下面是代码:
public class GetClass {
public interface HasBatteries{}
public interface Waterproof{}
public interface Shoots{}
/**
* 默认构造器必须有
* You must define the required default constructor for up.newInstance( ); the
compiler can’t create it because a non-default constructor already exists.
* @author Administrator
*
*/
public static class Toy{
public Toy(){}
public Toy(int i){}
}
}
public class FancyToy extends Toy implements HasBatteries, Waterproof, Shoots {
public FancyToy(){
super(1);
}
}
public class GenericToyTest {
public static void main(String[] args) throws Exception {
Class<FancyToy> ftClass=FancyToy.class;
FancyToy fancy=ftClass.newInstance();
Class<? super FancyToy> up=ftClass.getSuperclass();
Object obj= up.newInstance();
System.out.println(obj);
}
}
Exception in thread "main" java.lang.IllegalAccessException: Class th.GenericToyTest can not access a member of class th.FancyToy with modifiers ""
网上各种查询,说Class前面要必须是public,但是我的就是public;最后各种调试,发现问题出在构造函数这块,构造函数是默认的修饰符,包可见的,
必须改成public,有继承关系的子类父类的构造函数也要修改成public,改完运行ok.
public class GetClass {
public interface HasBatteries{}
public interface Waterproof{}
public interface Shoots{}
/**
* 默认构造器必须有
* You must define the required default constructor for up.newInstance( ); the
compiler can’t create it because a non-default constructor already exists.
* @author Administrator
*
*/
public static class Toy{
public Toy(){}
public Toy(int i){}
}
}
public class FancyToy extends Toy implements HasBatteries, Waterproof, Shoots {
public FancyToy(){
super(1);
}
}
public class GenericToyTest {
public static void main(String[] args) throws Exception {
Class<FancyToy> ftClass=FancyToy.class;
FancyToy fancy=ftClass.newInstance();
Class<? super FancyToy> up=ftClass.getSuperclass();
Object obj= up.newInstance();
System.out.println(obj);
}
}
Exception in thread "main" java.lang.IllegalAccessException: Class th.GenericToyTest can not access a member of class th.FancyToy with modifiers ""
网上各种查询,说Class前面要必须是public,但是我的就是public;最后各种调试,发现问题出在构造函数这块,构造函数是默认的修饰符,包可见的,
必须改成public,有继承关系的子类父类的构造函数也要修改成public,改完运行ok.
相关文章推荐
- Java设计模式 -- 适配器模式
- 苹果通知推送服务(APNS)一些关键特性摘要
- 配置rsync服务器
- 国内VDI市场之乱象分析,第二篇:传输协议篇
- c 强大的vim配置文件,让编程更随意
- MongoDB学习四
- JavaScript常用标签和方法总结
- 前端模块化--这是我看过讲得比较好的东东
- 设置Chrome忽略网站证书错误
- html 表格小demo 1
- msp430 看门狗
- java静态方法是否能重写问题
- 源码推荐(9.07):Quartz纯代码绘制hellokit机器猫,滚动图片选取HySideScrollingImage
- Log 信息记录与查看
- eclipse启动:failed to create jvm
- Git 常用命令大全
- LTE学习:PHICH(二)
- 【LeetCode】之Valid Parentheses
- .NET Windows Form 改变窗体类名(Class Name)有多难?
- Objective-C 与 Runtime:为什么是这样?