我正在做一些簡單的代碼。我有一個類插件,是爲了進行擴展,具有功能INITIALISE():錯誤#1034:類型強制失敗:無法將類轉換爲同一類?
package me.bleachisback.cocmod {
import me.bleachisback.cocmod.CoCMain;
public class Plugin {
private var _main:CoCMain;
private var _name:String;
private var _desc:String;
private var _ver:String;
private var _auth:String;
internal var _enabled:Boolean = false;
internal function initialise(main:CoCMain, name:String, description:String, version:String, author:String):void {
_main = main;
_name = name;
_desc = description;
_ver = version;
_auth = author;
}
}
}
我的主類加載插件,然後初始化它們:
package me.bleachisback.cocmod {
import me.bleachisback.cocmod.Plugin;
public class CoCMain extends MovieClip {
private function pluginLoadingComplete(e:Event):void {
//This class extends Plugin
var plugin:Object = new e.target.content.mainClass();
var desc:XML = new XML(e.target.content.description);
trace(this); //outputs: [object CoCMain]
plugin.initialise(this, desc.name[0], desc.description[0], desc.version[0], desc.author[0]);
}
}
}
但在最終,當我嘗試使用INITIALISE功能,它給了我這個錯誤:
TypeError: Error #1034: Type Coercion failed: cannot convert me.bleachisback.cocmod::[email protected] to me.bleachisback.cocmod.CoCMain.
at me.bleachisback.cocmod::CoCMain/pluginLoadingComplete()
難不成我不能在一個類轉換爲自身???
你試圖沿...'VAR插件線做一些事情:e.target.content.mainClass()=新e.target.content.mainClass ();'或設置一個類變量...'var newClass:Class = e.target.content.mainClass();'然後'var plugin:newClass = new newClass();' –
是的,那就是我然後它給了我這個錯誤:'1046:Type找不到或者不是編譯時常量:newClass'。我也嘗試過'var plugin:Plugin = new e.target.content.mainClass();'但是它只是給了我這個錯誤:'錯誤#1034:類型強制失敗:無法轉換plugin.bleachisback.testPlugin :: TestMain @ afc6309 to me.bleachisback.cocmod.Plugin'即使TestMain擴展插件。 – user2885503
在初始化函數中,嘗試初始化'_main',然後將它設置爲'main',即'_main = new CoCMain();'然後'_main = main'。錯誤是說你試圖將'CoCMain @ 4dfc041'這個'CoCMain對象'在內存位置'4dfc041'上轉換成'Class'。 –