this关键字

用法1:区分类的成员变量和方法的参数/局部变量

如果方法中的局部变量的名字与成员变量的名字相同,那么方法就隐藏了成员变量,如果想在该方法中使用被隐藏的成员变量,就必须使用关键字this(this.xxx)

 1 package com.demo03;
 2 
 3 public class Example {
 4     int number=10;
 5     
 6     public void showNumber() {
 7         System.out.println("show当前值:"+this.number);
 8     }
 9     
10     public void test() {
11         int number=1000;
12         System.out.println("test当前值:"+number);
13         
14     }
15     
16     public void setName(int number) {
17         
18         System.out.println("set当前值:"+number);
19     }
 1 package com.demo03;
 2 
 3 public class Application {
 4     public static void main(String[] args) {
 5         Example ex=new Example();
 6         ex.number=100;
 7         ex.showNumber();//100
 8         ex.test();//1000
 9         ex.setName(0);//0
10     }
11     
12 
13 }

2:调用本类中其他的重载的构造方法

this(xxx)

package com.demo04;

public class Car {
	String color;
	int price;
	
	public Car() {
		
	}
	
	public Car(String color) {
		this.color=color;
	}
	
	public Car(String color, int price) {
		this(color);
		this.price=price;
	}
	
	public void introduct() {
		System.out.println(color+"的车价格为"+price);
	}
	
}
package com.demo04;

public class Test {
	public static void main(String[] args) {
		Car c=new Car("红色",20000);
		c.introduct();//红色的车价格为20000
		
	}

}

 

综合练习1:用面向对象的思维来模拟LOL里的盖伦上阵杀敌。

package com.demo08;
 
public class Hero {
	String name;
	String skill_q;
	String skill_w;
	String skill_e;
	String skill_r;
	
	public Hero(String name) {
		this.name=name;
	}
	
	public Hero(String name,String skill_q,String skill_w,String skill_e,String skill_r) {
		this(name);
		this.skill_q=skill_q;
		this.skill_w=skill_w;
		this.skill_e=skill_e;
		this.skill_r=skill_r;	
		
	}
	
	public void fight() {
		System.out.println(this.name+"上阵杀敌");//这里this.有没有都可以
	}
	
	public static void main(String[] args) {
		Hero h=new Hero("盖伦","小宝剑","防御","电风扇","大宝剑");
		h.fight();//运行结果:盖伦上阵杀敌
	}
 
}

2:用面向对象的思维来完成植物大战僵尸。

package com.demo09;
 
public class ZhiWu {
	String name;
	int hp;
	int attack;
	
	public ZhiWu(String name,int hp,int attack) {
		this.name=name;
		this.hp=hp;
		this.attack=attack;
		
	}
	
	public void fight(JiangShi js) {
		System.out.println(this.name+"在干"+js.name); //这个函数里的this.有没有都可以,只要保证js.就行了
		hp=hp-js.attack;
		System.out.println(this.name+"血量还剩"+this.hp);
	}
 
}
package com.demo09;
 
public class JiangShi {
	String name;
	int hp;
	int attack;
	
	public JiangShi(String name,int hp,int attack) {
		this.name=name;
		this.hp=hp;
		this.attack=attack;
	}
	
	public void eat(ZhiWu zw) {
		System.out.println(this.name+"在吃"+zw.name);
		hp=hp-zw.attack;
		System.out.println(this.name+"血量还剩"+this.hp);
	}
 
}
package com.demo09;
 
public class Client {
	public static void main(String[] args) {
		ZhiWu zw=new ZhiWu("豌豆",1000,5);
		JiangShi js=new JiangShi("铁桶",800,20);
		zw.fight(js);
		js.eat(zw);
        /*
        运行结果为:
        豌豆在干铁桶
        豌豆血量还剩980
        铁桶在吃豌豆
        铁桶血量还剩795
        */       
		
	}
 
}

 

posted @ 2022-03-11 20:04  SherryYuan  阅读(49)  评论(0)    收藏  举报