this的小问题 就是指Employee当前这个实例,也就是 上面调用者的staff[i] 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 this 有一个很好辨认的方法就是:他出现在哪个类(对象)中,你就把THIS换成那个类名(对象名)这样,是不是就很好理解了啊? 但是这个staff[i]是在上面那个EmployeeTest里定义的阿?我很菜唉~~ 原代码如下:import java.util.*;public class EmployeeTest{ public static void main(String[] args) { // fill the staff array with three Employee objects Employee[] staff = new Employee[3]; staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15); staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1); staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15); // raise everyone's salary by 5% for (int i = 0; i < staff.length; i++) staff[i].raiseSalary(5); // print out information about all Employee objects for (int i = 0; i < staff.length; i++) { Employee e = staff[i]; System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay()); } }}class Employee{ public Employee(String n, double s, int year, int month, int day) { name = n; salary = s; GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day); // GregorianCalendar uses 0 for January hireDay = calendar.getTime(); } public String getName() { return name; } public double getSalary() { return salary; } public Date getHireDay() { return (Date)hireDay.clone(); } public void raiseSalary(double byPercent) { double raise =this. salary * byPercent / 100; this.salary += raise; } private String name; private double salary; private Date hireDay;} 我说的但是你一个类总有一个实例的啊,class Employee { public void raiseSalary(double byPercent) { //因为每个类的成员变量都不相同的, //我调用abc.raiseSalary方法,那么在这个方法内部就需要访问abc的所有信息 //所以在这个raiseSalary方法中使用this,就等于abc了 this == abc double raise =this. salary * byPercent / 100; this.salary += raise;// }} private String name; private double salary; private Date hireDay;} 输出101~200的质数 (帮小弟讲解一下每句的意思)! 求救!!!作业题,各位大大帮忙 求教:使用Java操纵文件,替换特定字符串.................................... 怎么得到一个控件的控件名 请问如何使用菜单树? 有关classloader的问题? 菜鸟求教!!感激死我!! 请问调用windows的程序要含哪个包? 如何Add JTable 的Rows Cols? 请教一个程序 这段代码的几个Testthis2分别是干什么的 紧急求助,关于静态!在线等待
就是:他出现在哪个类(对象)中,你就把THIS换成那个类名(对象名)
这样,是不是就很好理解了啊?
我很菜唉~~
import java.util.*;public class EmployeeTest
{
public static void main(String[] args)
{
// fill the staff array with three Employee objects
Employee[] staff = new Employee[3]; staff[0] = new Employee("Carl Cracker", 75000,
1987, 12, 15);
staff[1] = new Employee("Harry Hacker", 50000,
1989, 10, 1);
staff[2] = new Employee("Tony Tester", 40000,
1990, 3, 15); // raise everyone's salary by 5%
for (int i = 0; i < staff.length; i++)
staff[i].raiseSalary(5); // print out information about all Employee objects
for (int i = 0; i < staff.length; i++)
{
Employee e = staff[i];
System.out.println("name=" + e.getName()
+ ",salary=" + e.getSalary()
+ ",hireDay=" + e.getHireDay());
}
}
}class Employee
{
public Employee(String n, double s,
int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar
= new GregorianCalendar(year, month - 1, day);
// GregorianCalendar uses 0 for January
hireDay = calendar.getTime();
} public String getName()
{
return name;
} public double getSalary()
{
return salary;
} public Date getHireDay()
{
return (Date)hireDay.clone();
} public void raiseSalary(double byPercent)
{
double raise =this. salary * byPercent / 100;
this.salary += raise;
} private String name;
private double salary;
private Date hireDay;
}
class Employee {
public void raiseSalary(double byPercent)
{
//因为每个类的成员变量都不相同的,
//我调用abc.raiseSalary方法,那么在这个方法内部就需要访问abc的所有信息
//所以在这个raiseSalary方法中使用this,就等于abc了 this == abc
double raise =this. salary * byPercent / 100;
this.salary += raise;//
}}
private String name;
private double salary;
private Date hireDay;
}