1、面向对象的世界里
类:就是图纸
属性:这一类事物拥有的共同属性
动作:这一类事物共同能执行的功能
对象:使用创建的具体某一个东西
对象能干什么? 完全取决于类是如何定义的
写代码
类要使用 class 来定义
属性:成员变量来描述,直接写在类中的变量
动作:成员方法,不写 static 就是成员方法
创建对象:
类 引用 = new 类()
package com.company;
public class Main {
// 成员变量
String color; // 颜色
int speed; // 速度
int seat = 2 ; // 座位
// 成员方法
public void run(){
System.out.println("跑车类型");
}
public void fly(){
System.out.println("车会飞");
}
public static void main(String[] args) {
int a =10 ; // 写在方法里的变量 局部变量
// 创建对象
// int a = 10 ;
//在面向对象的世界里 变量是没有市场的 这种变量被称为
// Java分为两种数据类型: 1 基本数据类型 2 引用数据类型 String 和我们所创建的所有的类引用
Main m = new Main(); // 创建对象 创建一辆车 , 后面想用这两车,就需要用这m 来访问
// 对象或者引用.方法()
m.run(); // . 表示调用. "的"
m.color = "红色玛莎";
m.speed = 300 ;
// m.pailiang = 1.5; 类中没有定义的内容不可以使用
System.out.println(m.color);
System.out.println(m.speed); // 座位默认 2 不需要再写
Main m2 = new Main();
m.run();
m.fly();
m2.color = "黄金玛莎" ;
m2.speed = 301 ;
System.out.println(m2.color);
System.out.println(m2.speed);
}
}
2 this关键字
this: 当前类的对象
this: 可以在方法内部获取到对象中的属性信息
this: 还可以区分局部变量和成员变量
package com.company;
public class Car {
String color;
int speed;
int seat = 5;
public void run(){
// 默认会有一个this:当前正在执行这个方法的对象
// 获取到车的颜色和速度
//System.out.println(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 里面找
}
public static void main(String[] args) {
/* Car c = new Car(); // 车里的属性就是类中定义好的成员变量
c.color = "红色";
c.speed = 120;
c.run(); // 再调用方法的时候,Java会自动的把对象传递给方法,在方法中由this来接收对象
// System.out.println(c);
// System.out.println(c.color);
Car c2 = new Car();
c2.color = "绿色";
c2.speed = 180;
c2.run();*/
// this 可以帮我们区分成员变量和局部变量
Car c = new Car();
c.color = "绿色";
c.fly("黑色");
}
}
3 构造方法
构造方法也是方法,也可以进行重载
作用:可以有更多的方法去创建对象
使用 this 可以访问其它的构造方法
this()不是 this.
package com.company;
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 bnagpai,String waihao) {
this(name,age,bnagpai); // this还可以调用当前类中其他的构造方法
this.waihao =waihao;
}
public static void main(String[] args) {
// 岳不群
Daxia dx1 = new Daxia("岳不群",18,"华山派");
// 林冲
Daxia dx2 = new Daxia("林冲",19,"梁山","豹子头");
}
}
面向对象练习
1 用面向对象的思维来模拟LOL英雄作战
package com.company;
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_e,String skill_q,String skill_r,String skill_w){
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+"在上阵杀敌人");
}
public static void main(String[] args) {
Hero h = new Hero("盖伦","小宝剑","防御技能","电风扇","大宝剑");
h.fight();
}
2 用面向对象的思维来模拟植物大战僵尸
package com.company;
public class Client {
public static void main(String[] args) {
zhiwu zw = new zhiwu("豌豆",100,5);
Jiangshi js = new Jiangshi("铁通僵尸",110,6);
zw.fight(js);
js.eat(zw);
}
}
package com.company;
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);
}
}
package com.company;
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);
}
}
总结:不一定所有代码都写在一个类里面