java参数传值代码举例
作者:励志当野王
在编程中往方法中传递参数的方法往往有两种,一种是值传递,一种是引用传递,而在java中所有的参数传递全部都是值传递,这篇文章主要给大家介绍了关于java参数传值的相关资料,需要的朋友可以参考下
基本数据类型参数的传值
package Absent;
public class chapter1 {
public static void main(String[] args) {
Computer com=new Computer();
int b=100;
int a=12;
int result=com.add(b, a);
System.out.println(result);
result=com.add(b*12+2,a*12+3);
System.out.println(result);
}
}
class Computer{
int add(int x,int y) {
return x+y;
}
}引用类型参数的传值
package Absent;
public class Chapter2 {
public static void main(String[] args) {
Abolish abolish=new Abolish(100);
System.out.println("南孚电池的储量是:"+abolish.electricityAmount);
Absolute absolute=new Absolute();
System.out.println("收音机开始使用南孚电池");
absolute.openRadio(abolish);
System.out.println("目前南孚电池的储存量为:"+abolish.electricityAmount);
}
}
class Abolish{
int electricityAmount;
Abolish(int amount){
electricityAmount=amount;
}//构造方法
}
class Absolute{
void openRadio(Abolish abolish) {
abolish.electricityAmount=abolish.electricityAmount-10;
}//消耗的电量
}总结
到此这篇关于java参数传值的文章就介绍到这了,更多相关java参数传值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
