2023-3-28——面向对象

成员变量与局部变量的区别

成员变量:位于方法外,有默认初始值,位于堆内存,随着对象的创建而存在,随着对象的消失而消失。

局部变量:位于方法中,没有默认初始值,使用前需要完成赋值,随着方法的调用而存在,随着方法的运行结束而消失。

当在方法中局部变量和成员变量重名时,使用this关键字调用成员变量,this代表的就是当前对象。

构造方法

作用:创建对象

格式:方法名与类名相同,没有返回值类型(void都没有),没有具体的返回值。

封装

通过将成员变量的用private修饰,只能在本类中使用,通过set和get方法,对成员变量赋值并取出。隐匿实现细节,仅对外暴露公共的访问方式。

案列:

Phone类

public class Phone {
    private String brand;
    private String color;
    private int price;

    public Phone(String brand, String color, int price) {
        this.brand = brand;
        this.color = color;
        this.price = price;
    }

    public Phone() {

    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public void call() {
        System.out.println("正在使用价格为" + this.price + "元" + this.color + "的" + this.brand + "打电话");

    }

    public void sendMessage() {
        System.out.println("正在使用价格为" + this.price + "元" + this.color + "的" + this.brand + "发短信");

    }
}

PhoneTest类

Manager类

public class Manager {
    private String name;
    private String id;
    private int salary;
    private int bonus;

    public Manager(String id, int salary, int bonus) {
        this.id = id;
        this.salary = salary;
        this.bonus = bonus;
    }

    public Manager() {
    }

    public Manager(String name, String id, int salary, int bonus) {
        this.name = name;
        this.id = id;
        this.salary = salary;
        this.bonus = bonus;
    }

    public void work() {
        System.out.println("工号为" + this.id + "基本工资为" + this.salary + "奖金为" + this.bonus + "的项目经理正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public int getBonus() {
        return bonus;
    }

    public void setBonus(int bonus) {
        this.bonus = bonus;
    }
}

ManagerTest类

Coder类

public class Coder {
    private String name;
    private String id;
    private int salary;

    public Coder() {
    }

    public Coder(String id, int salary) {
        this.id = id;
        this.salary = salary;
    }

    public Coder(String name, String id, int salary) {
        this.name = name;
        this.id = id;
        this.salary = salary;
    }

    public void work() {
        System.out.println("工号为" + this.id + "基本工资为" + this.salary + "的程序员正在努力的写着代码......");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }
}

CoderTest类

WaterDispenser类

public class WaterDispenser {
    private String brand;
    private String color;
    private int capacity;
    private String model;

    public WaterDispenser() {
    }

    public WaterDispenser(String brand, String color, int capacity, String model) {
        this.brand = brand;
        this.color = color;
        this.capacity = capacity;
        this.model = model;
    }

    public void show() {
        System.out.println("品牌:" + this.brand
                + "\n颜色:" + this.color
                + "\n容量:" + this.capacity + "L"
                + "\n模式:" + this.model);
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

WaterDispenserTest类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值