开发者社区> 问答> 正文

子类如何调用父类的protected方法??报错

我尝试的方法:

1.直接调用,行不通。

2.强转,反射,也报错。


ProgressBar instance = (ProgressBar) this;
@SuppressWarnings("unchecked")
Class<ProgressBar> clazz = (Class<ProgressBar>) instance.getClass();
try {
    progressBarOnDraw = clazz.getDeclaredMethod("onDraw", Canvas.class);
			progressBarOnDraw.setAccessible(true);
} catch (NoSuchMethodException e) {
    e.printStackTrace();

		}

 if (progressBarOnDraw != null) {
    try {
	progressBarOnDraw.invoke((ProgressBar) this, canvas);
    } catch (IllegalAccessException | IllegalArgumentException
		 | InvocationTargetException e) {
	e.printStackTrace();
        }
} else {
    super.onDraw(canvas);
}





展开
收起
爱吃鱼的程序员 2020-06-14 15:16:25 383 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    <spanstyle="font-family:Consolas,'CourierNew',Courier,mono,serif;font-size:12px;line-height:18px;background-color:#F5FAE2;"> method.setAccessible(<spanstyle="color:blue;font-weight:bold;font-family:Consolas,'CourierNew',Courier,mono,serif;font-size:12px;line-height:18px;background-color:#F5FAE2;">true<spanstyle="font-family:Consolas,'CourierNew',Courier,mono,serif;font-size:12px;line-height:18px;background-color:#F5FAE2;">); 我设置了,还是不行呢。super.method();

    2020-06-14 15:16:41
    赞同 展开评论 打赏
问答分类:
问答地址:
相关产品:
问答排行榜
最热
最新

相关电子书

更多
建立联系方法之一 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载