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.对本包可见——默认(很遗憾),不需要修饰符。

发布了5 篇原创文章 · 获赞 2 · 访问量 8996
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览