【问题标题】:Why is a static method giving me an error为什么静态方法给我一个错误
【发布时间】:2012-05-23 14:43:53
【问题描述】:

通过静态类型 Class 的引用调用可能未定义的方法测试。

这是我的课

package com.singleton.sample{
    public class SampleSingleton{

        public static function test( ):void{
            trace('hello world')
        }

    }
}

这是我的 mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init()">
    <mx:Script>
        <![CDATA[
            import com.singleton.sample.SampleSingleton;
            public function init():void{

                SampleSingleton.test() // error on this line
            }
        ]]>
    </mx:Script>
</mx:Application>

请忽略命名中的单例引用,因为我将我的课程剥离到此,但它仍然不起作用。

[编辑]

import com.singleton.sample.SampleSingleton;
public function init():void{
  SampleSingleton.test(); // this gives me the error
  com.singleton.sample.SampleSingleton.test(); // this works
}

【问题讨论】:

  • 能否在 SampleSingleton 类中添加非静态方法并创建实例并在 init 方法中调用这些方法?代码对我来说看起来不错,可能是项目/类路径等有问题。
  • 无法重现。我唯一能想到的是,SampleSingleton 在物理上不在正确的包中。
  • @RIAstar 我是这么认为的,但是当我输入导入语句时,IDE 会自动生成 com.singleton.sample.SampleSingleton。所以我不知道。这是一个新项目,所以设置不正确
  • @Baris Usakli 好点,所以我创建了一个实例并尝试访问我刚刚创建的另一个非静态函数,但它也找不到。哦,让我补充一下,该类位于项目外部的文件夹中。我在创建项目时将文件夹添加到项目中。
  • 自动生成的包是类的package声明中声明的包,不是物理路径。不过,如果这是问题所在,您应该会收到类似 A file found in a source-path must have the same package structure 'com.singleton.sample', as the definition's package 的错误消息。

标签: actionscript-3 apache-flex flex4 flex3 flex4.6


【解决方案1】:

应用程序文件被命名为 SampleSingleton 我敢打赌,所以你有一个名称冲突。重命名应用程序。

【讨论】:

  • 好电话我应该自己尝试一下。我知道这是一个愚蠢的错误。重命名 mxml 有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-10
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
相关资源
最近更新 更多