分享
 
 
 

静态方法的使用

王朝other·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

什么时候使用静态方法:

静态方法与一般方法在使用上是有所区别的

静态方法可以不需要建立类的实例就可以直接调用

但是一般方法需要建立实例

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 track and transition between states. However, those fancy ways would detract from the intent of the example. Now, without further ado...

printMood(), receivePinch(), and receiveHug() are all instance methods. Syntactically, 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 specific object instance. Instance methods are tied to a particular instance because the behavior that the method invokes relies upon the state of that particular instance.

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;

}

这个静态方法就记录了类创建实例的数量。

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有