Can I limit the methods another class can call in Java?

前端 未结 6 909
猫巷女王i
猫巷女王i 2020-12-11 15:01

Let\'s assume I have classes A, B, and C where class C has readable and writable properties:

public class         


        
6条回答
  •  一生所求
    2020-12-11 15:15

    Yes it is, but you have to go through some gyrations.

    public interface Incrementable {
        public void increment();
    }
    
    public interface Readable {
        public int getScore();
    }
    
    public class C implements Incrementable, Readable
    {
        ...
    }
    

    Now when you define the method in A that receives a reference to a B instance, define that method to take an Incrementable instead. For B, define it to take a Readable.

提交回复
热议问题