this关键字and构造器

一.this关键字

1、this在类中的作用

【区别成员变量和局部变量】

public class Student{
private String name;
public void setName(String name){
//this.name表示类中的属性name
this.name = name;
}
}

【调用类中的其他方法】

public class Student{
private String name;
public void setName(String name){
this.name = name;
}
public void print(){
//表示调用当前类中的setName方法
this.setName("tom");
}
}

注:默认情况下,setName("tom")和this.setName("tom")的效果是一样的.

【调用类中的其他构造器】

public class Student{
private String name;
public Student(){
//调用一个参数的构造器,并且参数的类型是String
this("tom");
}
public Student(String name){
this.name = name;
}
}

注:this的这种用法,只能在构造器中使用.普通的方法是不能用的.并且这局调用的代码只能出现在构造器 中的第一句.

【示例】

public class Student{
private String name;
//编译报错,因为this("tom")不是构造器中的第一句代码.
public Student(){
System.out.println("hello");
this("tom");
}
public Student(String name){
this.name = name;
}
}

2、this关键字在类中的意义

this在类中表示当前类将来创建出的对象.

【例子】

public class Student{
private String name;
public Student(){
System.out.println("this = "+this);
}
public static void main(String[] args){
Student s = new Student();
System.out.println("s = "+s);
}
}

运行后看结果可知,this和s打印的结果是一样的,那么其实也就是变量s是从对象的外部执行对象,而this是 在对象的内部执行对象本身.

这样也就能理解为什么this.name代表的是成员变量,this.setName("tom")代表的是调用成员方法,因为 这俩句代码从本质上讲,和在对象外部使用变量s来调用是一样的,s.name和s.setName("tom")。

【this和s打印出来的内存地址是一样的,使用==比较的结果为true。】

public class Student{
public Student getStudent(){
return this;
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = s1.getStudent();
System.out.println(s1 == s2);//true
}
}

【 类中的this是和s1相等还是和s2相等呢?】

public class Student{
private String name;
public void test(){
System.out.println(this);
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
s1.test();
s2.test();
}
}

注:这句话是要这么来描述的,s1对象中的this和s1相等,s2对象中的this和s2相等,因为类是模板,模板中写 的this并不是只有一个,每个对象中都有一个属于自己的this,就是每个对象中都一个属于自己的name属性 一样.

二.构造器

类中的构造器也称为构造方法,是在进行创建对象的时候必须要调用的。并且构造器有以下俩个特点:

  1. 1. 必须和类的名字相同
  2. 2. 必须没有返回类型,也不能写void

1.构造器的作用:

  • 1. 使用new创建对象的时候必须使用类的构造器
  • 2. 构造器中的代码执行后,可以给对象中的属性初始化赋值

【演示】

public class Student{
private String name;
public Student(){
name = "tom";
}
}

2.构造器重载

除了无参构造器之外,很多时候我们还会使用有参构造器,在创建对象时候可以给属性赋值.

【例子】

public class Student{
private String name;
public Student(){
name = "tom";
}
public Student(String name){
this.name = name;
}
}

3.构造器之间的调用

使用this关键字,在一个构造器中可以调用另一个构造器的代码。

注意:this的这种用法不会产生新的对象,只是调用了构造器中的代码而已.一般情况下只有使用new关键 字才会创建新对象。

【演示】

public class Student{
private String name;
public Student(){
this();
}
public Student(String name){
this.name = name;
}
}

4.默认构造器

在java中,即使我们在编写类的时候没有写构造器,那么在编译之后也会自动的添加一个无参构造器,这个 无参构造器也被称为默认的构造器。

【示例】

public class Student{
}
main:
//编译通过,因为有无参构造器
Student s = new Student();

但是,如果我们手动的编写了一个构造器,那么编译后就不会添加任何构造器了

【示例】

public class Student{
private String name;
public Student(String name){
this.name = name;
}
}
main:
//编译报错,因为没有无参构造器
Student s = new Student();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值