在main函数中使用了this后,会提示出错。

原因是在main函数中不能使用this,main函数属于static,main中必须生成一个确定的对象来调用方法,凡是属于static的,this均不能用。

 

我想在main方法中执行 :URL url = this.getClass().getResource("/images/user.jpg"); 报错。

修改后:URL url = Test.class.getClassLoader().getClass().getResource("/images/user.jpg"); 

Test.class.getClassLoader().getClass()得到了一个实例,所以可以调用。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-24
  • 2022-01-22
  • 2021-10-20
  • 2022-12-23
  • 2021-12-14
猜你喜欢
  • 2021-09-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-20
  • 2021-09-15
  • 2021-10-31
相关资源
相似解决方案