javz笔记之:有趣的静态方法的使用
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了!正常的结果
/*
* 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了!因为静态方法达不成你要的效果
这是因为静态方法不能对对象产生效果,和静态域一样,它属于类,不属于任何对象。
/**
* 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;
}
上一篇: 基于vue如何发布一个npm包的方法步骤
推荐阅读
-
Vuejs学习笔记之使用指令v-model完成表单的数据双向绑定
-
blender怎么开启模拟数字键盘? blender笔记本数字键盘的使用方法
-
在Javascript中处理数组之toSource()方法的使用
-
在JavaScript中操作数组之map()方法的使用
-
在JavaScript中处理数组之reverse()方法的使用
-
在JavaScript中操作时间之getMonth()方法的使用
-
在JavaScript中处理时间之getHours()方法的使用
-
Java Web学习之MySQL在项目中的使用方法
-
Android开发之瀑布流控件的实现与使用方法示例
-
在JS中操作时间之getUTCMilliseconds()方法的使用