Java - 语句/转换|纸浆芯

发布于 2024-11-28 16:33:21 字数 2702 浏览 1 评论 0原文

[请注意,这可能需要 AS3 + Java 知识]

背景信息:

我正在尝试使用 Java + Pulpcore 构建游戏,但我对这个场景相当陌生。我正在构建的游戏可能比我想象的更加性能密集,我知道 Java 可以解决我的问题,但是我在处理严格类型等方面有几个问题。

这是我在 AS3 中的代码:

Main.as3

import org.tbmb.champions.Container;
import org.tbmb.zombies.ZContainer;

public class Main extends MovieClip {

    // ******* temporary properties ******* /
    private var blunder:Container = null;
    // *******                      ******* /

    public function Main() {

        init(); // initialize objects

    }

    public function init():void {

        blunder = new Container(Blunder as Class);

    } // end of class

}

Container.as3

 package org.tbmb.champions {

     import flash.display.MovieClip;

     public class Container extends MovieClip {

          public function Container(champ:*) {

          } // end of constructor

     } // end of class

 } // end of package

Blunder.as3

package org.tbmb.champions.blunder {

    import flash.display.MovieClip;

    public class Blunder extends MovieClip {

        public function Blunder() {

        } // end of constructor

    } // end of class

} // end of constructor

1.) 我将如何用Java重写?

blunder = new Container(Blunder as Class);

2.) 我如何能够接受 Container 类中上述行的任何 Java 类?

public function Container(champ:*) {

我需要这样做,因为我将不同的冠军类别(取决于用户选择的类别)发送到包含所有其他类别(健康等)的包含类别。我需要我的 Container 类接受任何类,而不仅仅是一个类;我会使用什么类型?

这是到目前为止我在 Java 中所拥有的内容:

ZomboPulp.java(主类)

import pulpcore.scene.Scene2D;

import org.tnpfk.champions.Container;
import org.tnpfk.champions.blunder.Blunder;

import pulpcore.sprite.FilledSprite;
import pulpcore.image.Colors;

public class ZomboPulp extends Scene2D {

    FilledSprite background = new FilledSprite(Colors.WHITE);

    Container container = null; // Container that contain's blunder, 
    // and all his objects (health, mana, etc)

    public void load() {

        this.initScreen(); // initialize main screen.
        this.initObjects(); // initialize our objects.

    } // end of load();

    public void initScreen() {

        add(background);

    } // end of initScreen();

    public void initObjects() {

        container = new Container(Blunder as Class); // ERROR HERE

    } // end of initObjects();

}

Container.java

package org.tnpfk.champions;

public class Container {

    public Container(Object champ) {

    } // end of constructor

} // end of class

很抱歉这篇冗长的文章,感谢您的帮助。顺便说一句,我确实检查了 StackOverflow;和谷歌,但我无法找到任何有关此的信息。

谢谢, 纸浆

[please note this may require AS3 + Java knowledge]

Background Information:

I'm trying to build a game using Java + Pulpcore, but I'm fairly new to the scene. The game I'm building could possibly be more performance intensive than I thought, and I know Java would solve my problems with this, but there are a couple questions I have dealing with strict-types, etc.

Here is my code in AS3:

Main.as3

import org.tbmb.champions.Container;
import org.tbmb.zombies.ZContainer;

public class Main extends MovieClip {

    // ******* temporary properties ******* /
    private var blunder:Container = null;
    // *******                      ******* /

    public function Main() {

        init(); // initialize objects

    }

    public function init():void {

        blunder = new Container(Blunder as Class);

    } // end of class

}

Container.as3

 package org.tbmb.champions {

     import flash.display.MovieClip;

     public class Container extends MovieClip {

          public function Container(champ:*) {

          } // end of constructor

     } // end of class

 } // end of package

Blunder.as3

package org.tbmb.champions.blunder {

    import flash.display.MovieClip;

    public class Blunder extends MovieClip {

        public function Blunder() {

        } // end of constructor

    } // end of class

} // end of constructor

1.) How would I rewrite in Java?

blunder = new Container(Blunder as Class);

2.) How would I be able to accept any Classes in Java for the above line within my Container class?

public function Container(champ:*) {

I need to do this because I'm sending different champion classes (depending on what the user picks) to a containing class that will hold all their other classes (health, etc). I need my Container class to accept any Class rather than just one; what type would I use?

Here is what I have in Java so far:

ZomboPulp.java (Main Class)

import pulpcore.scene.Scene2D;

import org.tnpfk.champions.Container;
import org.tnpfk.champions.blunder.Blunder;

import pulpcore.sprite.FilledSprite;
import pulpcore.image.Colors;

public class ZomboPulp extends Scene2D {

    FilledSprite background = new FilledSprite(Colors.WHITE);

    Container container = null; // Container that contain's blunder, 
    // and all his objects (health, mana, etc)

    public void load() {

        this.initScreen(); // initialize main screen.
        this.initObjects(); // initialize our objects.

    } // end of load();

    public void initScreen() {

        add(background);

    } // end of initScreen();

    public void initObjects() {

        container = new Container(Blunder as Class); // ERROR HERE

    } // end of initObjects();

}

Container.java

package org.tnpfk.champions;

public class Container {

    public Container(Object champ) {

    } // end of constructor

} // end of class

Sorry for the lengthy post, and thanks for any help. By the way, I did check StackOverflow; and Google, but I was unable to find anything about this.

Thanks,
jvmpulp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

游魂 2024-12-05 16:33:21

好吧!我没有使用 PulpCore 的经验,但我了解 AS3 和 Java,所以我想我可以回答你的问题。首先,我想我并不 100% 理解您需要对 Container 类中的 champ 对象做什么,而且我真的不明白为什么您要执行 Blunder as Class 而不仅仅是传递一个错误的实例。无论哪种方式,这就是我对您目前所拥有的内容的建议:

public void initObjects() {

    container = new Container(Blunder.class);

}

如您所见,您只需通过获取任何类的 class 属性即可获取 Class 实例。现在,您已经有了使用 Object 作为任何类型的 Container 构造函数的类型的正确想法。但是,使用 Object 是不好的做法(使用方法重载/更具体的类型代替),这里甚至不需要它。获取 class 属性将始终为 Class 类型,即使它们代表不同的类。因此,将构造函数重写为:

public Container(Class champ) {

}

然后,对该类执行您需要执行的操作。

现在,这基本上是一个直接端口,但似乎你正在做的一些事情是不好的做法。传递 Class 对象的整个系统似乎无关紧要且不必要;为什么不只传递对象的实例呢?例如,如下所示:

public class Container {

    protected Champion champ;

    public Container(Champion champ) {

        this.champ = champ;

    }

}

现在,使 Champion 成为一个抽象类,其中包含所有冠军的通用方法:(

public abstract class Champion {

    protected Something something;

    abstract Something getSomething();

}

显然,此处显示的变量/方法只是示例。)然后,让您的个人冠军类子类Champion

public class Blunder extends Champion {

    public Something getSomething() {
        return this.something;
    }

}

等等。然后,最后,执行以下操作:

public void initObjects() {

    container = new Container(new Blunder());

}

显然,这是一个基本示例,您不必接受我的建议。但它可能比 AS3 中已有的系统更容易实现。

Alrighty! I have no experience with PulpCore, but I do know both AS3 and Java, so I think I can answer your question. First off, I guess I don't 100% understand what you need to do with the champ object in the Container class, and I really don't understand why you were doing Blunder as Class instead of just passing an instance of Blunder. Either way, here's what I'd recommend with what you have as of now:

public void initObjects() {

    container = new Container(Blunder.class);

}

As you can see, you can get a Class instance just by getting the class property of any class. Now, you have the right idea with using Object as the type for the Container constructor for any type. However, using Object is bad practice (use method overloading/more specific types instead), and it's not even required here. Getting the class property will always be of type Class, even though they represent different classes. So, rewrite the constructor as this:

public Container(Class champ) {

}

Then, do whatever you need to do with the class.

Now, that's basically a direct port, but it seems some of the things you're doing are bad practice. The whole system of passing a Class object seems irrelevant and unnecessary; why not just pass an instance of the object? For example, like so:

public class Container {

    protected Champion champ;

    public Container(Champion champ) {

        this.champ = champ;

    }

}

Now, make Champion an abstract class that contains the common methods for all the champions:

public abstract class Champion {

    protected Something something;

    abstract Something getSomething();

}

(Obviously, the variable/method shown here are just examples.) Then, have your individual Champion classes subclass Champion:

public class Blunder extends Champion {

    public Something getSomething() {
        return this.something;
    }

}

Etc. Then, finally, do this:

public void initObjects() {

    container = new Container(new Blunder());

}

Obviously, this is a basic example, and you don't have to take my advice. But it would probably be easier to do than the system you already had in AS3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文