Java面向对象(二)封装

面向对象三大特性:封装、继承、多态。

一、封装(重点)

概念:尽可能隐藏对象的内部实现细节,控制对象的修改及访问的权限。

访问修饰符:private(只能在类中使用,不能在类外面使用)
被这些修饰的属性和方法被称为私有属性和私有方法

1.1为什么要封装

如果没有封装,那么类里面的信息可以随便更改,谁都可以改

public class BuyBooks {
    public static void main(String[] args) {
        Computer computer = new Computer();
        computer.price = 100;    
        computer.brand = "外星人";
        computer.buyComputer();     //外星人电脑只要100元????!!
    }
}

class Computer{
    public double price;
    public String brand;

    public void buyComputer(){
        System.out.println(brand + "笔记本电脑价格:" + price +  "元");
    }
}

结果:
外星人笔记本电脑价格:100.0元

很显然这个价格不合理,我们可以随意赋值价格多少,那么厂家已经破产了,因为public修饰符意味着公开,每个人都能获取访问和修改。

接下来进行封装(private)

public class BuyBooks {
    public static void main(String[] args) {
        Computer computer = new Computer();
        computer.setPrice(500);  //只能通过setPrice来赋值,且这次我赋值500元
        computer.brand = "外星人";
        computer.buyComputer();
    }
}

class Computer{
    private double price;
    public String brand;

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        if (price<10000) {
            this.price = 10000;
        }else{
            this.price = price;
        }
    }

    public void buyComputer(){
        System.out.println(brand + "笔记本电脑价格:" + price + "元");
    }
}

结果:
外星人笔记本电脑价格:10000.0元

- 属性
有private修饰的属性,只能在类里面使用,且一般要设置set和get来赋值和取值

如果上面例子中,computer.price = 500;那么报错;

- 方法
private修饰的方法,也是不能在外面使用,只能在类的内部使用

例如上面例子中,
如果 public void buyComputer() 改为 private void buyComputer()

那么实例化computer后 直接调用computer.buyComputer();报错

- 构造方法

如果一个类里面只有一个无参的构造函数,且无参构造也用了private修饰,那么着代表了该类是无法被实例化的

在这里插入图片描述显示,Book类里中具有私有访问权限,我们不能访问。

如果我们还有其他构造方法并不是用private修饰的,就可以通过编辑,可以实例化
在这里插入图片描述

二、this

this就是代表当前类本身

如果学过python,那么它和python类里面的self很像)

this ------- 代表当前类
this.属性 ------ 调用当前类对象的全局属性

this.方法() ------- 调用当前类对象的方法

this() ------- 调用当前类对象的无参构建方法

  • 使用this那么这里会牵扯到局部变量和全局变量
class Book{
    public int price;    //这个叫“全局price”

    public void setPrice(int price) {  	 //这个叫“局部price”
        price = price;    //注意这里
    }
}

在这里,我们定义的局部变量和全局变量的名字相同了,那么调用时候编辑器会调用谁的呢?

在setPrice方法里面,他会用的是自己的变量(也就是局部变量),也就是在方法里面,方法会先调用自己定义的变量

所以上面的 price = price 为 “局部price” = “局部price”, “全局price”并没有被赋值

所以用 this.price = price,代表“全局price” = “局部price”,那么就可以赋值成功了。

- 方法
很多时候直接调用方法就行了,不用this.方法(),但当多次实例一个类,多次调用同一个方法名时候,可以用this来区分开,增强可读性

class Person{
    void say(){
        this.hello();     //this.方法()
    }

    private void hello() {
        System.out.println("hello world");
    }
}
三、代码块(了解)

代码块是实例化时候,比构造方法先运行的

{
代码块
}

public class Demo03{
    public static void main(String[] args) {
        Cat cat = new Cat();
    }
}

class Cat {
    {
        System.out.println("你好啊");
    }

    public Cat(){
        System.out.println("我是猫");
    }
}
结果:
你好啊
我是猫

用的不多,但一般会出现在考试或面试题上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值