简体   繁体   English

如何根据用户输入创建新对象?

[英]How to create new objects based on user input?

I am not sure what the proper terminology for this is but, I am trying to figure out how I could create new instances of an object based on a users input.我不确定这的正确术语是什么,但是,我试图弄清楚如何根据用户输入创建对象的新实例。

Let's say I have 5 different classes of object and I will only need a total of 5 objects from all or some of the classes, I might have more than 1 of a certain class and none of another.假设我有 5 个不同的对象类,我只需要来自所有类或部分类的总共 5 个对象,我可能有超过 1 个某个类,而没有其他类。 what could I write that would create a new instance of all or some of these objects?我可以写什么来创建所有或部分这些对象的新实例?

Furthermore, I would need to have the new objects fields be set to specific parameters without the users input and I would need to have an ArrayList created at the same time.此外,我需要在没有用户输入的情况下将新对象字段设置为特定参数,并且我需要同时创建一个ArrayList

Here is an example of 1 such object and arraylist I might need to be generated based on user input with these exact fields:这是我可能需要根据用户输入使用这些确切字段生成 1 个这样的对象和数组列表的示例:

ArrayList<Item> rangerInv0 = new ArrayList<Item>();    
Ranger rangerObj0 = new Ranger("Ranger", 100, 4, 10, rangerInv0);

Is there a way I could do this?有没有办法做到这一点? What would this be called?这会叫什么?

Also, I am looking to avoid a situation like this另外,我希望避免这样的情况

if (resp == 1){
  if (rangerCount == 0){
      ArrayList<Item> rangerInv0 = new ArrayList<Item>();    
      Ranger rangerObj0 = new Ranger("Ranger", 100, 4, 10, rangerInv0);

      rangerObj0.pickUp(mh1000);
      rangerObj0.pickUp(s1000);
      rangerObj0.pickUp(t1000);

      rangerObj0.equip(mh1000);
      rangerObj0.equip(s1000);
      rangerObj0.equip(t1000);

      playerUnits.add(rangerObj0);
      playerUnitsCount++;
      rangerCount++;
      }
      else if (rangerCount == 1){
      ArrayList<Item> rangerInv1 = new ArrayList<Item>();    
      Ranger rangerObj1 = new Ranger("Ranger", 100, 4, 10, rangerInv1);

      rangerObj1.pickUp(mh1000);
      rangerObj1.pickUp(s1000);
      rangerObj1.pickUp(t1000);

      rangerObj1.equip(mh1000);
      rangerObj1.equip(s1000);
      rangerObj1.equip(t1000);

      playerUnits.add(rangerObj1);
      playerUnitsCount++;
      rangerCount++;
      }
      else if (rangerCount == 2){
      ArrayList<Item> rangerInv2 = new ArrayList<Item>();    
      Ranger rangerObj2 = new Ranger("Ranger", 100, 4, 10, rangerInv2);

      rangerObj2.pickUp(mh1000);
      rangerObj2.pickUp(s1000);
      rangerObj2.pickUp(t1000);

      rangerObj2.equip(mh1000);
      rangerObj2.equip(s1000);
      rangerObj2.equip(t1000);

      playerUnits.add(rangerObj2);
      playerUnitsCount++;
      rangerCount++;
      }
    } 

is there a way I could update my object and ArrayList names via variables or some kind of counter?有没有办法通过变量或某种计数器更新我的对象和 ArrayList 名称?

it would be cool if I could do something like this but so far I cannot find a way to make this work:如果我能做这样的事情会很酷,但到目前为止我找不到使这项工作的方法:

int rangerCount = 0;
String rangerInvNum = ("rangerInv" + rangerCount);
String rangerObjNum = ("rangerObj" + rangerCount);

ArrayList<Item> rangerInvNum = new ArrayList<Item>();    
Ranger rangerObjNum = new Ranger("Ranger", 100, 4, 10, rangerInvNum);

What you need is to use proper design pattern for this kind of logic and what you can to choose here seems to be Builder , Factory or Strategy (the last one in this case would be kind of factory actually)您需要的是为这种逻辑使用适当的设计模式,您在这里可以选择的似乎是BuilderFactoryStrategy (在这种情况下,最后一个实际上是一种工厂)

The simpliest solution like this would look like像这样最简单的解决方案看起来像

public interface Item {}

public class Ranger implements Item {
    // ...
}

// ...

public class CommonCreationProperties {
    // All the fields user can popularne
}

public class ItemFactory{
    public Item createItem(CommonProperties properties)
        if(isRanger(properties)) {
            return new Ranger(properties.getType(), ...); // Event better introduce builder here
        } else if(is...) {
             return ...
        }
    }

    private boolean isRanger(CommonProperties properties) {
        return "Ranger".equals(properties.getType());
    }

    // ...
}

Of course providing some special type field to recognize what type of Item user provided is one of the options - you can implement some another mechanism - the only thing is that using such approach your items should have some common interface like Item and many implementations depends on what types of items you need当然,提供一些特殊的type字段来识别用户提供的Item类型是其中一个选项 - 您可以实现一些其他机制 - 唯一的事情是使用这种方法您的项目应该具有一些通用接口,例如Item并且许多实现取决于您需要什么类型的物品

You could try a switch case:你可以试试开关盒:

String className = "user input here"
switch(className) {
  case "Ranger": {
    Ranger myRanger = new Ranger();
    ArrayList<Item> rangerInv0 = new ArrayList<Item>();
    break;
    }
  case "otherClass": {
    OtherClass myObj = new OtherClass();
    ArrayList<Item> otherClassInv0 = new ArrayList<Item>();
    break;
    }
case "....": {
    // ....
    break;
    }
  default: {
    // print error message, end program, ...
    }
}
  • This will help to you这对你有帮助
  • you don't need to create Object types and One by one variables and you can use some generics like I have implemented你不需要创建对象类型和一个一个的变量,你可以使用我已经实现的一些泛型

public class Ranger<E> {

    private  String objectName;
    private Type val1;
    private Type val2;
    private Type val3; // here you can set any kind of typs ex, int,string 
    private E[] rangerInv0; // you can set any kind of array 

    public Ranger(String objectName, Type val1, Type val2, Type val3, E[] rangerInv0) {
        super();
        this.objectName = objectName;
        this.val1 = val1;
        this.val2 = val2;
        this.val3 = val3;
        this.rangerInv0 = rangerInv0;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM