由用户创建新实例
creating new instance by user
作为最熟练的 JAVA 用户,您可能已经看到,这是行不通的。 sb
不能同时用作 String
和 instance
。但是你也一定已经知道我在这里想做什么。我尝试让用户创建一个新的instance
of the class Cook
。但是由于 cook1
和 cook2
已经存在(已经在代码中创建),我现在希望用户创建一个新的。 sb
必须以某种方式充当变量,导致必须将潜在的下一位厨师插入为 cook4
等等......
在Java中如何做到这一点???
package kebabStore;
import java.util.Scanner;
public class NewCook {
static String newcookname;
static String newcookaddress;
static String newcookpobox;
static String newcooktown;
static int newcooktaxnumber;
static Scanner scanner = new Scanner(System.in);
public static void newcook () {
Cook.numberofcooks++; //this works, this counter adds up.
System.out.println("Enter name new cook");
newcookname = scanner.nextLine();
System.out.println("enter address new cook");
newcookaddress = scanner.nextLine();
System.out.println("Enter PO Box new cook");
newcookpobox = scanner.nextLine();
System.out.println("Enter town new cook?");
newcooktown = scanner.nextLine();
System.out.println("Enter tax number new cook");
newcooktaxnumber = scanner.nextInt();
StringBuilder sb = new StringBuilder("cook");
sb.insert(3,Cook.numberofcooks);
System.out.println(sb); // check if the constructor works, yes it does!! it says "cook3"
Cook sb = new Cook (newcookname, newcookaddress, newcookpobox, newcooktown, newcooktaxnumber);
System.out.println("A new cook has been hired " +sb.getName() + ", living in " + sb.getTown() );
}
}
你应该用集合来做(正如评论中已经提到的那样)。我还假设您应该使您的方法 return 成为您创建的实例:
public static Cook newcook() {
//all your logic is the same
return sb;
}
然后,在调用方法的作用域的开头 newcook
:
List<Cook> cooks = new ArrayList<Cook>();
...
cooks.add(cook1);
cooks.add(cook2); // Since they already exist, as you mentioned
cooks.add(newcook()); // inserts third
作为最熟练的 JAVA 用户,您可能已经看到,这是行不通的。 sb
不能同时用作 String
和 instance
。但是你也一定已经知道我在这里想做什么。我尝试让用户创建一个新的instance
of the class Cook
。但是由于 cook1
和 cook2
已经存在(已经在代码中创建),我现在希望用户创建一个新的。 sb
必须以某种方式充当变量,导致必须将潜在的下一位厨师插入为 cook4
等等......
在Java中如何做到这一点???
package kebabStore;
import java.util.Scanner;
public class NewCook {
static String newcookname;
static String newcookaddress;
static String newcookpobox;
static String newcooktown;
static int newcooktaxnumber;
static Scanner scanner = new Scanner(System.in);
public static void newcook () {
Cook.numberofcooks++; //this works, this counter adds up.
System.out.println("Enter name new cook");
newcookname = scanner.nextLine();
System.out.println("enter address new cook");
newcookaddress = scanner.nextLine();
System.out.println("Enter PO Box new cook");
newcookpobox = scanner.nextLine();
System.out.println("Enter town new cook?");
newcooktown = scanner.nextLine();
System.out.println("Enter tax number new cook");
newcooktaxnumber = scanner.nextInt();
StringBuilder sb = new StringBuilder("cook");
sb.insert(3,Cook.numberofcooks);
System.out.println(sb); // check if the constructor works, yes it does!! it says "cook3"
Cook sb = new Cook (newcookname, newcookaddress, newcookpobox, newcooktown, newcooktaxnumber);
System.out.println("A new cook has been hired " +sb.getName() + ", living in " + sb.getTown() );
}
}
你应该用集合来做(正如评论中已经提到的那样)。我还假设您应该使您的方法 return 成为您创建的实例:
public static Cook newcook() {
//all your logic is the same
return sb;
}
然后,在调用方法的作用域的开头 newcook
:
List<Cook> cooks = new ArrayList<Cook>();
...
cooks.add(cook1);
cooks.add(cook2); // Since they already exist, as you mentioned
cooks.add(newcook()); // inserts third