如何限制子类访问超级类方法? [英] How to restrict Child Class from accessing super Class methods?

查看:81
本文介绍了如何限制子类访问超级类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

我上课

package test.inheritance.checkMultiple;

public class Company {



protected void run (){
    System.out.println("Time to Run");
}

}

public class Department extends Company {



    public void testDepartment()
    {
        Department d =new Department();
        d.run();
    }

}


public class Employee extends Department{


    public void checkValidEmployee(){
        Employee e =new Employee();
        e.run();
        e.testDepartment();

    }

    public static void main (String[] artgs)
    {
        Employee e =new Employee();
        e.checkValidEmployee();
        System.out.println("Valid Emplyee");
    }

}

1)我不希望员工类有权访问run方法.我该怎么办?

1) I don't want employee class to have access to run method. What should I do for this?

2)如果我想授予员工班级运行方法的访问权限,而不是部门级的访问权限,该怎么办?

2) If I wanted to give employee class the access to run method, but not department class, what should I do?

推荐答案

您不应该这样做,因为每个子类都必须是一个有效的子类,请参见

You shouldn't, as every childclass has to be a valid class of which it is a child - see Liskovs substitution principle.

因此,按照@Michael Laffargue的建议,使用合成代替继承.

So use composition instead of inheritance as suggested by @Michael Laffargue.

我不知道您到底想要实现什么,但是它看起来应该更像以下内容,其中Company是Department的成员变量,而Department又是Employee的成员变量.

I don't know what exactly you want to achieve, but it should look somewhat more like the following, with Company being a member variable of Department which in turn is a member variable of an Employee.

import java.util.Arrays;
import java.util.List;

public class Company
{
    private final String name;

    public Company(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return this.name;
    }

    public void run()
    {
        System.out.println("Time to Run");
    }
}

public class Department
{
    private static final List<String> VALID_DEPARTMENTS = Arrays.asList("IT");
    private final String name;
    private final Company company;

    public Department(String name, Company company)
    {
        this.name = name;
        this.company = company;
    }

    public String getName()
    {
        return this.name;
    }

    public Company getCompany()
    {
        return this.company;
    }

    public void checkValid()
    {
        if (!VALID_DEPARTMENTS.contains(this.name))
            throw new AssertionError();
    }

}

public class Employee
{
    private final Department department;
    private final String name;

    public Employee(String name, Department department)
    {
        this.department = department;
        this.name = name;
    }

    public void checkValid()
    {
        this.department.getCompany().run();
        this.department.checkValid();
    }

    public Department getDepartment()
    {
        return this.department;
    }

    public String getName()
    {
        return this.name;
    }
}

public class Main
{
    public static void main(String[] artgs)
    {
        final Company company = new Company("Company");
        final Department department = new Department("IT", company);
        Employee e = new Employee("Peter", department);
        e.checkValid();
        System.out.println("Valid Emplyee");
    }
}                                                                                                                                                                                                                                                                                                                                                                                   

这篇关于如何限制子类访问超级类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆