Topic: 随笔1--静态方法的使用(转载)

  Print this page

1.随笔1--静态方法的使用(转载) Copy to clipboard
Posted by: liuyongsu
Posted on: 2002-12-10 22:10

发信人: lucan (采菊东篱者), 信区: Java
标 题: Lucan随笔1--静态方法的使用
发信站: 北大未名站 (2001年11月26日18:14:41 星期一) , 站内信件

什么时候使用静态方法:
静态方法与一般方法在使用上是有所区别的
静态方法可以不需要建立类的实例就可以直接调用
但是一般方法需要建立实例

public class MrHappyObject { 

private String _mood = _HAPPY;

private final static String _HAPPY = "happy";
private final static String _ANNOYED = "annoyed";
private final static String _ANGRY = "angry";

public void printMood() {
System.out.println( "I am " + _mood );
}

public void receivePinch() {
if( _mood.equals( _HAPPY ) ) {
_mood = _ANNOYED;
} else {
_mood = _ANGRY;
}
}

public void receiveHug() {
if( _mood.equals( _ANGRY ) ) {
_mood = _ANNOYED;
} else {
_mood = _HAPPY;
}
}
}

如上,由于一般方法需要建立实例,因此他们调用的结果和每个实例的状态是有关系的
Java Q&A

Mr. Happy Object teaches static methods
When to choose static methods over instance methods

By Tony Sintes

Printer-friendly version | Mail this to a friend

Advertisement

November 21, 2001

When would you create static methods as opposed to instance methods? I understa
nd that static methods allow you to use those methods without having to create a
n instance of that class, and that class methods apply to the class rather than
an object. Are these the only reasons? Could you give an example of a case where
you would use a class method over an instance method?

Many Java developers find it confusing to decide when, and when not to declare a
method as static. However, making the choice is simple if you have a clear unde
rstanding of the difference between a class method and an instance method.

Note: You can download the source code that accompanies this article from Resour
ces.

Consider the following class definition:

public class MrHappyObject { 

private String _mood = _HAPPY;

private final static String _HAPPY = "happy";
private final static String _ANNOYED = "annoyed";
private final static String _ANGRY = "angry";

public void printMood() {
System.out.println( "I am " + _mood );
}

public void receivePinch() {
if( _mood.equals( _HAPPY ) ) {
_mood = _ANNOYED;
} else {
_mood = _ANGRY;
}
}

public void receiveHug() {
if( _mood.equals( _ANGRY ) ) {
_mood = _ANNOYED;
} else {
_mood = _HAPPY;
}
}
}


Figure 1. Mr. Happy Object

First, before I get emails about it, there are more object-oriented ways to trac
k and transition between states. However, those fancy ways would detract from th
e intent of the example. Now, without further ado...

printMood(), receivePinch(), and receiveHug() are all instance methods. Syntacti
cally, you call these methods instance methods because they are not static; but
the important distinction concerns why I didn't declare them as static.

Instance methods are instance methods because they rely on the state of the spec
ific object instance. Instance methods are tied to a particular instance because
the behavior that the method invokes relies upon the state of that particular i
nstance.

Consider the following example:

MrHappyObject obj1 = new MrHappyObject(); 
MrHappyObject obj2 = new MrHappyObject();

obj1.printMood();
obj2.printMood();

obj1.receiveHug();
obj2.receivePinch();

obj1.printMood();
obj2.printMood();


大家可以看到上面的两个实例调用方法的结果是不一样的。
而静态方法调用的结果都是一样的,它是类一级的方法,与实例的状态没有关系。

private static int _instantiations;

public MrHappyObject() {

_instantiations++;
}

public static int instances() {
return _instantiations;
}

这个静态方法就记录了类创建实例的数量。
因此,如果你建立的类需要根据实例的状态进行处理那么就不要使用静态方法,如果对于不
同实例的状态结果都一样,那么就可以(注意是可以,而不是必须)使用静态方法

这是我第一天写随笔,很多东西都是从国外网站里看到的还加如了我的理解,很多地方翻译
的不好的地方请大家指正,多多和我探讨。谢谢

2.我也来 [Re: liuyongsu] Copy to clipboard
Posted by: supertoy
Posted on: 2002-12-12 00:50

以前看文章说static method的调用成本是none static method的3倍以上,从性能上不够合算,可是我自己做的实验是static method要快一些,有人能解释的清除么?

3.Can you explain how you did the test? [Re: liuyongsu] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-12 01:47


4.Re:我也来 [Re: supertoy] Copy to clipboard
Posted by: floater
Posted on: 2002-12-12 04:20

supertoy wrote:
以前看文章说static method的调用成本是none static method的3倍以上,从性能上不够合算,可是我自己做的实验是static method要快一些,有人能解释的清除么?


I wouldn't worry about the cost in general because every optimization at this stage is *very* likely a local opt value.

I would take the approach to finish coding and testing and then do a global optimization, rather than many local optimization. Before the global optimization, there are just so many things to worry about.

I was majored in optimization when I was an under, and picked up a few sayings along the way, this is the best approach I am using. Just my two cents, of course.

5.Re:我也来 [Re: floater] Copy to clipboard
Posted by: Biubiu
Posted on: 2002-12-12 15:37

floater wrote:
I wouldn't worry about the cost in general because every optimization at this stage is *very* likely a local opt value.

I would take the approach to finish coding and testing and then do a global optimization, rather than many local optimization. Before the global optimization, there are just so many things to worry about.

I was majored in optimization when I was an under, and picked up a few sayings along the way, this is the best approach I am using. Just my two cents, of course.


agree

6.Re:随笔1--静态方法的使用(转载) [Re: liuyongsu] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-11-17 12:51

(digging post action)

7.Re:随笔1--静态方法的使用(转载) [Re: liuyongsu] Copy to clipboard
Posted by: magin
Posted on: 2004-11-18 14:52

“静态”跟“多态”是对立的。重载机制体现了“多态”性,那么静态的方法和变量将不能被重载。实际上即使是非静态的变量也不能被重载。
只要是“静态的”那么我门逆着重载的“多态”去考虑,目前还没有发现问题。
如:“多态性说超类可以引用子类,超类引用子类以后,其成员方法还是子类的方法”。这时对非静态而言的,那么我门对静态的方法就采用相反的考虑“超类可以引用子类,超类引用子类以后,其静态成员方法就不是子类的方法了”。
大家看我这种思路有无不妥?


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923