1

我正在浏览Haxe 简介,我想整理我的源文件夹。到目前为止,我只有一些继承示例可以放在一个文件夹中。

我的问题是我遇到了一个type not found不允许我运行代码的错误。我究竟做错了什么?

我有一个结构如下的文件夹:

  • 来源/继承

在这个文件夹中,我有:

  • 动物.hx
  • 狗.hx
  • 猫.hx

Cat.hx

package;

// package inheritance;

class Cat extends Animal{
    public function new(name:String, speed:Float, height:Float, weight:Float, legs:Int):Void {
        super(name, speed, height, weight, legs);   
    }

    public override function Speak():String {
        return ("Meow");
    }
}      

Animal.hx

package inheritance;

class Animal {
    private var name:String;
    private var speed:Float;
    private var height:Float;
    private var weight:Float;
    private var legs:Int;

    private function new(name:String, speed:Float, height:Float, weight:Float, legs:Int):Void {
        this.name = name;
        this.speed = speed;
        this.height = height;
        this.weight = weight;
        this.legs = legs;
    }

    private function toString():String {
        return ("\nName: " + name +  "\nSpeed: " + speed + " mph\nHeight: " + height + " inches\nWeight: " + weight + " lbs\nLegs: " + legs);
    }

    private function Speak():String {
        return ("Needs to fill out");
    }
}

Main.hx

package;
// package inheritance; //Tried both and neither work
// import inheritance; //Tried both and neither work

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.math.FlxMath;

class MenuState extends FlxState
{
    private var x:Int;
    private var myPoint:Point;
    private var myPoint3D:Point3;
    private var myDog:Dog;
    private var myCat:Cat;

    override public function create():Void
    {
        myPoint  = new Point(7,0);
        myPoint3D = new Point3(7,6,0);
        myDog = new Dog("Dog", 10, 12, 15, 4);
        myCat = new Cat("Cat", 12, 7, 6, 4);
        trace(myCat);
        trace(myCat.Speak());
        super.create();
    }

    override public function update(elapsed:Float):Void
    {
        super.update(elapsed);
    }

    private function changeMe(?x:Int) {
        this.x = x;
    }
}
4

1 回答 1

2

在查看了很多其他人的代码后,我发现了我的错误。事实证明,我需要在我的Main.hx.

Main.hx

package;
import inheritance.*; // This is what was missing
import inheritance.Dog; //Can be used if you only want one file
import inheritance.Cat; //Can be used if you only want one file

class MenuState extends FlxState
{
    private var x:Int;
    private var myPoint:Point;
    private var myPoint3D:Point3;
    private var myDog:Dog;
    private var myCat:Cat;
...
于 2016-09-27T23:23:57.047 回答