gpt4 book ai didi

java - 你能创建一个类实例数组吗?

转载 作者:行者123 更新时间:2023-12-01 19:59:00 25 4
gpt4 key购买 nike

是否可以创建类实例数组?

下面是我尝试的一个基本示例。重点关注方法“生成”。

import java.util.Scanner;

public class Main {

public static Scanner Scan = new Scanner(System.in);

public static void main(String[] args) {

System.out.println("What is your name?");
String name = Scan.nextLine();
Player player1 = new Player(name);
player1.getStats();
}

public static void generate() {

String[] weaponShopInv = {rustySword, ironSword, sharpSword}

Weapon rustySword = new Weapon("Rusty Sword","Melee","Short Sword", 5, 30);
Weapon ironSword = new Weapon("Iron Sword","Melee","Short Sword", 10, 100);
Weapon sharpIronSword = new Weapon("Sharp Iron Sword","Melee","Short Sword", 15, 250);
}

}

为了以防万一,下面是正在实例化的类的代码。

public class Weapon {

String name;
String type;
String style;
int damage;
int price;

public Weapon(String e, String a, String b, int c, int d) {
type = a;
style = b;
damage = c;
price = d;
name = e;
}
}

最佳答案

顺序和类型声明不正确

Weapon rustySword = new Weapon("Rusty Sword","Melee","Short Sword", 5, 30);
Weapon ironSword = new Weapon("Iron Sword","Melee","Short Sword", 10, 100);
Weapon sharpIronSword = new Weapon("Sharp Iron Sword","Melee","Short Sword", 15, 250);

Weapon[] weaponShopInv = {rustySword, ironSword, sharpSword}

关于java - 你能创建一个类实例数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48615107/

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