Instantiate Subclasses from Static Methods

I'm think perhaps there is not a way to do this, but I thought it worth asking. I want to do something like the following: public class Super { public static String print() { System.out.println(new Super().getClass().getSimpleName()); } public Super() {} } public class Subclass extends Super { public Subclass() {} public void main(String[] args) { Super.print(); Subclass.print(); } } My hope is to get the Super.print() to show "Super" and Subclass.print() to show "Subclass". I don't see how to do this from a static context however. Thanks for the help. I'm well aware that I can do this without static methods, and that I can pass a class into each method call. I don't want to do that as that requires redefining several static methods on many subclasses.
You might want to have Subclass extend Super so it can call print() and have print() return a string or change its signature to void...just an FYI.

以上就是Instantiate Subclasses from Static Methods的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏