1、this 可以在实例方法中,不能使用在静态方法中。
2、this 关键字大部分情况下可以省略,那什么时候不能省略呢?
思考一下以下代码
public class ThisTest{
public class void main(String[] args) {
Student s = new Student();
s.setNum(111);
s.setName("张三");
System.out.println("学号" + s.getNum());
System.out.println("姓名" + s.getName( ) );
Student s = new Student(2222, "李四");
System.out.println("学号" + s2.getNum());
System.out.println("姓名" + s2.getName( ) );
}
}
// 分析一下:一下代码哪里写的不好
// 学生类
class Student{
// 学生号
private int num;
// 姓名
private String name;
// 无参构造方法
public Student() {
}
// 有参构造方法
public Student( int i, String s ) {
num = i;
name = s;
}
// setter and getter 方法
public void setNum(int i) {
num = i;
}
public ge