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;
}
推荐阅读
-
javz笔记之:有趣的静态方法的使用
-
Android开发之瀑布流控件的实现与使用方法示例
-
Java Web学习之MySQL在项目中的使用方法
-
PHP 面向对象程序设计(oop)学习笔记 (二) - 静态变量的属性和方法及延迟绑定
-
Android 自动判断是电话,网址,EMAIL方法之Linkify的使用
-
Android开发之SQLite的使用方法
-
HTML5 form标签之解放表单验证、增加文件上传、集成拖放的使用方法
-
python-tkinter之按钮的使用,开关方法
-
Python之使用adb shell命令启动应用的方法详解
-
Android笔记之:App自动化之使用Ant编译项目多渠道打包的使用详解