In this post we will see the different types of visibility that we can use for classes, variables and methods explain what modifiers apply to each and their differences as a first step we start naming which are present in JavaFX.
Modifiers:
Default Access
The package Access Modifier
The protected Access Modifier
- The public Access Modifier
- The public- Access Modifier read
- The public-init Access Modifier
- Default Access
- This switch is one who unknowingly use it in all the previous examples and we mentioned it when we said which is known as the modifier "script-only", this means that any class
- The Package Access Modifier Modifier by package allows us to define
- , bone we could have two. fx which are the same package so we could use things from each other provided we have defined the visibility package
- Example:
/ / Here we define package this script package tutorial;
/ / define the message var package visibility package var message = "Hello from script1.fx!"
/ / define the function also package package visibility function printMessage () { println ("{message} (in function printMessage)");}
script2.fx
/ / Here we define the package of this script package tutorial;
/ / acedo var message to the script1
println (script1.message)
/ / Call function
script1 script1.printMessage ();
In the example you can see from a script we can access variables or functions in another script if they are defined in the same package and whether those variables or functions with a defined package visibility. The
Protected Access Modifier With this switch, we can define visibility variables and functions between the same package and between subclasses.
Example:
one.fx
/ / Here we define the package of this script
package tutorial; public class one {protected var message = "Hello!"}
two. fx
style = "font-size: 100 %;">// import the import script
one tutorial.one;
/ / How to inherit from one and two message this / / defined as protected can do to your value class one extends two {
printMessage function () { println ("Class says two {message}");
}};
two var t = {};
t.printMessage ();
The
public Access Modifier By using this modifier are giving you the ability to access either from any class or package to classes, variables and functions that have been exposed and public.
Example:
One.fx
package tutorial;
public def someMessage = "This is a public script variable, in one.fx";
public class one {
     public var message = "Hello from class one!";
     public function printMessage() {
         println("{message} (in function printMessage)"); } } Two.fx
import tutorial.one;
println(one.someMessage); var o = one{}; println(o.message);
o.printMessage ();
The Public-Access Modifier read
Well the last two switches are the toughest in my opinion so pay attention and work. Public
read gives us the possibility to tell them that variables are only read from either side but it works for writing from the same package, but here is where the trap we could also define what will be read only and define public another scope for writing, we accomplish this in three ways.
/ / Here we are in the case where the variable / / is read only, except in the same public-read script var example1 = 'Test';
/ / Here we are in the case where the variable
/ / is read only, except for the script of the same package
public-read package var example2 = 'Test';
/ / Here we are in the case where the variable / / is read only, except for script / / the same package or subclasses protected public-read Example3 var = 'Test'; I think that seeing above three variants of us should be a clear idea of \u200b\u200bhow this switch but if there is any doubt, do not hesitate to discuss.
The public-init Access Modifier
This is very similar to the switch above with the difference that we use to define where we can start our variable. Example
Two.fx
import
tutorial.one;
var o = {
one message: "Initialized variable from A Different this package!"
} / / does not compile, as writing was not defined / / so the default is the same script
o.message = "Changing the message ...";
println (o.message)
Well with this we have taken a look at each one, in this part did not give a great theoretical explanation because I think the examples reflect well concepts and there is no much to say about these switches.
In the next post we will concentrate on operators of the language we see that are very similar to Java, but it should explain if only briefly and make a difference. Greetings
0 comments:
Post a Comment