最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javz筆記之:有趣的靜態(tài)方法的使用

 更新時間:2013年04月26日 16:49:56   作者:  
本篇文章介紹了,java中靜態(tài)方法的使用介紹,需要的朋友參考下

復制代碼 代碼如下:

import java.util.*;

public class welcome {

    public static void main(String[] args)
       {
          /*
           * Test 1: Methods can't modify numeric parameters
           */
          System.out.println("Testing tripleValue:");
          double percent = 10;
          System.out.println("Before: percent =" + percent);
          percent = tripleValue(percent);
          System.out.println("After: percent =" + percent);  //這里輸出為30了!正常的結果

          /*
           * Test 2: Methods can change the state of object parameters
           */
          System.out.println("\nTesting tripleSalary:");
          Employee harry = new Employee("Harry", 50000);
          System.out.println("Before: salary =" + harry.getSalary());
          tripleSalary(harry);
          System.out.println("After: salary =" + harry.getSalary());

          /*
           * Test 3: Methods can't attach new objects to object parameters
           */
          System.out.println("\nTesting swap:");
          Employee a = new Employee("Alice", 70000);
          Employee b = new Employee("Bob", 60000);
          System.out.println("Before: a  =" + a.getName());
          System.out.println("Before: b  =" + b.getName());
          swap(a, b);
          System.out.println("After: a=" + a.getName());
          System.out.println("After: b=" + b.getName());
       }

       public static double tripleValue(double x) // doesn't work
       {
          return x = 3 * x;
          //System.out.println("End of method: x=" + x);
       }

       public static void tripleSalary(Employee x) // works
       {
          x.raiseSalary(200);
          System.out.println("End of method: salary=" + x.getSalary());
       }

       public static void swap(Employee x, Employee y)
       {
          Employee temp = x;
          x = y;
          y = temp;
          System.out.println("End of method: x=" + x.getName());
          System.out.println("End of method: y=" + y.getName());
       }
    }

    class Employee // simplified Employee class
    {
       public Employee(String n, double s)
       {
          name = n;
          salary = s;
       }

       public String getName()
       {
          return name;
       }

       public double getSalary()
       {
          return salary;
       }

       public void raiseSalary(double byPercent)
       {
          double raise = salary * byPercent / 100;
          salary += raise;
       }

       private String name;
       private double salary;
    }


如果是以下代碼:System.out.println("After: percent =" + percent);  //這里輸出為10了!因為靜態(tài)方法達不成你要的效果

這是因為靜態(tài)方法不能對對象產(chǎn)生效果,和靜態(tài)域一樣,它屬于類,不屬于任何對象。

復制代碼 代碼如下:

/**
 * This program demonstrates parameter passing in Java.
 * @version 1.00 2000-01-27
 * @author Cay Horstmann
 */
public class ParamTest
{
   public static void main(String[] args)
   {
      /*
       * Test 1: Methods can't modify numeric parameters
       */
      System.out.println("Testing tripleValue:");
      double percent = 10;
      System.out.println("Before: percent=" + percent);
      tripleValue(percent);
      System.out.println("After: percent=" + percent);

      /*
       * Test 2: Methods can change the state of object parameters
       */
      System.out.println("\nTesting tripleSalary:");
      Employee harry = new Employee("Harry", 50000);
      System.out.println("Before: salary=" + harry.getSalary());
      tripleSalary(harry);
      System.out.println("After: salary=" + harry.getSalary());

      /*
       * Test 3: Methods can't attach new objects to object parameters
       */
      System.out.println("\nTesting swap:");
      Employee a = new Employee("Alice", 70000);
      Employee b = new Employee("Bob", 60000);
      System.out.println("Before: a=" + a.getName());
      System.out.println("Before: b=" + b.getName());
      swap(a, b);
      System.out.println("After: a=" + a.getName());
      System.out.println("After: b=" + b.getName());
   }

   public static void tripleValue(double x) // doesn't work
   {
      x = 3 * x;
      System.out.println("End of method: x=" + x);
   }

   public static void tripleSalary(Employee x) // works
   {
      x.raiseSalary(200);
      System.out.println("End of method: salary=" + x.getSalary());
   }

   public static void swap(Employee x, Employee y)
   {
      Employee temp = x;
      x = y;
      y = temp;
      System.out.println("End of method: x=" + x.getName());
      System.out.println("End of method: y=" + y.getName());
   }
}

class Employee // simplified Employee class
{
   public Employee(String n, double s)
   {
      name = n;
      salary = s;
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }

   private String name;
   private double salary;
}

相關文章

  • SpringSecurity在單機環(huán)境下使用方法詳解

    SpringSecurity在單機環(huán)境下使用方法詳解

    本文詳細介紹了SpringSecurity和SpringBoot的整合過程,包括配置用戶認證、JSP頁面的使用、數(shù)據(jù)庫認證以及授權功能的實現(xiàn),感興趣的朋友一起看看吧
    2025-02-02
  • idea配置maven環(huán)境時maven下載速度慢的解決方法

    idea配置maven環(huán)境時maven下載速度慢的解決方法

    我們在idea配置maven環(huán)境的時候會發(fā)現(xiàn)maven更新慢的現(xiàn)象,解決辦法就是下載國內的鏡像包,完美解決下載速度慢的問題,文中有詳細的具體操作方法,并通過圖文介紹的非常詳細,需要的朋友可以參考下
    2024-02-02
  • Java中常用數(shù)據(jù)類型的輸入輸出詳解

    Java中常用數(shù)據(jù)類型的輸入輸出詳解

    本文主要介紹了Java中幾個常用的數(shù)據(jù)類型是如何輸入和輸出的,例如:Char型、int型、double型、數(shù)組、字符串等,對我們學習java有一定的幫助,感興趣的小伙伴可以跟隨小編一起學習學習
    2021-12-12
  • idea實現(xiàn)類快捷生成接口方法示例

    idea實現(xiàn)類快捷生成接口方法示例

    這篇文章主要介紹了idea實現(xiàn)類快捷生成接口方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • tomcat connection-timeout連接超時源碼解析

    tomcat connection-timeout連接超時源碼解析

    這篇文章主要為大家介紹了tomcat connection-timeout連接超時源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • Spring Security如何在Servlet中執(zhí)行

    Spring Security如何在Servlet中執(zhí)行

    這篇文章主要介紹了Spring Security如何在Servlet中執(zhí)行,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • 詳解如何將已有項目改造為Spring Boot項目

    詳解如何將已有項目改造為Spring Boot項目

    本篇文章主要介紹了如何將已有項目改造為Spring Boot項目,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Spring MVC獲取查詢參數(shù)及路徑參數(shù)代碼實例

    Spring MVC獲取查詢參數(shù)及路徑參數(shù)代碼實例

    這篇文章主要介紹了Spring MVC獲取查詢參數(shù)及路徑參數(shù)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • Java線程生命周期的終止與復位

    Java線程生命周期的終止與復位

    這篇文章主要介紹了Java線程生命周期的終止與復位,Java的線程狀態(tài)描述放在Thread類里面的枚舉類State中.總共包含了6中狀態(tài),具體詳情需要的小伙伴可以參考一下文章描述
    2022-07-07
  • 一文弄懂fastjson

    一文弄懂fastjson

    fastjson?是一個java語言編寫的高性能且功能完善的JSON庫,本文主要介紹了fastjson的使用,具有一定的參考價值,感興趣的可以了解一下
    2023-05-05

最新評論

皮山县| 扬中市| 漳平市| 长武县| 南安市| 瑞昌市| 铁力市| 伊金霍洛旗| 阿坝| 久治县| 铜梁县| 郧西县| 和林格尔县| 巴马| 桃源县| 和顺县| 田东县| 阜南县| 从化市| 图木舒克市| 舟曲县| 天门市| 鄂温| 祁阳县| 台北县| 武宁县| 错那县| 日照市| 灵山县| 新和县| 肃宁县| 广德县| 增城市| 澄迈县| 门头沟区| 长垣县| 民丰县| 汽车| 遂昌县| 许昌县| 麻城市|