国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

靜態方法的使用

2019-11-18 13:20:46
字體:
來源:轉載
供稿:網友

  什么時候使用靜態方法:
  靜態方法與一般方法在使用上是有所區別的
  靜態方法可以不需要建立類的實例就可以直接調用
  但是一般方法需要建立實例
  
  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;
  }
  
  這個靜態方法就記錄了類創建實例的數量。
  因此,假如你建立的類需要根據實例的狀態進行處理那么就不要使用靜態方法,假如對于不 同實例的狀態結果都一樣,那么就可以(注重是可以,而不是必須)使用靜態方法 .

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 庄河市| 抚松县| 建平县| 酒泉市| 白城市| 奇台县| 宣恩县| 昌邑市| 普兰店市| 哈密市| 五寨县| 瓦房店市| 阿鲁科尔沁旗| 津市市| 桃源县| 逊克县| 玉山县| 黄浦区| 米脂县| 渝中区| 威海市| 昌图县| 东兰县| 明水县| 靖江市| 措勤县| 呼伦贝尔市| 房山区| 二连浩特市| 进贤县| 噶尔县| 揭东县| 湄潭县| 理塘县| 信宜市| 屯门区| 黑龙江省| 昆明市| 基隆市| 杂多县| 阿拉善盟|