Oct 15, 2013

vJASS for mules #8: Visibility

Variables, methods and structs can have two kinds of visibility: Public and private
The meaning of public depends of the context. For methods, instance and static variables (remember?) it means that it can be accessible everywhere the struct is accessible. For structs, it means that it can be accessible everywhere in program. Example:

public struct Book
   public static integer sold 
   public integer isbn 
   public method sell takes unit owner returns nothing
      ...
   endmethod
endstruct

Book kingLear = Book.allocate() // allowed
set Book.sold = 10 // allowed
set kingLear.isbn = 20 // allowed
call kingLear.sell(...) //allowed

Important note: When you don't define a visibility, it is by default public.

Now private has the reverse effect. For methods, instance and static variables, it means that it can be accessible ONLY on its struct. For structs, it means that it can be accessible ONLY on its scope (yes, structs have a scope, too. We will take about it next lesson). Example:

scope structScope

   private struct Book
      private static integer sold
      private integer isbn 
      private method sell takes unit owner returns nothing
         set thistype.sold = 10 // allowed
         set this.isbn = 20 // allowed
      endmethod
  endstruct

   Book kingLear = Book.allocate() // allowed
   set Book.sold = 10 // NOT allowed
   set kingLear.isbn = 20 // NOT allowed
   call kingLear.sell(...) // NOT allowed

endscope

Book romeu = Book.allocate() // NOT allowed




Answer mentally:
  • What means the keyword public?
  • And private?
  • What happens when we don't define a visibility?


<<Previous>>                                                                                  <<Next>>

No comments :

Post a Comment