亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間: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了!因為靜態(tài)方法達(dá)不成你要的效果

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

復(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)文章

  • MyBatisPlus分頁的同時指定排序規(guī)則說明

    MyBatisPlus分頁的同時指定排序規(guī)則說明

    這篇文章主要介紹了MyBatisPlus分頁的同時指定排序規(guī)則說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • DDD框架落地實戰(zhàn)

    DDD框架落地實戰(zhàn)

    這篇文章主要為大家介紹了DDD框架落地實戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 深入理解Java設(shè)計模式之原型模式

    深入理解Java設(shè)計模式之原型模式

    這篇文章主要介紹了JAVA設(shè)計模式之原型模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2021-11-11
  • spring MVC搭建及配置詳解

    spring MVC搭建及配置詳解

    本篇文章主要介紹了spring MVC配置方法,要想靈活運(yùn)用Spring MVC來應(yīng)對大多數(shù)的Web開發(fā),就必須要掌握它的配置及原理,有興趣的可以了解一下。
    2017-01-01
  • mybatis plus 的動態(tài)表名的配置詳解

    mybatis plus 的動態(tài)表名的配置詳解

    這篇文章主要介紹了mybatis plus 的動態(tài)表名的配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java 模擬數(shù)據(jù)庫連接池的實現(xiàn)代碼

    Java 模擬數(shù)據(jù)庫連接池的實現(xiàn)代碼

    這篇文章主要介紹了Java 模擬數(shù)據(jù)庫連接池的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • WeakHashMap的使用方法詳解

    WeakHashMap的使用方法詳解

    這篇文章主要介紹了WeakHashMap的使用方法詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • Java IO流 文件傳輸基礎(chǔ)

    Java IO流 文件傳輸基礎(chǔ)

    這篇文章主要介紹了Java IO流 文件傳輸基礎(chǔ)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • java版十大排序經(jīng)典算法:完整代碼(4)

    java版十大排序經(jīng)典算法:完整代碼(4)

    優(yōu)秀的文章也不少,但是Java完整版的好像不多,我把所有的寫一遍鞏固下,同時也真誠的希望閱讀到這篇文章的小伙伴們可以自己去從頭敲一遍,不要粘貼復(fù)制!希望我的文章對你有所幫助,每天進(jìn)步一點點
    2021-07-07
  • IDEA導(dǎo)入Eclipse項目的方法步驟(圖文教程)

    IDEA導(dǎo)入Eclipse項目的方法步驟(圖文教程)

    這篇文章主要介紹了IDEA導(dǎo)入Eclipse項目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03

最新評論