动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ Java教程 ]的信息

本月文章推荐
.Java中鲜为人知的缺点(中).
.Java图像技术.
.Java Servlet API说明文档(2.1a.
.SWT/JFace开发指南之了解Display.
.控制bean定制器的技巧.
.Java与JavaScript的通信lixiaolo.
.反汇编器-javap.exe.
.Java核心代码例程之:(EJB) Remo.
.java 学习之路.
.情人碰面的问题:JAVA代码概述.
.DateLastAccessed 属性.
.详解用J2EE架构企业级应用.
.用Struts实现模板.
.Java网络编程之URI、URL研究专题.
.JVM技术,反射与动态代理.
.程序员生活 J2EE学习者值得研究的.
.java Socket 通讯的代码例子.
.Java如何操作Word, Excel, PDF文.
.在Applet中实现数字签名.
.JDK 1.5编译中的一个奇怪问题.

静态方法的使用

发表日期:2008-1-5 |



  什么时候使用静态方法:
  静态方法与一般方法在使用上是有所区别的
  静态方法可以不需要建立类的实例就可以直接调用
  但是一般方法需要建立实例
  
  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;
  }
  
  这个静态方法就记录了类创建实例的数量。
  因此,假如你建立的类需要根据实例的状态进行处理那么就不要使用静态方法,假如对于不 同实例的状态结果都一样,那么就可以(注重是可以,而不是必须)使用静态方法 .
上一篇:关于GUI界面的布局与建立 人气:546
下一篇:Java的垃圾收集机制 人气:533
浏览全部Java的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-9-6 Movie34电影搜索引擎 v3.0
2008-9-6 wap2.0仿帝国建站喜用 v2.0
2008-9-6 免费人才招聘网 宽屏版 v3.01
2008-9-6 喜喔喔视频采集程序 v1.0 beta
2008-9-6 ASP客户管理系统
2008-9-6 主流驿站中秋祝福程序
2008-9-6 php实现msn协议的类
2008-9-5 Coppermine Photo Gallery v1.4.
2008-9-5 清松网络日记本 v2.4
2008-8-23 Mini WinMount V0.4
2008-8-23 Vista优化大师3.11正式版
2008-8-23 Wine 1.13
2008-8-23 KlipFolio 5.0 Build 5899-80
2008-8-23 Windows Sysinternals Desktops
2008-8-23 OneTap Movies1.2破解版
2008-8-23 AnnotaterPDF阅读1.1.503 破解版
2008-8-23 SoundMeter分贝测量仪 v1.0汉化破
2008-8-23 iDrum音乐节拍1.0破解版
  发表评论
姓 名: 验证码:
内 容:
站长工具:网站收录查询 | Google PR查询 | ALEXA排名查询 | CSS在线编辑器 | 广告代码 | Html转换js | js/vbs加密 | md5加密 | 进制转换
实用工具:汉字翻译拼音 | 符号对照表 | 个税计算 | 经典小工具 | 汉字简繁转换 | 普通单位换算 | 公制单位换算 | 生辰老黄历 | 国内电话区号 国家代码与域名缩写 | 文字加密解密 | 健康查询 | 万年历 | 汉字横竖排版 | 手机号码查询 | 计算器 | ip搜索
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号