JAVA用于控制可见性的访问修饰符

1.仅对本类可见——private

超类:Employee

package com.ben.corejava.inherit;

import java.time.LocalDate;

public class Employee {

    private String name;
    private double salary;
    private LocalDate hireDate;

    public Employee(String name, double salary, int year, int month, int day) {
        this.name = name;
        this.salary = salary;
        this.hireDate = LocalDate.of(year, month, day);
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public LocalDate getHireDate() {
        return hireDate;
    }

    public void raiseSalary(double percent) {
        double raise = salary * percent / 100;
        this.salary = salary + raise;
    }
}

子类:Manager

package com.ben.corejava.inherit;

public class Manager extends Employee {

    private double bonus;

    public Manager(String name, double salary, int year, int month, int day) {
        super(name, salary, year, month, day);
        this.bonus = 0;
    }

    @Override
    public double getSalary() {
        double baseSalary = super.getSalary();
        return baseSalary + bonus;
    }

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

测试类:

package com.ben.corejava.inherit;

public class ManagerTest {
    public static void main(String[] args) {
        Manager boss = new Manager("Cal Cracker", 80000, 1987, 12, 15);
        boss.setBonus(5000);

        Employee[] staff = new Employee[3];

        staff[0] = boss;
        staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
        staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);

        for (Employee e : staff) {
            System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDate=" + e.getHireDate());
        }
    }
}

测试结果:

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

方法中不能访问hireDate域

输出结果:
name=Cal Cracker,salary=85000.0,hireDate=1987-12-15
name=Harry Hacker,salary=50000.0,hireDate=1989-10-01
name=Tommy Tester,salary=40000.0,hireDate=1990-03-15

2.对本包和所有子类可见——protected
超类:Employee

package com.ben.corejava.protect;

import java.time.LocalDate;

public class Employee {

    private String name;
    private double salary;
    protected LocalDate hireDate;

    public Employee(String name, double salary, int year, int month, int day) {
        this.name = name;
        this.salary = salary;
        this.hireDate = LocalDate.of(year, month, day);
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public LocalDate getHireDate() {
        return hireDate;
    }

    public void raiseSalary(double percent) {
        double raise = salary * percent / 100;
        this.salary = salary + raise;
    }
}

子类:Manager

package com.ben.corejava.protect;

import java.time.LocalDate;

public class Manager extends Employee {

    private double bonus;

    public Manager(String name, double salary, int year, int month, int day) {
        super(name, salary, year, month, day);
        this.bonus = 0;
    }

    @Override
    public double getSalary() {
        double baseSalary = super.getSalary();
        return baseSalary + bonus;
    }

    public void setBonus(double bonus) {
        this.hireDate = LocalDate.of(1989, 10, 1);
        this.bonus = bonus;
    }
}

测试结果:

public void setBonus(double bonus) {
    this.hireDate = LocalDate.of(1989, 10, 1);
    this.bonus = bonus;
}
将超类Employee中的hireDate声明为protected,而不是private,Manager中的方法就可以直接地访问它。
不过,Manager类中的方法只能访问Manager对象中的hireDate域,而不能访问其他Employee对象中的这个域。这种限制有助于避免滥用受保护机制,使得子类获得访问受保护域的权利。
name=Cracker,salary=85000.0,hireDate=1989-10-01
name=Harry,salary=50000.0,hireDate=1989-10-01
name=Tommy,salary=40000.0,hireDate=1990-03-15

3.对所有类可见——public

4.对本包可见——默认(很遗憾),不需要修饰符。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值