使用通过用户输入传入的值在另一个类中创建类的实例

时间:2014-11-14 00:58:44

标签: java string class pointers return

我有两个课程:AnimalShelter。 我有一个toString方法,它应该打印出Shelter的名称以及来自动物的所有信息,这些信息都是通过扫描仪类通过用户输入收集的。但是,当我在Shelter类中创建Animal的实例时,那个实例中显然还没有任何值。

问题:如何将通过用户输入收集的值指向shelter类,以便它可以使用shelterName方法打印出toString()

用户输入:

public class AnimalShelter {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        System.out.println("What is your animal's name?");
        String animalName = scan.nextLine();
        System.out.println("What is your animal's type?");
        String type = scan.nextLine();
        System.out.println("What is your animal's age?");
        int age = scan.nextInt();
        scan.nextLine();
        System.out.println("What is the name of your animal shelter?");
        String shelterName = scan.nextLine();
        Shelter myShelter = new Shelter(shelterName);
        System.out.println(shelterName.toString());

    }

}

动物类:

public class Animal {

private String animalName;
private String type;
private int age;

    public Animal(String animalName, String type, int age)
    {
        this.animalName = animalName;
        this.type = type;
        this.age = age;
    }

    public String getAnimalName()
    {
        return animalName;
    }

    public String getType()
    {
        return type;
    }

    public int getAge()
    {
        return age;
    }

}

Shelter class:如何将用户输入的值指向myAnimal对象

public class Shelter {

    Animal myAnimal = new Animal(); 

    private String shelterName;

    public Shelter(String shelterName)
    {
        this.shelterName = shelterName;
    }

    public String toString(Animal myAnimal)
    {
        myAnimal.getAnimalName();
        myAnimal.getType();
        myAnimal.getAge();
        return "" + shelterName + myAnimal.getAnimalName() + myAnimal.getType() + myAnimal.getAge();
    }

}

3 个答案:

答案 0 :(得分:1)

在您的示例代码中,您有一个从未使用过的动物类。我反对勺子喂食的答案,但我心情很好,所以生病了一行一行解释。

public class AnimalShelter {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        //Save user input as temp variable values
        System.out.println("What is your animal's name?");
        String animalName = scan.nextLine();
        System.out.println("What is your animal's type?");
        String type = scan.nextLine();
        System.out.println("What is your animal's age?");
        int age = scan.nextInt();
         //Create an instance of your Animal class passing your temp variables
        Animal animal= new Animal(animalName,type,age);
        scan.nextLine();
        System.out.println("What is the name of your animal shelter?");
        String shelterName = scan.nextLine();

        //Create your Shelter class here passing the shelter name
        Shelter myShelter = new Shelter(shelterName);

        //Use the same class to pass your animal class(the one where you placed the attributes in) into the Shelter class.
        System.out.println(myShelter.toString(animal));

    }

}

答案 1 :(得分:0)

将以下代码附加到main()方法的尾部:

Animal animal = new Animal(animalName, type, age);
System.out.println(myShelter.toString(animal));

答案 2 :(得分:0)

简短的回答是,你可能不想!

在这里,您的AnimalShelter类似乎设置得很好,但实际上并不需要像这样链接。

相反,请考虑在Animal方法中创建main(...),然后使用新方法添加'他们到现有的避难所。

例如:

public class Shelter {
    // Animal myAnimal = new Animal(); Remove this
    private  String shelterName; // This is good.
    public Shelter(..) // This is fine too

    public String toString(...) // This works, perhaps?
    // New method here, with list
    private ArrayList<Animal> listOfAnimals_
    public void addAnimal(Animal newAnimal)
    {
        listOfAnimals_.add(newAnimal);
    }
}

然后从main

Animal TimTheCow = new Animal ("Tim", "Cow", 5);
myShelter.addAnimal(TimTheCow);

这消除了你不需要的一些强耦合,并减少了传递参数的压力。

如果您想列出Animal的所有Shelter,请将toString方法更改为:

public String toString()
{
    String newStr;
    for ( Animal animal : listOfAnimals ) // apologies if bad syntax
    {
        newStr += animal.toString();
    }
    return "" + shelterName + newStr;
}

并在您的Animal课程中

public String toString()
{
    return "" + getAnimalName() + getType() + getAge();
}