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

一杯茶,一台电脑

文章分类

全部博文(93)

文章存档

2018年(4)

2017年(57)

2016年(32)

分类: Java

2017-01-22 14:09:43


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


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


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

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


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

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

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

  58. }


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


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

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

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

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

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

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

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