我想在不实例化对象的情况下访问 Actionscript 中的公共静态常量...这可能吗?
我有一个静态属性,我想通过导入它的类来访问它,而不实例化该类的对象。这可能吗?
本质上,我正在一遍又一遍地编写一堆 styleSheet 对象。我想如果我有一个名为 CSS 的类,如下所示:
package com
{
import flash.text.*;
public class CSS
{
public static var STYLE_SHEET:StyleSheet = new StyleSheet();
并在构造函数中分配它的所有属性,例如:
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);
}
如果我将该类导入到该类中,我希望像这样使用它:
package{
import com.CSS;
在我的类中使用如下变量:
textField.styleSheet = CSS.STYLE_SHEET;
我可以避免一些头痛。 我觉得奇怪的是我创建了该类的一个实例来访问它的常量。所以在类头中我必须写:
private var css:CSS = new CSS();
即使我从未最终使用这个对象......
这有道理吗?我是否以错误的方式处理这个问题?
-J ps 谁希望 Tab 键在此问题编辑器窗口中起作用?
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 make 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以编写静态 init() 函数,您只需调用一次该函数,或者您可以使用“静态初始化器”:
现在导入类并仅使用 static var STYLE_SHEET
You can write static init() function, which you will have call only once or you can use "static initializers":
Now import class and just use static var STYLE_SHEET
您不必创建类的实例来访问其静态成员,但您可以在类构造函数内启动静态变量。
这意味着当您像这样访问静态变量时:
STYLE_SHEET 属性尚未设置!
通常解决这种问题的方法是使用“init”静态方法。
init 方法将设置 STYLE_SHEET,就像您在构造函数中所做的那样。
这确实意味着在您的应用程序中,您必须在其他任何地方使用 STYLE_SHEET 之前调用 CSS.init() 一次。
还:
您所说的“const”根本不是const。这只是一个静态。 Const 表示该值是恒定的(永不改变)。
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:
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.
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).
感谢 ivann 和 TandemAdam 带领我走上正确的道路。最终我发现,首先,我必须实例化类的实例的原因是因为我在构造函数中定义了变量属性。因此,我构建了一个静态构造函数,消除了实例化的需要,但我仍然必须调用该函数(而不仅仅是导入)。
然后,ivann 指出,如果我只是构建一个静态 init() 函数,我根本不需要调用该函数。很棒的想法,除了缺少一点信息。您不能在静态初始值设定项中声明变量。因此,最终的解决方案是调用我的私有静态函数,该函数可以从我的静态初始化函数中声明变量。
例子:
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: