代码中完全体现:中心思想——->this代表当前对象
package com.Example.chapter04;
/**
* @author Administrator
* @date : 2017年2月9日 下午11:17:49
* @function : this的三大特效
* 1.成员属性
* 2.构造方法
* 3.当前对象
*/
public class ThisDesign01 {
public static void main(String[] args) {
PersonAgain personagain=new PersonAgain(23, "光唯");
personagain.getInfo();
}
}
class PersonAgain{
private int age;
private String name;
public PersonAgain(){
System.out.println("此处Person类无参构造函数发言");
}
public PersonAgain(int age,String name){
this();//第二种 构造方法的调用
this.setAge(age);
this.setName(name);
System.out.println("此处Person类有参构造函数发言"+"\t姓名"+this.getName()+"\t年龄"+this.getAge());
}
public void getInfo(){
System.out.println("试试第三种this的用法"+this);
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;//第一种 成员变量的用法
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;//第一种 成员变量的用法
}
}
本文详细介绍了Java中this关键字的应用场景,包括使用this引用当前对象的成员变量、调用当前类的其它构造方法以及引用当前对象实例。通过具体的代码示例展示了this关键字在实际编程中的作用。
353

被折叠的 条评论
为什么被折叠?



