Java this 关键字基本使用

this 关键字基本使用

  • 什么是this

    • Java虚拟机会给每个对象分配this,代表当前对象.坦白的讲,要明白this不是件容易的事
    • 使用this解决前面变量命名问题
    public class This01 {
        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");
        }
    }
    
  • 深入理解 this

  • 使用hashCode()看看对象的情况

    public class This01 { 
    	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");
    	}
    }
    

  • this 的注意事项和使用细节

    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
      	}
      }	    
      
    3. 访问成员方法的语法: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();
          }
      }
      
      
    4. 访问构造器语法: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)构造器");
          }
      }
      
    5. this 不能在类定义的外部使用,只能在类定义的方法中使用。
  • this 的案例

    • 定义 Person 类,里面有 name、age 属性,并提供 compareTo 比较方法,用于判断是否和另一个人相等,提供测试类 TestPerson 用于测试, 名字和年龄完全一样,就返回 true, 否则返回 false
      public class TestPerson { 
      	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;
      	}
      }
      
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值