Java 将一条线分成两个不同的对象

Java 将一条线分成两个不同的对象,java,object,Java,Object,我不知道如何实现这一点 假设我有两个班,狗和猫 public class Dog { private int a, b; private double c; //Constructors etc } public class Cat { private int x, y; private char z; //Constructors etc } 我还得到了一个字符串 “a、b、c/x、y、z” 字符串可以是任意组合,即Cat/Dog、Dog/C

我不知道如何实现这一点

假设我有两个班,狗和猫

public class Dog
{
    private int a, b;
    private double c;
    //Constructors etc
}

public class Cat
{
    private int x, y;
    private char z;
    //Constructors etc
}
我还得到了一个字符串

“a、b、c/x、y、z”

字符串可以是任意组合,即Cat/Dog、Dog/Cat、Dog/Dog等。每个部分的第三个值可以是double或char

我已经拆分了字符串并创建了两个数组。 这就是我到目前为止所做的:

public void object ()
{

    //My code to Split the line on "/" into 2 parts
    //splits the part on "," to create two String arrays

    //oneArray[] is [a,b,c] twoArray[] is [x,y,z]

    oneArray[2] = ?? //Not sure how to create the right object that matches the data type
    twoArray[2] = ??
 } 

当第三个值属于不同的数据类型时,如何创建两个对象(无论字符串组合如何)

您可以尝试以下方法:

List<Object> animalsList = new ArrayList<>(); // Will contains list of all animals
String input = "Cat/Dog, Dog/Cat ,Dog/Dog "; // Sample Input List
String [] inputEntries = input.split(","); // Separating based on comma
for(String inputEntry: inputEntries)
{
   String [] animals = inputEntry.split("/");
   for(String animal: animals)
   {
    objectList.add(animal.equalsIgnoreCase("Cat")?new Cat():new Dog());  
                                                         /\                                                             
                                                         ||
                                                         ||
                                  You can pass the values to constructor from here
    }
}
List animalsList=new ArrayList();//遗嘱包含所有动物的名单
字符串输入=“猫/狗,狗/猫,狗/狗”//样本输入列表
字符串[]inpuntertries=input.split(“,”);//基于逗号的分离
for(字符串inpunterry:inpunterries)
{
字符串[]动物=inpuntery.split(“/”);
用于(字符串动物:动物)
{
objectList.add(animal.equalsIgnoreCase(“Cat”)?new Cat():new Dog());
/\                                                             
||
||
您可以从这里将值传递给构造函数
}
}

以下代码以更面向对象的方式解决您的问题:

实用工具方法(检查第三个元素是否可以加倍解析或默认为字符串):

班犬:

class Dog implements Animal
{
    private Integer a;
    private Integer b;
    private String c;

    public Dog(Integer a, Integer b, String c)
    {
        super();
        this.a = a;
        this.b = b;
        this.c = c;
    }
}
类别类别:

class Cat implements Animal
{
    private Integer x;
    private Integer y;
    private Double c;

    public Cat(Integer x, Integer y, Double c)
    {
        super();
        this.x = x;
        this.y = y;
        this.c = c;
    }
}
class Dog implements Animal
{
    private Integer a;
    private Integer b;
    private String c;

    public Dog(Integer a, Integer b, String c)
    {
        super();
        this.a = a;
        this.b = b;
        this.c = c;
    }
}
class Cat implements Animal
{
    private Integer x;
    private Integer y;
    private Double c;

    public Cat(Integer x, Integer y, Double c)
    {
        super();
        this.x = x;
        this.y = y;
        this.c = c;
    }
}