静态方法的使用
2008-01-05 09:53:03 来源:WEB开发网核心提示:什么时候使用静态方法: 静态方法与一般方法在使用上是有所区别的 静态方法可以不需要建立类的实例就可以直接调用 但是一般方法需要建立实例 public class MrHappyObject { PRivate String _mood = _HAPPY; private final static String _HAP
什么时候使用静态方法:
静态方法与一般方法在使用上是有所区别的
静态方法可以不需要建立类的实例就可以直接调用
但是一般方法需要建立实例
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;
}
这个静态方法就记录了类创建实例的数量。
因此,假如你建立的类需要根据实例的状态进行处理那么就不要使用静态方法,假如对于不 同实例的状态结果都一样,那么就可以(注重是可以,而不是必须)使用静态方法 .
[]
- ››使用脚本恢复WinXP系统的用户登录密码
- ››使用phpMyadmin创建数据库及独立数据库帐号
- ››使用Zend Framework框架中的Zend_Mail模块发送邮件...
- ››使用cout标准输出如何控制小数点后位数
- ››使用nofollow标签做SEO的技巧
- ››使用 WebSphere Message Broker 的 WebSphere Tra...
- ››使用SQL Server事件探查器做应用程序的性能分析
- ››使用SQL Server事件探查器分析死锁原因
- ››使用纯文本文件打造WCF服务
- ››使用 Dojo 开发定制 Business Space 小部件,第 4...
- ››使用 ADDRESS 与 INDIRECT函数查询信息
- ››使用 COLUMN函数编制单元信息
更多精彩
赞助商链接