本文介绍了如何从main调用类进入类?并保持输出值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3节课。主要,ZombieEncounter和库存。

I have 3 classes. Main, ZombieEncounter and inventory.

这是我的主要:pastebin.com/9uyDsPTT

This is my main: pastebin.com/9uyDsPTT

这是我的库存:

这是我的zombieEncounter: 尚未完成

This is my zombieEncounter: http://pastebin.com/77U7gv2Z Not completed yet.

如果我制作另一个名为gun的类,其中包含用于弹药和剪辑大小的。我可以通过class main将该类添加到库存中吗?这样我就可以调用库存并在zombieEncounter类中使用它。 同样当zombieEncounter使用类枪时,我怎么能保存int? 让我们说我使用x量的弹药,如何保存剪辑大小/弹药使用的最终结果?

If I make another class titled "gun" with int for ammo and clip size. Can I add that class to the inventory through class main? So that I can call the inventory and use it in zombieEncounter class. Also how would I be able to save the int when zombieEncounter uses the class gun? Lets say I use "x" amount of ammo, how do I save the end result of the clip size/ammo usage?

对不起令人困惑的问题......我现在还是新手,我认为将源代码粘贴到代码示例中会很困难。

Sorry for the confusing question..I'm still new at this and I think it'll be hard to paste the source in a Code Sample.

推荐答案

解决这个问题的一种方法是给你的 ZombieEncounter 类,一个类型为库存的变量(可能是私有的?)并在构造函数中为其赋值。然后你可以在main中创建一个 Inventory 和一个 ZombieEncounter 对象,然后传递 Inventory 对象 ZombieEncounter 的构造函数。

One way of solving this would be to give your ZombieEncounter class, a variable (maybe private?) of type Inventory and assign it a value in the constructor. Then you can create an Inventory and a ZombieEncounter object inside your main and then pass the Inventory object to the ZombieEncounter's constructor.

对于保存部分,你必须提供有关如何实现它的更多信息。例如,您可以将其保存在全局变量或文件中。

For the saving part you have to provide more information about how you want to implement it. For example you can save that on a global variable or maybe in a file.

(请注意,最好将每个类变量更改为私有并创建setter / getters对于他们每个人)

(note that it is good practice to change to private every class variable and create setters/getters for each one of them)

这篇关于如何从main调用类进入类?并保持输出值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 05:03