I've got a static property I would like to access by importing it's class without instantiating an object of that class. Is this possible?
Essentially I'm writing a bunch of styleSheet Objects over and over. I figure If I have a class called CSS and like this:
package com
{
import flash.text.*;
public class CSS
{
public static var STYLE_SHEET:StyleSheet = new StyleSheet();
and assign all of it's properties in the constructor like:
public function CSS
{
//NAV BUTTON
var navButtonBlock:Object = new Object();
navButtonBlock.color = "#000000";
navButtonBlock.fontSize = "25";
navButtonBlock.fontFamily = "Arial";
navButtonBlock.marginLeft = "5";
navButtonBlock.kerning = "true";
navButtonBlock.fontWeight = "bold";
//TITLE
var titleBlock:Object = new Object();
titleBlock.color = "#CCCCCC";
titleBlock.fontSize = "25";
titleBlock.fontFamily = "Arial";
titleBlock.marginLeft = "5";
titleBlock.kerning = "true";
titleBlock.fontWeight = "bold";
STYLE_SHEET.setStyle("code", titleBlock);
STYLE_SHEET.setStyle("navButton", navButtonBlock);
}
If I import the class into the class I wish to m开发者_如何学Pythonake use of it like:
package{
import com.CSS;
and inside my class use the variable like:
textField.styleSheet = CSS.STYLE_SHEET;
I can save some headaches. The thing I find weird is that I have create an instance of that class to access it's const. So in the class header I have to write:
private var css:CSS = new CSS();
even though I never end up making use of this object...
Does this make sense? Am I going about this the wrong way?-J p.s. Who wishes the tab key worked in this question editor window?
You can write static init() function, which you will have call only once or you can use "static initializers":
package com
{
import flash.text.*;
public class CSS
{
public static var STYLE_SHEET:StyleSheet = new StyleSheet();
// Static init block
// This block is run once when the class is first accessed
{
var navButtonBlock:Object = new Object();
/*...*/
var titleBlock:Object = new Object();
/*...*/
STYLE_SHEET.setStyle("code", titleBlock);
STYLE_SHEET.setStyle("navButton", navButtonBlock);
}
}
}
Now import class and just use static var STYLE_SHEET
import com.CSS;
...
textField.styleSheet = CSS.STYLE_SHEET;
You don't have to make an instance of the class to ACCESS its static members, but you are initiating your static variable inside the classes constructor.
This mean that at the time you access the static variable like this:
textField.styleSheet = CSS.STYLE_SHEET;
The STYLE_SHEET property is not set yet!
Usually the way to get around this kind of thing is to have an "init" static method.
CSS.init();
The init method would set STYLE_SHEET, like you have done in your constructor.
This does mean that in you application you will have to call CSS.init() once before you use STYLE_SHEET anywhere else.
ALSO: What you refer to as a "const", is not a const at all. It is just a static. Const means the value is constant (never changes).
Thanks to both ivann and TandemAdam for leading me down the right path. Ultimately I figured out that first, the reason I had to instantiate an instance of my class is because I was defining my variables properties in the constructor function. So I built a static constructor function which eliminated the need to instantiate, but I still had to call the function (rather than just import).
Then, ivann pointed out, I don't need to call the function at all If I just build a static init() function. Awesome idea except missing one bit of information. You cannot declare variables in a static initializer. So, the ultimate solution was to call my private static function that CAN declare variables from my static initilaizer function.
example:
package com.atJascha
{
import flash.text.*;
public class CSS
{
public static var STYLE_SHEET:StyleSheet = new StyleSheet();
{
init();
}
private static function init()
{
//NAV BUTTON
var navButtonBlock:Object = new Object();
navButtonBlock.color = "#FFFFFF";
//TITLE
var titleBlock:Object = new Object();
titleBlock.color = "#00FFFF";
STYLE_SHEET.setStyle("navButton", navButtonBlock);
STYLE_SHEET.setStyle("title", titleBlock);
}
}
}
精彩评论