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;
}
这是因为静态方法不能对对象产生效果,和静态域一样,它属于类,不属于任何对象。
/*
* 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;
}
如对本文有疑问, 点击进行留言回复!!
[杭电多校2020]第一场 1004 Distinct Sub-palindromes
Swift -- 将本地生成的UIImage进行持久化保存(存到文件中fileManager.createFile)
网友评论