12-7java面向对象之this关键字的说明
2014-12-07 16:48
531 查看
在java中,this关键字能说清楚的人并不多,但是使用确实不少。本文是初学时对this的一些理解,便于大家参考。
结果没有任何问题,那么问题来了,在构造函数的书写过程中,public Info(String n, int a) 没有人能说明白n和a分别代表了什么,要么使用的参数名称和属性名称相似,要么就直接使用属性名称。
案例:使用属性名称:
直接用属性名称作为参数无法传递,导致不能实现。要修改就可以使用this关键字。分析得知,我们知道name=name中的第一个name代表的是类的属性名,所以可以用this.name代替。
案例:
进一步说明:只要是操作本类的属性时,就可以用this表示。
案例:同上类似,构造方法多。
本程序不论调用哪个方法,都会调用System.out.println("*******产生了一个对象"); 所以重复性太高,修改代码如下:
使用this一定要分清楚使用的是对象、属性、还是方法。
生成对象之后,实例化。第一行打印这个s是传递的参数s,第二个打印的是对象的属性,四三个打印的是参数传递后的s。
说明:
欢迎大家补充。
1.this关键字
this关键字可以指代那些呢? 可以表示操作类的属性、方法和当前对象。1.this代表属性
案例:定义一个类,表示年龄和姓名基本信息,实例化对象并输出结果//本程序是对this关键字的说明 class Info { //属性定义 public String name; public int age; //构造方法 public Info(String n, int a) { name = n; age = a; } //方法 public void print() { System.out.println("姓名: " + name + ", 年龄:" + age ); } } public class TestThis { public static void main(String[] args) { Info per = new Info("Tyrion", 24); per.print(); } }结果如下:
结果没有任何问题,那么问题来了,在构造函数的书写过程中,public Info(String n, int a) 没有人能说明白n和a分别代表了什么,要么使用的参数名称和属性名称相似,要么就直接使用属性名称。
案例:使用属性名称:
//本程序是对this关键字的说明 class Info { //属性定义 public String name; public int age; //构造方法 <span style="color:#ff0000;background-color: rgb(255, 255, 0);">public Info(String name, int age)</span> { name = name; age = age; } //方法 public void print() { System.out.println("姓名: " + name + ", 年龄:" + age ); } } public class TestThis { public static void main(String[] args) { Info per = new Info("Tyrion", 24); per.print(); } }结果:
直接用属性名称作为参数无法传递,导致不能实现。要修改就可以使用this关键字。分析得知,我们知道name=name中的第一个name代表的是类的属性名,所以可以用this.name代替。
案例:
//本程序是对this关键字的说明 class Info { //属性定义 public String name; public int age; //构造方法 public Info(String name, int age) { this.name = name; this.age = age; } //方法 public void print() { System.out.println("姓名: " + name + ", 年龄:" + age ); } } public class TestThis { public static void main(String[] args) { Info per = new Info("Tyrion", 24); per.print(); } }结果:
进一步说明:只要是操作本类的属性时,就可以用this表示。
2.this用于构造方法中
在构造方法中,可能会有多个,而且每种构造方法都可能相似,有相同的语句。如何才能避免重复?案例:同上类似,构造方法多。
//本程序是对this关键字的说明 class Info { //属性定义 public String name; public int age; //构造方法 public Info() { System.out.println("*******产生了一个对象"); } public Info(String name) { this.name = name; System.out.println("*******产生了一个对象"); } public Info(String name, int age) { this.name = name; this.age = age; System.out.println("*******产生了一个对象"); } //方法 public void print() { System.out.println("姓名: " + name + ", 年龄:" + age ); } } public class TestThis { public static void main(String[] args) { Info per = new Info("Tyrion", 24); per.print(); } }结果
本程序不论调用哪个方法,都会调用System.out.println("*******产生了一个对象"); 所以重复性太高,修改代码如下:
//本程序是对this关键字的说明 class Info { //属性定义 public String name; public int age; //构造方法 public Info() { System.out.println("*******产生了一个对象"); } public Info(String name) { this(); //调用本类中的无参构造 this.name = name; } public Info(String name, int age) { this(name); //调用本类中的一个参数的构造 this.age = age; } //方法 public void print() { System.out.println("姓名: " + name + ", 年龄:" + age ); } } public class TestThis { public static void main(String[] args) { Info per = new Info("Tyrion", 24); per.print(); } }注意:构造方法在类实例化时调用,在类调用普通方法前,this调用构造方法时必须放在首行,而且this至少一个构造方法不使用this。
3.this表示当前的对象
当前对象:正在操作类中方法的对象,此时this不固定,对着调用类中的方法不同而不同,之前this属性是当前的对象。//本程序是对this关键字的说明 class Info { public void print() { System.out.println(this); } } public class TestThis { public static void main(String[] args) { Info per1 = new Info(); //生成一个对象 Info per2 = new Info(); System.out.println(per1); per2.print(); //调用this打印 } }结果:
使用this一定要分清楚使用的是对象、属性、还是方法。
4.经典案例
//本程序是对this关键字的说明 class Info { String s = "hello world!!!"; //public String s; public Info(String s) { System.out.println(s); //打印的构造方法传递的参数s System.out.println(this.s); //打印的是per这个对象的s属性,由于s属性之前就初始化,所以是初始化的值 this.s=s; System.out.println(s); //打印的是传递过来的s } } public class TestThis { public static void main(String[] args) { Info per1 = new Info("nihao"); //生成一个对象 } }
生成对象之后,实例化。第一行打印这个s是传递的参数s,第二个打印的是对象的属性,四三个打印的是参数传递后的s。
//本程序是对this关键字的说明 class A { //构造类 public A() { new B(this).print(); // 调用B的方法,这里的this显然是类A的对象 } public void print() { System.out.println("HelloAA from A!"+ this); } } class B { A a; public B(A a) { this.a = a; } public void print() { a.print(); //调用A的方法 System.out.println("HelloAB from B!"+this+a); } } public class TestThis { public static void main(String[] args) { A aaa = new A(); aaa.print(); B bbb = new B(aaa); bbb.print(); } }结果:
说明:
欢迎大家补充。
相关文章推荐
- 12-10java面向对象之static关键字
- 12_Java面向对象_第4天(构造方法、this、super)
- 黑马程序员——Java基础语法 之面向对象(概念,特征,this,static关键字,代码块)
- java学习基础面向对象之this和static关键字 五-4
- java学习12-面向对象(this与super)
- Java 面向对象之构造函数和 this 关键字
- java学习之路 之 面向对象编程-面向对象-关键字(this、package、import)及java中主要包的介绍
- 黑马程序员--关于Java关键字this的用法说明
- java 第五天 面向对象(基础篇之this关键字)
- java技术之01 关键字this的三大用法
- Java中的关键字static、this、super、final详解
- JAVA基础--关键字 final/static/this/super
- Java关键字this、super使用总结
- java中的this,和super关键字
- Java关键字this、super使用总结
- JAVA this关键字用在构造方法中
- java基础3--面向对象(上)(static关键字与main方法)
- Java学习笔记---12.面向对象编程07-Java中的static关键字用法
- Java关键字this、super使用总结
- java this和super关键字