在从它的对象类直接访问的方法 [英] Accessing directly a method in a class from its object

查看:93
本文介绍了在从它的对象类直接访问的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个类只有一个方法,不叫可能时间却很少,然后,而不是调用该方法以传统方式如下

if a class has only one method which is not called may times but rarely, then instead of calling the method in a traditional way as below

RarelyCalledClass orarelyCalled = new RarelyCalledClass();
orarelyCalled.rarelyCalledMethod();



我可以如下调用它。

can i call this as below.

(new RarelyCalledClass()).rarelyCalledMethod();



这是否会提高性能的编译器所要做的操作少。

Will this increase the performance as compiler has to do less operation.

推荐答案

这将是完全一样的性能和代码。只是,你不能再访问该实例中的代码..而且可读性也差(在我和大多数人的意见)。

It will be exactly the same performance and code. Just that you can't access the instance anymore in your code.. And the readability is also worse (in my and most peoples opinion).

另外一些你总是应牢记: 过早的微优化是邪恶的

Also something you always should keep in mind: Premature micro optimization is evil.

简介您的应用程序。这是一个实际的瓶颈?没有?然后不要打扰。

Profile your application. Is this an actual bottleneck? No? Then don't bother.

这篇关于在从它的对象类直接访问的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆