方法参数注意三要点:

一个方法不能修改一个基本数据类型的参数(数值型或者布尔型)。

一个方法可以改变一个对象参数的状态。

一个方法不能让对象参数引用一个新的对象。

package testbotoo;

public class ParamTest{

    public static void main(String[] args)
{
/*
* Test 1 :Methods cant't modify numeric parameers
*/
System.out.println("Testing tripleValue");
double percent = 10;
System.out.println("before:percent="+percent);
tripleValue(percent);
System.out.println("after:perent="+percent); /*
* Test2 :Methods can change the state of object parameters
*/
System.out.println("\nTesting tripleSalary:");
Empl harry = new Empl("harry", 5000);
System.out.println("\n before salary = "+ harry.getSalary());
tripleSalary(harry);
System.out.println("after salary = "+ harry.getSalary()); /*
* Test 3 : nethods can't attach new objects to objects parameters
*/ System.out.println("\ntesting swap:");
Empl a = new Empl("sss",500);
Empl b = new Empl("bob", 600);
System.out.println("befoer: a = "+ a.getName());
System.out.println("befoer: 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)//dosen't work
{
x = 3*x;
System.out.println("x="+x);
} public static void tripleSalary(Empl x) //works
{
x.raiseSalary(200);
System.out.println("end of method : salary = "+ x.getSalary()); } public static void swap(Empl x ,Empl y)
{
Empl temp = x;
x = y ;
y = temp;
System.out.println("x = "+ x.getName());
System.out.println("y = "+ y.getName());
} } class Empl
{ private String name;
private double salary; public Empl(String n, double s)
{
name = n;
salary = s;
} public String getName()
{ return name;
} public double getSalary()
{
return salary;
} public void raiseSalary(double byPrecent)
{
double raise = salary * byPrecent /100;
salary += raise;
}
}

最新文章

  1. Elasticsearch增删改查 之 —— Get查询
  2. 【BZOJ-3626】LCA 树链剖分
  3. Leetcode: Perfect Rectangle
  4. 关于strlen
  5. <Araxis Merge>Windows平台下的Merge概览
  6. hdoj 2187 悼念512汶川大地震遇难同胞——老人是真饿了【贪心部分背包】
  7. Windows7添加SSD硬盘后重启卡住正在启动
  8. 怎样给win7系统硬盘分区
  9. JavaScript动漫作品(闭幕)
  10. Storm官方文档翻译之设置开发环境
  11. Docker 学习7 Dockerfile详解
  12. Cocos 2dx项目lua调用OC出现卡死但不闪退的坑
  13. json中获取key值
  14. doker学习笔记
  15. ==和equasl、hashmap原理(***)
  16. AngularJS之登录显示用户名
  17. 字符串截取函数slice, substring, substr
  18. crontab条目包含%号问题
  19. Event Tracing For Windows
  20. 【剑道】日常练习相关Q&A 整理

热门文章

  1. [GDOI2014]拯救莫莉斯 状压DP
  2. POJ3254:Corn Fields——题解
  3. BZOJ1568:[JSOI2008]Blue Mary开公司——题解
  4. BZOJ4539 [Hnoi2016]树 【倍增 + 主席树】
  5. 弱校的ACM奋斗史
  6. Django Session配置
  7. Multi-target tracking with Single Moving Camera
  8. ACE_Message_Block功能简介
  9. ubuntu启动脚本
  10. sub-G 无线芯片基础知识