类和对象及this关键字
this:当前类的对象
this可以在方法内部获取对象的属性信息
this可以区分局部变量和成员变量
public class Car {
//默认会有一个this:当前执行这个方法的对象
//获取到车的颜色和速度
//成员变量【直接写在class中】
//属性
String color;
int speed;
int seat = 5;//此处定义后面的车的座位都是5.
//方法
public void run() {
// System.out.println(this);
//this是随动的,当前定义是谁就是谁
System.out.println(this.color);
System.out.println(this.speed);
System.out.println("车能跑");
}
public void fly(String color){
System.out.println(this.color+"颜色的车会飞在"+color+"颜色的云朵中");//此时访问的也是成员变量。
//前面加了this的表示对象中的颜色;后面没加this的表示默认先找自己方法内
//变量的查找顺序:先找自己方法内,如果没有就去this里面找。
}
public static void main(String[] args) {
//int a = 10;//写在方法里的变量。【局部变量】
//创建对象:类 引用 = new 类();
// 【在面向对象的世界里,变量是没有市场的。这种变量被称为引用。】
//Java分为两种数据类型:1.基本数据类型 2.引用数据类型 String和我们创建的所有的类
Car c = new Car();//创建对象:创建了一辆车。后面想要使用这辆车,就需要使用c来访问。
//车中的属性取决于类中定义好的变量
c.color = "黑色";
c.speed = 180;
//让车跑
//对象或者引用.方法()
c.run();//在调用方法时,Java会自动的把对象传递给方法,在方法中由this来接收对象
// System.out.println(c);
//c.sise = 95;//类中没有定义的内容不能随意使用。想使用就去前面定义
// System.out.println(c.color);
Car c2 = new Car();
c2.color = "蓝色";
c2.speed = 100;
c2.run();
//this可以帮我们区分成员变量和局部变量
Car c3 = new Car();
c3.color= "绿色";
c3.fly("白色");//这是就是白色的车会飞
// System.out.println("车1的座位数:" + c.seat);
// System.out.println("车2的座位数:" + c2.seat);
//
// System.out.println("车1的color:" + c.color);
// System.out.println("车2的color:" + c2.color);
}
}
构造方法
语法:
public 类名(传参){
}
注意:
1.没有返回值这一项
2.在我们new的时候,自动调用构造方法
作用:在创建对象的时候,给对象设置属性信息
Java会自动的赠送每一个类一个无参数的构造方法
注:如果自己定义了构造方法,Java就不会再赠送了【定义任何一个之后,剩下的就不会再有赠送了】
public class Car2 {
String color;
int speed;
int seat = 5;
//Java会自动的赠送每一个类一个无参数的构造方法
//注:如果自己定义了构造方法,Java就不会再赠送了【定义任何一个之后,剩下的就不会再有赠送了】
//在创建对象的时候,自动调用方法
public Car2(String color,int speed) {
// System.out.println("你好,我是构造方法");
//设置属性信息
this.color=color;//添加这两排之后就可以将下面定义color和speed给注释掉了
this.speed=speed;
}
public void run() {
System.out.println(this.color + "的车在跑");
}
public static void main(String[] args) {
Car2 c1 = new Car2("绿色",120);//默认调用的是构造方法
// c1.color = "绿色";
// c1.speed = 120;
Car2 c2 = new Car2("红色",180);
// c2.color = "红色";
// c2.speed = 180;
c1.run();
c2.run();
}
}
构造方法重载
构造方法也是方法,也可以重载
可以让我们有更多的方式去创建对象
使用this可以访问当前类中的其他构造方法
语法:this()
public class DaXia {
String name;
String waihao;
int age;
String bangpai;
//构造方法也是方法,也可以重载
//可以让我们有更多的方式去创建对象
//这个不存在外号
public DaXia(String name, int age, String bangpai) {
this.name=name;
this.age=age;
this.bangpai=bangpai;
}
//这个存在外号
public DaXia(String name, int age, String bangpai,String waihao) {
//使用this可以访问当前类中的其他构造方法
this(name,age,bangpai);
// this.name=name;
// this.age=age;
// this.bangpai=bangpai;
this.waihao=waihao;
}
//此处是在设置方法
public void person() {
System.out.println("我是"+this.name+"我属于"+this.bangpai+"我今年"+this.age+"也可以叫我"+this.waihao);
}
public static void main(String[] args) {
//岳不群
DaXia dx = new DaXia("岳不群", 18, "华山派 ");
//鲁正婷
DaXia dx2 = new DaXia("鲁正婷",18, "华山派 ","卤猪蹄");
//调用person类
dx.person();//我是岳不群我属于华山派 我今年18也可以叫我null[因为没有外号,此处为空]
dx2.person();//我是鲁正婷我属于华山派 我今年18也可以叫我卤猪蹄
}
}
以上内容的相关小练习:
注:不是所有代码都写在一个类中
1.用面向对象的思维来模拟冰火人
public class Hero {
//火娃的操作
String name;
String up;
String down;
String left;
String right;
public Hero(String name){
this.name=name;
}
public Hero(String name,String up,String down,String left,String right) {
this.name=name;
this.up=up;
this.down=down;
this.left=left;
this.right=right;
}
public void run() {
System.out.println(this.name+"执行了操作"+up+"跳了一下,"
+"然后"+left+"吃掉一颗星星,"+down+"掉到传送门,"+right+"进入传送门来到下一关。");
}
public static void main(String[] args) {
Hero huo=new Hero("火娃","向上","向下","向左","向右");
//执行
huo.run();
}
}
运行结果:
2.用面向对象的思维来模拟植物大战僵尸
需要三部分 1.植物
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);
//僵尸血量减少
js.hp -=this.attack;
System.out.println("僵尸的血量剩余"+js.hp);
}
}
2.僵尸
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+"植物");
//僵尸吃植物
zw.hp -= this.attack;
System.out.println("植物的血量还剩余"+zw.hp);
}
}
3.场景
public class Client {
public static void main(String[] args) {
//创建植物和僵尸
ZhiWu zw=new ZhiWu("豌豆射手",1000,50);
JiangShi js=new JiangShi("铁桶僵尸",800,40);
//植物打僵尸
zw.fight(js);
//僵尸吃植物
js.eat(zw);
}
}
特别提醒: 真正创建对象的是场景部分
运行结果展示:
okk,good night!!!