简体   繁体   English

接受参数化构造函数的值的方法

[英]Method to take in values for parameterized constructor

I'm running into a problem here. 我在这里遇到问题。 I was told to initialize values through a parameterized constructor. 有人告诉我通过参数化构造函数初始化值。 Write a method "getInput" to take in values for these attributes, call the parameterized constructor and then return a Child object. 编写方法“ getInput”以获取这些属性的值,调用参数化的构造函数,然后返回Child对象。

I think this is what I did...but when I call the method i get something like this: Child@75d9fd51 Is this just the object reference? 我认为这就是我所做的...但是当我调用该方法时,我得到的是这样的:Child @ 75d9fd51这仅仅是对象引用吗? I'm trying to get it to print the inputted age, name and grade. 我正在尝试打印输入的年龄,姓名和等级。 Thank you in advance! 先感谢您!

So far this is what i have: 到目前为止,这就是我所拥有的:

import java.util.Scanner;

class Child{
    int age;
    String name;
    char grade;

public Child(){

}

public Child(int age, String name, char grade) {
    this.age = age;
    this.name = name;
    this.grade = grade;
}
public int getAge() {
    return age;
}
public void setAgel(int age) {
    this.age = age;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public char getGrade() {
    return grade;
}
public void setGrade(char grade) {
    this.grade = grade;
}
}

public class Data {

public static Child getInput(Child s){
    Scanner input = new Scanner(System.in);
    System.out.println("Input age: ");
    int age = input.nextInt();
    System.out.println("Input name: ");
    String name = input.next();
    System.out.println("Input grade: ");
    char grade = input.next().charAt(0);
    s = new Child(age, name, grade);

    return s;
}

public static void main(String[] args) {
    // TODO Auto-generated method stub


    Child x = new Child(20, "James", 'A');   //I want this to be changed with input

    getInput(x);

    System.out.print(x);
}

Override your toString methode in your child class 覆盖子类中的toString方法

public String toString(){
        return age + " " + name + " " + grade;
    }

when your run this line in your agent class which is Data 当您在代理类(即数据)中运行此行时

public static void main(String...args){
   Child x = new Child(20, "James", 'A');   
   System.out.print(x.toString());
  }

output: 输出:

Input age: 
32
Input name: 
izak
Input grade: 
A
32 izak A

No, 'Child@75d9fd51' is not a object reference. 不,“ Child @ 75d9fd51”不是对象引用。 It's the data stored in the instance x of the class Child. 它是存储在Child类的实例x中的数据。 In the Java programming language, every class provides a hashCode() method, which digests the data stored in an instance of the class into a single hash value (a 32-bit signed integer). 在Java编程语言中,每个类都提供一个hashCode()方法,该方法将存储在该类实例中的数据摘要为单个哈希值(32位带符号整数)。

If you want to get the input data and print the inputted data(age, name and grade), you should make some changes: 如果要获取输入数据并打印输入的数据(年龄,姓名和等级),则应进行一些更改:

First, change getInput(x); 首先,更改getInput(x); to x = getInput(x); x = getInput(x); . This statement assigns new class to the class x. 该语句将新类别分配给类别x。

Second, put the method 二,放方法

public String toString() { 
   return "Input age: " + getAge() + "\nInput name: " + getName() + "\nInput grade: " +        getGrade(); 
}

inside child class. 内孩子课。 When you call System.out.print(x); 当您调用System.out.print(x); on main method, System.out.print(x.toString()); 在主要方法上, System.out.print(x.toString()); is actually called. 实际上被称为。

A generic Object doesn't have a specific printable representation just because Java can't just guess how you'd like to print it. 通用Object没有特定的可打印表示形式,因为Java不能仅仅猜测您要如何打印它。 So what you obtain is the reference in memory. 因此,您获得的是内存中的引用。

But the language gives you the public String toString() method from Object class, which can be overridden to provide the specific representation you'd like. 但是该语言为您提供了Object类的public String toString()方法,可以重写该方法以提供您想要的特定表示形式。 This method is automatically called when you pass an object to the println method. 将对象传递给println方法时,将自动调用此方法。

Eg: 例如:

class Child {
  public String toString() { 
       return name + "," + grade + "," + age;
  }
}

When passed an object, System.out.println() will print out the results of the object's toString method. 传递对象时, System.out.println()将打印出对象的toString方法的结果。 The default output is in the format you're seeing. 默认输出采用您看到的格式。 If you want to see something more informative, you can either pass the fields individually 如果您想查看更多有用的信息,则可以分别传递字段

System.out.println("Age: " + x.getAge());
System.out.println("Name: " + x.getName());
System.out.println("Grade: " + x.getGrade());

or you can override the toString method on Child : 或者您可以重写Child上的toString方法:

@Override
public String toString() {
    return "Age: " + this.age + ", Name: " + this.name + ", Grade: " + this.grade;
}

Now you can call System.out.println(x) as you were before. 现在,您可以像以前一样调用System.out.println(x)

On a side note, there's no point in creating and passing an instance of Child just to replace it with a new instance in getInput . 附带说明一下,创建并传递Child的实例只是用getInput的新实例替换它是没有意义的。

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

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