Java 基础【01】 This 用法
用类名定义一个变量的时候,定义的只是一个引用,外面可以通过这个引用来访问这个类里面的属性和方法。
那们类里面是够也应该有一个引用来访问自己的属性和方法纳?
呵呵,JAVA提供了一个很好的东西,就是 this 对象,它可以在类里面来引用这个类的属性和方法。先来个简单的例子:
public class ThisDemo {
String name="Mick";
public void print(String name){
System.out.println("类中的属性 name="+this.name);
System.out.println("局部传参的`这里写代码片`属性="+name);
}
public static void main(String[] args) {
ThisDemo tt=new ThisDemo();
tt.print("Orson");
}
}
关于返回类自身的引用,《Thinking in Java》有个很经典的例子。
通过this 这个关键字返回自身这个对象然后在一条语句里面实现多次的操作,还是贴出来。
public class TestThis{
int number;
public TestThis(){
System.out.println("---this--");
}
public TestThis( int a){
this();
System.out.println("---this a--");
}
TestThis increment(){
number++;
return this;
}
private void print(){
System.out.println("number="+this.number);
}
public static void main(String[] args) {
TestThis tt=new TestThis();
tt.increment().print();
tt.increment().increment().increment().print();
}
}
运行结果:
如果不返回this,而是返回一個类的对象,看结果
public class TestThis{
int number;
public TestThis(){
System.out.println("---this--");
}
public TestThis( int a){
this();
System.out.println("---this a--");
}
TestThis increment(){
number++;
TestThis test = new TestThis();
return test;
}
private void print(){
System.out.println("number="+this.number);
}
public static void main(String[] args) {
TestThis tt=new TestThis();
tt.increment().print();
tt.increment().increment().increment().print();
}
运行结果:
顺便学习下this()方法:this()是指调用无参构造函数
public class TestThis{
int number;
public TestThis(){
System.out.println("---this--"+this.number);
}
public TestThis( int a){
this();
System.out.println("---this a--");
}
TestThis increment(){
number++;
TestThis test = new TestThis();
return test;
}
private void print(){
System.out.println("number="+this.number);
}
public static void main(String[] args) {
TestThis tt=new TestThis(3);
tt.increment().print();
tt.increment().increment().increment().print();
}
}
运行结果:
public class TestThis {
String name;
int age;
public TestThis (){
this.age=21;
}
public TestThis(String name,int age){
this();
this.name="Mick";
}
private void print(){
System.out.println("最终名字="+this.name);
System.out.println("最终的年龄="+this.age);
}
public static void main(String[] args) {
TestThis tt=new TestThis("",0); //随便传进去的参数
tt.print();
//System.exit(0);
}
}
调式结果:
调试输入报错只需要在main方法添加即可:System.exit(0);(参考:http://www.cnblogs.com/duffy/p/4612619.html)
看上面这段代码虽然很短,理解起来似乎也很顺理成章,在有参数的构造函数中赋值 name 在无参数的当中赋值age属性。
但我个人觉得其中有点问题,实例化一个类应该先为对象 ThisDemo 分配内存,先调用构造函数 ThisDemo(String name,int age)。
执行第一行时,调用 ThisDemo()构造函数,也就是说这里应该会有两个内存空间的,一个是为ThisDemo(String name,int age) 分配的内存空间和另一个是 ThisDemo() 执行空间。
为什么最后打印出来的结果是都实例化了一个对象中的两个属性name和age纳?请大牛赐教!
个人认为有效答复:先分配空间再调用构造函数,所以只分配一次内存
总结一下:
1) this 关键字是类内部当中对自己的一个引用,可以方便类中方法访问自己的属性;
2)可以返回对象的自己这个类的引用,同时还可以在一个构造函数当中调用另一个构造函数。
转载博客:http://www.cnblogs.com/java-class/archive/2012/12/19/2825463.html
=====================================
1.什么是super?什么是this?
super关键字表示超(父)类的意思。this变量代表对象本身。
2.使用super&this调用成员变量和方法
可以使用super访问父类被子类隐藏的变量或覆盖的方法。当前类如果是从超类继承而来的,当调用super.XX()就是调用基类版本的XX()方法。见示例1。
当类中有两个同名变量,一个属于类(类的成员变量),而另一个属于某个特定的方法(方法中的局部变量),使用this区分成员变量和局部变量
2.使用super&this应该注意些什么?
1)调用super()必须写在子类构造方法的第一行,否则编译不通过。每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有这种形式的构造函数,那么在编译的时候就会报错。
2)super()和this()类似,区别是,super从子类中调用父类的构造方法,this()在同一类内调用其它方法。
3)super()和this()均需放在构造方法内第一行。
4)尽管可以用this调用一个构造器,但却不能调用两个。
5)this和super不能同时出现在一个构造函数里面,因为this必然会调用其它的构造函数,其它的构造函数必然也会有super语句的存在,所以在同一个构造函数里面有相同的语句,就失去了语句的意义,编译器也不会通过。
6)this()和super()都指的是对象,所以,均不可以在static环境中使用。包括:static变量,static方法,static语句块。
7)从本质上讲,this是一个指向本对象的指针, 然而super是一个Java关键字。
转载博客:http://blog.sina.com.cn/s/blog_5e8392b10100nnb6.html