麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發(fā)設計 > 正文

靜態(tài)方法的使用

2019-11-18 13:20:46
字體:
供稿:網(wǎng)友

  什么時候使用靜態(tài)方法:
  靜態(tài)方法與一般方法在使用上是有所區(qū)別的
  靜態(tài)方法可以不需要建立類的實例就可以直接調(diào)用
  但是一般方法需要建立實例
  
  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;
  }
  }
  }
  
  如上,由于一般方法需要建立實例,因此他們調(diào)用的結(jié)果和每個實例的狀態(tài)是有關系的
  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();
  
  大家可以看到上面的兩個實例調(diào)用方法的結(jié)果是不一樣的。
  而靜態(tài)方法調(diào)用的結(jié)果都是一樣的,它是類一級的方法,與實例的狀態(tài)沒有關系。
  private static int _instantiations;
  
  public MrHappyObject() {
  
  _instantiations++;
  }
  
  public static int instances() {
  return _instantiations;
  }
  
  這個靜態(tài)方法就記錄了類創(chuàng)建實例的數(shù)量。
  因此,假如你建立的類需要根據(jù)實例的狀態(tài)進行處理那么就不要使用靜態(tài)方法,假如對于不 同實例的狀態(tài)結(jié)果都一樣,那么就可以(注重是可以,而不是必須)使用靜態(tài)方法 .

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 91网视频 | 久草干| 黄色片免费视频 | 暴力强行进如hdxxx | 国内自拍网址 | 欧美日韩在线视频一区二区 | gril hd| 久草在线视频看看 | 91久久精品一区二区 | 久久国产精品小视频 | 欧美日韩成人一区二区 | 羞羞网站在线看 | 日本xxxx视频 | 久久久激情网 | 中文字幕综合在线观看 | 成人免费一区二区三区在线观看 | 午夜久久电影 | 毛片免费在线播放 | www.91pron| 国产在线1区 | 欧美一级黄色录相 | 激情亚洲一区二区 | 久色成人 | 中文字幕欧美日韩 | omofun 动漫在线观看 | 97超碰资源站 | 免费黄色入口 | 成人毛片100部免费观看 | 久久精品资源 | 亚洲精品在线观看网站 | 中文字幕国产一区 | 久久精品日产高清版的功能介绍 | 亚洲欧美一区二区三区在线观看 | 91网页视频入口在线观看 | 色淫网站免费视频 | 毛片免费看网站 | 羞羞视频免费网站 | 亚洲片在线 | 国产亚洲精品综合一区91555 | 99精品视频在线免费观看 | 国产精品久久久久久久久久久久午夜 |