必须用this关键字的三种情况: 1、我们想通过构造方法将外部传入的参数赋值给类的成员变量,构造方法的形式参数名称与类的成员变量名相同。例如: class Person { String name; public Person(String name) { this.name = name; } } 2、假设有一个容器类和一个部件类,在容器类的某个方法中要创建部件类的实例对象,而部件类的构造方法要接受一个代表其所在容器的参数。例如: class Container { Component comp; public void addComponent() { comp = new Component(this); } } class Component { Container myContainer; public Component(Container c) { myContainer = c; } } 3、构造方法是在产生对象时被java系统自动调用的,我们不能再程序中像调用其他方法一样去调用构造方法。但我们可以在一个构造方法里调用其他重载的构造方法,不是用构造方法名,而是用this(参数列表)的形式,根据其中的参数列表,选择相应的构造方法。例如: public class Person { String name; int age; public Person(String name) { this.name = name; } public Person(String name,int age) { this(name); this.age = age; } }
首先谢谢各位的热心帮助,下面是我写的代码,但编译错误,用的是JCreator_cn编译器,却提示错误,错误是 “无法从静态上下文中引用非静态 变量 this”请各位同学帮我分析一下,谢谢啦! public class B{ static int a=2; static void map(){ int a=3; B b=new B(); System.out.println("a="+this.a); } public static void main(String args[]){ map(); } }
你自己在类里面写的时候 给类的属性赋值的时候 this 是隐式的 所以你其实早在用了。,写出来是 给自己能看得懂。
最重要的一点就是对当前对象的理解:当前对象就是指当前正在调用类中方法的对象 public class ThisTest { public static void main(String[] args) { T t1 = new T(); T t2 = new T(); t1.print(); System.out.println(t1); t2.print(); System.out.println(t2); } } class T { public void print() { System.out.println(this); } } 其运行结果是 [email protected]@[email protected]@1fb8ee3 可以发现t1.print();由t1调用了这个T类中的print()方法,t1就是个当前对象(的引用);运行时打印this,也就是打印对象t1自身, 通过System.out.println(t1);打印的结果来看,这t1.print()结果相同; 对于t2,根据这个法则:”this一般出现在方面里面,当这个方法还没有调用的时候this指的是谁并不知道, 但是实际当中如果new一个对象出来之后,this指的就是当前这个对象,对哪个对象调用这个方法this指的就是谁. 如果再new一个对象,这个对象也有自己的this.this指的就是另外一个对象了”不难理解,这是的this指的t2了
1、我们想通过构造方法将外部传入的参数赋值给类的成员变量,构造方法的形式参数名称与类的成员变量名相同。例如:
class Person
{
String name;
public Person(String name)
{
this.name = name;
}
}
2、假设有一个容器类和一个部件类,在容器类的某个方法中要创建部件类的实例对象,而部件类的构造方法要接受一个代表其所在容器的参数。例如:
class Container
{
Component comp;
public void addComponent()
{
comp = new Component(this);
}
}
class Component
{
Container myContainer;
public Component(Container c)
{
myContainer = c;
}
}
3、构造方法是在产生对象时被java系统自动调用的,我们不能再程序中像调用其他方法一样去调用构造方法。但我们可以在一个构造方法里调用其他重载的构造方法,不是用构造方法名,而是用this(参数列表)的形式,根据其中的参数列表,选择相应的构造方法。例如:
public class Person
{
String name;
int age;
public Person(String name)
{
this.name = name;
}
public Person(String name,int age)
{
this(name);
this.age = age;
}
}
1.在类的构造方法中,通过this语句调用这个类得另一个构造方法
2.在一个人实例方法内,局部变量或参数和实例变量同名,实例变量被屏蔽,因此采用this方法
3.在实例方法中,访问当前实例的引用
“无法从静态上下文中引用非静态 变量 this”请各位同学帮我分析一下,谢谢啦!
public class B{
static int a=2;
static void map(){
int a=3;
B b=new B();
System.out.println("a="+this.a);
}
public static void main(String args[]){
map();
}
}
给自己能看得懂。
public class ThisTest {
public static void main(String[] args) {
T t1 = new T();
T t2 = new T();
t1.print();
System.out.println(t1);
t2.print();
System.out.println(t2);
}
} class T {
public void print() {
System.out.println(this);
}
} 其运行结果是 [email protected]@[email protected]@1fb8ee3
可以发现t1.print();由t1调用了这个T类中的print()方法,t1就是个当前对象(的引用);运行时打印this,也就是打印对象t1自身,
通过System.out.println(t1);打印的结果来看,这t1.print()结果相同; 对于t2,根据这个法则:”this一般出现在方面里面,当这个方法还没有调用的时候this指的是谁并不知道,
但是实际当中如果new一个对象出来之后,this指的就是当前这个对象,对哪个对象调用这个方法this指的就是谁.
如果再new一个对象,这个对象也有自己的this.this指的就是另外一个对象了”不难理解,这是的this指的t2了
如this.方法名
楼主想搞技术的话,要自己多看书