gpt4 book ai didi

java - 使用静态变量调用对象

转载 作者:行者123 更新时间:2023-12-02 07:41:35 24 4
gpt4 key购买 nike

我最近开始编写 3D 电脑游戏,作为我的 IB MYP 个人项目的一部分,尽管我了解足够多的 Java,但我在让我的类(class)协同工作时遇到了困难。我想要做的是创建一个名为 block 的类,它定义一个 block 及其所有属性,然后在每次创建具有唯一属性的单个 block 时调用该 block 类来获取 block 的基本描述。我尝试过有效的扩展,但我必须为每个独特的 block 创建一个新的扩展类,并且我尝试创建一个对象,但它不起作用。我所有的搜索都毫无结果。这是我的代码:

package src;

public class Block {
//Defines a Block

double id; //Full = type, decimal = subtype
String type; //Name/tooltip
int sound; //Type of sound played on collision
int light; //Ammount of light given off
boolean breaks; //Wether the block is breakable
boolean solid; //Wether the block has collision detection

}

如何在不同的类中多次调用该对象,每次的所有值都略有不同?

最佳答案

您可以为 block 创建一个构造函数,如下所示:



public Block(double id, String type, int sound, int light, booleanbreak, booleansolid) {
这个.id = id;
this.type = 类型;
this.sound = 声音;
this.light = 光;
this.breaks = 中断;
this.solid = 固体;
}

有了这个,您可以根据需要创建任意多种不同类型的 block 。

关于java - 使用静态变量调用对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11528225/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com