加入收藏 | 设为首页 | 会员中心 | 我要投稿 大连站长网 (https://www.0411zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Java this关键字的使用解析

发布时间:2021-11-07 15:57:11 所属栏目:教程 来源:互联网
导读:目录 1. 先看一段代码,并分析问题2. 深入理解 this3. this 的注意事项和使用细节4. this 的案例 1. 先看一段代码,并分析问题 在这里插入图片描述 public class This01 { //编写一个main方法 public static void main(String[] args) { Dog dog1 = new Dog(
目录
1. 先看一段代码,并分析问题2. 深入理解 this3. this 的注意事项和使用细节4. this 的案例
1. 先看一段代码,并分析问题
在这里插入图片描述
 
public class This01 {
 
    //编写一个main方法
    public static void main(String[] args) {
 
        Dog dog1 = new Dog("大壮", 3);
        //dog1调用了 info()方法
        dog1.info();
 
    }
}
 
class Dog{ //类
 
    String name;
    int age;
    // public Dog(String dName, int  dAge){//构造器
    // name = dName;
    // age = dAge;
    // }
    //如果我们构造器的形参,能够直接写成属性名,就更好了
    //但是出现了一个问题,根据变量的作用域原则
    //构造器的name 是局部变量,而不是属性
    //构造器的age  是局部变量,而不是属性
    //==> 引出this关键字来解决
    public Dog(String name, int  age){//构造器
        //this.name 就是当前对象的属性name
        this.name = name;
        //this.age 就是当前对象的属性age
        this.age = age;
    }
 
    public void info(){//成员方法,输出属性x信息
        System.out.println(name + "t" + age + "t");
    }
}
 
2. 深入理解 this
为了进一步理解 this,我们再看一个案例 (This01.java)
 
使用hashCode()看看对象的情况
 
public class This01 {
 
//编写一个main方法
public static void main(String[] args) {
 
Dog dog1 = new Dog("大壮", 3);
System.out.println("dog1的hashcode=" + dog1.hashCode());
//dog1调用了 info()方法
dog1.info();
 
System.out.println("============");
Dog dog2 = new Dog("大黄", 2);
System.out.println("dog2的hashcode=" + dog2.hashCode());
dog2.info();
}
}
 
class Dog{ //类
 
String name;
int age;
 
public Dog(String name, int  age){//构造器
//this.name 就是当前对象的属性name
this.name = name;
//this.age 就是当前对象的属性age
this.age = age;
System.out.println("this.hashCode=" + this.hashCode());
}
 
public void info(){//成员方法,输出属性x信息
System.out.println("this.hashCode=" + this.hashCode());
System.out.println(name + "t" + age + "t");
}
}
 
3. this 的注意事项和使用细节
ThisDetail.java
 
this 关键字可以用来访问本类的属性、方法、构造器 this 用于区分当前类的属性和局部变量
public class ThisDetail {
    public static void main(String[] args) {
        T t = new T();
        t.f3();
    }
}
 
class T{
 
String name = "兮动人";
    int num = 10;
    
//this关键字可以用来访问本类的属性
public void f3(){
      String name = "smith";
      //传统方式
      System.out.println("name=" + name + " num=" + num);//smith  100
      //也可以使用this访问属性
      System.out.println("name=" + this.name + " num=" + this.num);//jack 100
}
}     
 
访问成员方法的语法:this.方法名(参数列表);
 
public class ThisDetail {
    public static void main(String[] args) {
        T t1 = new T();
        t.f2();
    }
}
 
class T {
    public void f1(){
        System.out.println("f1()方法...");
    }
    public void f2(){
        System.out.println("f2()方法...");
        //调用本类的 f1
        //第一种方式
        f1();
        //第二种方式
        this.f1();
    }
}
 
访问构造器语法:this(参数列表); 注意只能在构造器中使用(即只能在构造器中访问另外一个构造器, 必须放在第一条语句)
 
public class ThisDetail {
    public static void main(String[] args) {
 
        T t2 = new T();
    }
}
 
class T{
    /*
细节: 访问构造器语法:this(参数列表);
注意只能在构造器中使用(即只能在构造器中访问另外一个构造器)
注意: 访问构造器语法:this(参数列表); 必须放置第一条语句
 */
    public T(){
        //这里去访问 T(String name,int age)构造器,必须放在第一行
        this("Jack", 23);
        System.out.println("T()构造器");
 
    }
 
    public T(String name,int age){
        System.out.println("T(String name,int age)构造器");
    }
 
}
 
this 不能在类定义的外部使用,只能在类定义的方法中使用。
 
4. this 的案例
TestPerson.java
 
定义 Person 类,里面有 name、age 属性,并提供 compareTo 比较方法,用于判断是否和另一个人相等,提供测试类 TestPerson 用于测试, 名字和年龄完全一样,就返回 true, 否则返回 false
 
public class TestPerson {
 
//编写一个main方法
public static void main(String[] args) {
 
Person p1 = new Person("mary", 20);
Person p2 = new Person("mary", 20);
 
System.out.println("p1和p2比较的结果=" + p1.compareTo(p2));
}
}
 
/*
定义Person类,里面有name、age属性,并提供compareTo比较方法,
用于判断是否和另一个人相等,提供测试类TestPerson用于测试,
名字和年龄完全一样,就返回true, 否则返回false
 
 */
class Person {
String name;
int age;
//构造器
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//compareTo比较方法
public boolean compareTo(Person p) {
//名字和年龄完全一样
// if(this.name.equals(p.name) && this.age == p.age) {
// return true;
// } else {
// return false;
// }
return this.name.equals(p.name) && this.age == p.age;
}
}
Java this关键字的使用解析

(编辑:大连站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!