How to expose a method in an interface without making it public to all classes

前端 未结 5 625
无人共我
无人共我 2021-01-19 18:01

I have a issue where I\'m working with a particular interface for quite a lot of things. However, I have a particular method that I want to be available only to a particular

相关标签:
5条回答
  • 2021-01-19 18:04

    Maybe use namespaces instead of (or in addition) to your interface. Just a thought.

    0 讨论(0)
  • 2021-01-19 18:04

    Today I've encountered same problem. Fortunately I had opportunity to make a superclass instead of interface. Instead of using this:

    internal interface IPhysicalObject extends IIDObject {
    
        function get shape():IShape;
    
    }
    

    I wrote this:

    public class PhysicalObject extends IDObject {
    
        public function PhysicalObject():void {
    
            ...
    
        }
    
        internal function get shape():IShape { ... }
    
    }
    

    Of course it's only possible if classes that implement IPhysicalObject interface do not extend any other class. I think that is another possible solution.

    0 讨论(0)
  • 2021-01-19 18:07

    One possible idea I just thought of would be to define a second interface for the internal method:

    interface IThing {
        function thisMethodIsPublic():void;
    }
    
    interface IInternalThing {
        function thisMethodShouldOnlyBeVisibleToCertainClasses():void;
    }
    

    That way the method would not be visible without type casting when working with IThing, but would be when you explicitly type cast as IInternalThing. This doesn't really solve the problem but it makes that method a little more hidden.

    0 讨论(0)
  • 2021-01-19 18:16

    Answer: Define two interfaces, and keep the 'private' functions in the second interface. If ActionScript supports inheritance for interfaces, then define the 'private' interface as extending the 'public' interface.

    0 讨论(0)
  • 2021-01-19 18:25

    Sorry to keep answering my own question but... Another possibility, kind of crazy, would be to create a sort of lock and key for the method I want to make private similar to the SingletonEnforcer often used in AS3.

    // IThing.as
    interface IThing {
        function thisMethodIsPublic():void;
        function thisMethodShouldOnlyBeVisibleToCertainClasses(key:InternalKey):void;
    }
    
    // InternalKey.as
    internal class InternalKey {}
    

    This seems like overkill to me but I tested it and it works! Classes external to the InternalKey can see the method but cannot call it. It also guarantees that the interface makes use of the method in question instead of having to typecast to the second interface.

    It is also possible to make the second interface be an internal interface instead of using the Internal key.

    internal interface IInternalThing extends IThing { 
        function thisMethodShouldOnlyBeVisibleToCertainClasses():void;
    }
    

    Actually, both of these are not exactly what I need because I wanted to make the method exposed to a nested package within the package where IThing would be, so basically I want to use custom namespaces here but I can't. I'm probably going to have to move the classes in the nested package back into the same pacakge as IThing.

    0 讨论(0)
提交回复
热议问题