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

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

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

復(fù)制代碼 代碼如下:

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了!正常的結(jié)果

          /*
           * 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了!因?yàn)殪o態(tài)方法達(dá)不成你要的效果

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

復(fù)制代碼 代碼如下:

/**
 * 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;
}

相關(guān)文章

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

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

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

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

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

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

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

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

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

    tomcat connection-timeout連接超時(shí)源碼解析

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

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

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

    詳解如何將已有項(xiàng)目改造為Spring Boot項(xiàng)目

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

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

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

    Java線程生命周期的終止與復(fù)位

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

    一文弄懂fastjson

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

最新評(píng)論

大埔区| 大埔县| 沙湾县| 崇礼县| 疏勒县| 平阴县| 宣化县| 靖江市| 房山区| 台中市| 花莲县| 麦盖提县| 富川| 广宗县| 微博| 元谋县| 延川县| 临夏市| 潍坊市| 抚宁县| 新蔡县| 全椒县| 顺义区| 益阳市| 勃利县| 仪征市| 土默特左旗| 齐河县| 麦盖提县| 佛山市| 金门县| 衡阳市| 康马县| 德令哈市| 交口县| 龙游县| 中西区| 台州市| 长宁区| 拉萨市| 大化|