(今天,我们将开始面向对象的学习即总结。如有小伙伴发现错误问题,请大家即使指出来,以便使我即使更正,谢谢。)
面向对象 1.面向对象的概述以类的方式组织代码,以对象的组织(封装)数据
2.面向对象和面向过程例:把大象放进冰箱
面向过程:侧重的是过程,
优点:简单
缺点:代码量大了之后,维护性非常差
面向对象:侧重的是对象,我是上帝视角,,创建一个大象,告诉大象进冰箱。
优点:可扩展性非常强,维护成本低
缺点:新手上手难
-
类(抽象的):就是图纸
属性:这一类事物拥有的共同属性
动作:这一类事物共同能执行的功能 -
对象(具体的):使用创建的具体某一个东西
-
对象可以干什么?
完全取决与类是如何定义的 -
写代码
类要用class来定义
属性:成员变量来描述,直接写在类中的变量
动作:成员方法,不写static就是成员方法
创建对象:
类 引用 = new 类();
造车:
- 画图纸
定义车的属性信息:color,speed,seat
定义车的动作:跑 - 拿着图纸找工厂生产车
package com.oop;
//类与对象
//类:方法,属性
//对象:
// 类 引用 = new 类();
public class Car {//车的类
//成员变量--属性
String color;//颜色
int speed;//速度
int seat = 5;//座位
public void run() { //方法
System.out.println("车能跑");
}
public static void main(String[] args){
//在面向对象的世界里,变量是没有市场的,这种变量被称为引用
//java分为两种数据类型;1.基本数据类型2.引用数据类型 String和我们创建的所有的类
Car c = new Car();//创建对象
// 创建了一辆车,后面想用这俩车,就需要使用c来访问
// 让车去跑
//对象或者引用.方法()
c.run();//.表示调用“的”。
c.color = "绿色";
c.speed = 120;
//c.pailiang = 1.3;//类中没有定义的内容不可以使用
System.out.println(c.color);
Car c2 = new Car();//创建对象
c2.color = "红色";
c2.speed = 180;
System.out.println(c.seat);
System.out.println(c2.seat);
System.out.println(c.color);
System.out.println(c2.color);
}
}
4.this关键字
this:当前类的对象
this可以在方法类部获取到对象中的属性信息
this还可以区分局部变量和成员变量
- this.属性名 当局部变量与成员变量同名时,总是指向局部的变量(就近原则)。若要指向全局变量,就用this.属性名指向成员变量。 若不同名无冲突,则可以省略this。
- this.方法名 让类中一个方法,访问该类里的另一个方法或实例变量。
package com.oop;
//this关键字
public class This {
String color;//颜色
int speed;//速度
int seat = 5;//座位
public void Day04(){
String color = "白色";
//默认会有一个this:当前正在执行这个方法的对象
//获取到车的颜色和速度
System.out.println( 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) {
This c = new This();
c.color = "绿色";
c.speed = 120;
c.Day04();
This c2 = new This();
c2.color = "红色";
c2.speed = 180;
c2.Day04();
//this可以帮我们区分成员变量和局部变量
This D = new This();
D.color = "绿色";
D.fly("黑色");
}
}
5.构造方法
在创建对象的时候,自动调用的方法
语法:
public 类名(传参){
}
注意:
- 没有返回值这一项
- 在我们new的时候,自动调用构造方法
- 作用:在创建对象的时候,给对象设置属性信息
- Java会默认自动的送给每一个类一个无参数的构造方法,但是如果写了构造方法,系统就不会在赠送了。
package com.oop;
//构造方法
public class oopDay03 {
String color;
int speed;
int seat = 5;
//java会自动的赠送给每一个类一个无参的构造方法
//如果你自己定义了构造方法,Java就不会再赠送了
//在创建对象的时候,自动调用方法
public oopDay03(String color, int speed) {//构造方法
this.color = color;
this.speed = speed;
}
public void run() {
System.out.println(this.color + "颜色的车在跑");
}
public static void main(String[] args) {
oopDay03 c1 = new oopDay03("绿",120);//默认构造方法
//c1.color = "绿";
//c1.pseed = 120;
oopDay03 c2 = new oopDay03("红",180);
//c2.color = "红";
//c2.speed = 180;
c1.run();
c2.run();
}
}
6.构造方法的重载
- 构造方法也是方法,也可以进行重载
- 作用:可以有更多的方法去创建对象
使用this可以访问其他的构造方法
this()
package com.oop;
*/
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 waihao, String bangpai){
this(name,age,bangpai);
this.waihao = waihao;
}
public void run(){
System.out.println(name+age+bangpai+waihao);
}
public static void main(String[] args) {
oopDay04 dx1 = new oopDay04("岳不群",18,"华山派");
oopDay04 dx2 = new oopDay04("武松",19,"行者","梁山");
dx1.run();
dx2.run();
}
}
小练习:
1.用面向对象的思维来模拟LOL里的盖伦上阵杀敌。
思路:
1.定义英雄的名字,技能等属性。
2.写英雄杀敌的方法。
3.将英雄的属性,传入方法
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.String skill_q = String skill_q;
this.String skill_w = String skill_w;
this.String skill_e = String skill_e;
this.String skill_r = String skill_r;
}
public void figth(){
System.out.println(this.name+"上阵杀敌");
}
public static void main(String[] args){
Hero h = new Hero("盖伦","小宝剑","防御","旋转","大宝剑");
h.fight();
}
}
}
2.用面向对象的思维完成植物大战僵尸
思路:
1.植物的属性和打僵尸的方法
2.僵尸的属性和吃植物的方法
3.场景
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);
}
}
public class JiangShi{
String name;
int hp;
int attack;
public jiangshi(String name,int hp,int attack){
this.name = name;
this.attack = attack;
this. hp = hp;
}
//僵尸吃植物
public void eat(zhiwu zw){
System.out.println(this.name+"正在吃"+zw.name+"植物");
zw.hp -= this.attack;
System.out.println("植物血量剩余:"+zw.hp);
}
}
public class Client{//搭建场景用于植物大战僵尸
public static void main(String[] args){
//创建植物和僵尸
zhiwu zw = new zhiwu("豌豆",1000,20);
jiangshi js = new jiangshi("铁通僵尸",2000,20);
zw.figth(js);
js.eat(zw);
}
}
}



