Chinaunix首页 | 论坛 | 博客
  • 博客访问: 302294
  • 博文数量: 93
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 830
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-25 10:44
个人简介

一杯茶,一台电脑

文章分类

全部博文(93)

文章存档

2018年(4)

2017年(57)

2016年(32)

分类: Java

2017-02-02 17:53:47


  1. /*=============================================================================
  2. # FileName: ParamTest.java
  3. # Desc: 这个例子说明了在java当中过程参数,就算是传递的是引用,也是按照值传递的方式来进行的
  4. # Author: BarneyX
  5. # Email: vcmsdn@gmail.com
  6. # Version: 0.0.1
  7. # LastChange: 2016-01-04 10:22:16
  8. # History:
  9. =============================================================================*/
  10. public class ParamTest{
  11.     public static void main(String[] args){


  12.         /**
  13.          * Test 1:Methods can't modify numeric parameters
  14.          * */
  15.         System.out.println("Testing tripleValue");
  16.         double percent = 10;
  17.         System.out.println("Before: percent="+percent);
  18.         tripleValue(percent);
  19.         System.out.println("After: percent="+percent);


  20.         /**
  21.          * Test 2:Methods can change the state of object parameter
  22.          * */

  23.         System.out.println("\nTesting tripleSalary:");
  24.         Employee harry = new Employee("Harry",50000);
  25.         System.out.println("Before:salary="+harry.getSalary());
  26.         tripleSalary(harry);
  27.         System.out.println("After:salary="+harry.getSalary());


  28.         /**
  29.          * Test 3:Methods can't attch new objects to object paramets
  30.          * */
  31.         System.out.println("\nTesting swap");
  32.         Employee a = new Employee("Alice",70000);
  33.         Employee b = new Employee("Bob",60000);
  34.         System.out.println("Before:a="+a.getName());
  35.         System.out.println("Before:b="+b.getName());
  36.         swap(a,b);
  37.         System.out.println("After:a="+a.getName());
  38.         System.out.println("After:a="+a.getName());
  39.         
  40.     }
  41.     
  42.     public static void tripleValue(double x){
  43.         x= 3*x;
  44.         System.out.println("End of method: x="+x);
  45.     }

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

  50.     public static void swap(Employee x,Employee y){
  51.         Employee temp = x;
  52.         x= y;
  53.         y = temp;
  54.         System.out.println("End of method: x="+x.getName());
  55.         System.out.println("End of method: x="+y.getName());
  56.     }

  57. }


  58. class Employee{
  59.         private String name;
  60.         private double salary;


  61.         public Employee(String n,double s){
  62.             name = n;
  63.             salary = s;
  64.         }

  65.         public String getName(){
  66.             return name;
  67.         }

  68.         public double getSalary(){
  69.             return salary;
  70.         }

  71.         public void raiseSalary(double byPercent){
  72.             double raise = salary * byPercent / 100;
  73.             salary +=raise;
  74.         }
  75. }
  76. /*************************************************
  77.  C:\Windows\system32\cmd.exe /c (java ParamTest)
  78. Testing tripleValue
  79. Before: percent=10.0
  80. End of method: x=30.0
  81. After: percent=10.0

  82. Testing tripleSalary:
  83. Before:salary=50000.0
  84. End of method: salary=150000.0
  85. After:salary=150000.0

  86. Testing swap
  87. Before:a=Alice
  88. Before:b=Bob
  89. End of method: x=Bob
  90. End of method: x=Alice
  91. After:a=Alice
  92. After:a=Alice
  93. Hit any key to close this window...
  94. **************************************************/

阅读(852) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~