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

首頁 > 編程 > Java > 正文

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

2019-11-26 16:10:16
字體:
供稿:網(wǎng)友

復(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)方法不能對對象產(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;
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 一级黄色大片在线观看 | 国产精品一区二区三区在线播放 | 国产精品久久久免费看 | 一边吃奶一边摸下娇喘 | 天海翼四虎精品正在播放 | 午夜视频在线看 | av7777777| 亚洲精品欧美二区三区中文字幕 | 成人在线视频网 | 精品一区二区在线观看 | 成人三级电影网站 | 日本a v免费观看 | 狠狠撸电影 | 日韩999| 欧美日韩爱爱视频 | 激情网站视频 | 成人辣文| 一区二区三区欧洲 | 欧美国产日韩在线观看成人 | 久久综合婷婷 | 又黄又爽免费无遮挡在线观看 | 亚洲成人在线视频网 | 亚洲国产精品久久久久制服红楼梦 | 久久性生活免费视频 | 日本在线一区二区 | 久久久成人精品 | 在线看免费观看av | 91在线视频在线观看 | 欧美a∨一区二区三区久久黄 | 免费一区在线 | 蜜桃传媒视频麻豆第一区免费观看 | 国产一区二区精品在线观看 | 久久精品99久久久久久2456 | 他也色在线视频 | h视频免费在线观看 | 日韩黄色片免费看 | 精品亚洲一 | 毛片一区二区三区四区 | 日本中文视频 | 成人在线视频免费播放 | 久久精品亚洲成在人线av网址 |