栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【JavaSE】this 关键字基本使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【JavaSE】this 关键字基本使用

文章目录
  • 1. 先看一段代码,并分析问题
  • 2. 深入理解 this
  • 3. 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

  1. this 关键字可以用来访问本类的属性、方法、构造器
  2. 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
	}
}	    

  1. 访问成员方法的语法: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();
    }
}

  1. 访问构造器语法:this(参数列表); 注意只能在构造器中使用(即只能在构造器中访问另外一个构造器, 必须放在第一条语句)
public class ThisDetail {
    public static void main(String[] args) {

        T t2 = new T();
    }
}

class T{
    
    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)构造器");
    }

}

  1. 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));
	}
}


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;
	}
}

  • 把名字或年龄改成其他不同数据
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/349150.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号