Actionscript 3:有人可以向我解释静态变量和方法的概念吗? [英] Actionscript 3: Can someone explain to me the concept of static variables and methods?

查看:17
本文介绍了Actionscript 3:有人可以向我解释静态变量和方法的概念吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 AS3,对于静态变量或方法的作用,或者它与没有此关键字的方法或变量有何不同,我有点困惑.我想这应该很容易回答.

I'm learning AS3, and am a bit confused as to what a static variable or method does, or how it differs from a method or variable without this keyword. This should be simple enough to answer, I think.

推荐答案

static 指定变量、常量或方法属于类而不是类的实例.static 变量、函数或常量无需创建类的实例即可访问,即 SomeClass.staticVar.它们不被任何子类继承,只有类(没有接口)可以有静态成员.
static 函数不能访问类的任何非静态成员(变量、常量或函数)并且您不能在静态函数中使用 thissuper .这是一个简单的例子.

static specifies that a variable, constant or method belongs to the class instead of the instances of the class. static variable, function or constant can be accessed without creating an instance of the class i.e SomeClass.staticVar. They are not inherited by any subclass and only classes (no interfaces) can have static members.
A static function can not access any non-static members (variables, constants or functions) of the class and you can not use this or super inside a static function. Here is a simple example.

public class SomeClass 
{
  private var s:String;
  public static constant i:Number;
  public static var j:Number = 10;

  public static function getJ():Number 
  {
    return SomeClass.j;
  }
  public static function getSomeString():String 
  {
    return "someString";
  }
}

在TestStatic中,无需创建SomeClass的实例即可访问静态变量和函数.

In the TestStatic, static variables and functions can be accessed without creating an instance of SomeClass.

public class TestStaic 
{
  public function TestStaic():void 
  {
    trace(SomeClass.j);  // prints 10
    trace(SomeClass.getSomeString());  //prints "someString"
    SomeClass.j++; 
    trace(SomeClass.j);  //prints 11
  }
}

这篇关于Actionscript 3:有人可以向我解释静态变量和方法的概念吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆