简体   繁体   English

Java构造函数新手需要帮助

[英]new to java constructor help needed

I have an past exam question that says: "Create a class Element that records the name of the element as a String and has a public method, toString that returns the String name. Define a constructor for the class (that should receive a String to initialise the name)." 我有一个过去的考试问题,它说:“创建一个类Element ,该元素将元素的名称记录为String并具有一个公共方法toString ,该方法返回String的名称。为该类定义一个构造函数(应将String接收为初始化名称)。”

I gave it a go and don't where to go from here... 我试了一下,不要从这里去哪里...

main class is: 主要类别是:

public class builder { 
public static void main(String[] args) { 
    element builderObject = new element(elementName);

}
}

and constructor is: 构造函数是:

    import java.util.*;
class element {
public int getInt(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the first number");
    String elementName = scan.nextLine();
    System.out.println("%s");
}

public String toString() {
    return elementName;
}
}

I can't think of a way to explain this without actually giving the answer, so.... 如果没有给出答案,我想不出办法来解释这一点,所以...

public class Element { /// Create class Element

    private final String name; // Record the 'name'

    public Element(String name) { // constructor receives and sets the name
        this.name = name;
    }

    public String toString() { // public method toString() returns the name
        return name;
    }
}

You are missing the constructor itself. 您缺少构造函数本身。 The point of constructors is to initialize the object, usually by saving the given parameters to data members. 构造函数的目的是初始化对象,通常是通过将给定的参数保存到数据成员中来进行的。 Eg: 例如:

class Element {
    /** A data member to save the Element's name */
    private String elementName;

    /** A constructor from an Element's name*/
    public Element(String elementName) {
        this.elementName = elementName;
    }

    @Override
    public String toString() {
        return elementName;
    }
}
class Element {
    private String name = "";

    /**
    /* Constructor
    /**/
    public void Element(final String name){
        this.name = name;
    }

    @Override
    public String toString(){
        return name;
    }
}

You don't have a constructor in there. 您那里没有构造函数。 A constructor typically looks something like this: 构造函数通常如下所示:

public class MyClass {
     private String name;
     private int age;         

     //This here is the constructor:
     public MyClass(String name, int age) {
         this.name = name;
         this.age = age;
     }

     //here's a toString method just for demonstration
     @Override
     public String toString() {
         return "Hello, my name is " + name + " and I am " + age + " years old!";
     }
}

You should be able to use that as a guideline for making your own constructor. 您应该能够以此为指导来制作自己的构造函数。

class Element 
{
  private String name = "UNSET";
  public String getName() { return name; }

  public Element(String name) {
    this.name = name;
  }

  public String toString() { 
   return getName(); 
  }
}

You are missing a constructor you might be looking for something like this 您缺少构造函数,可能正在寻找类似这样的东西

public class Element{
   private String name;

   public Element(String name){ //Constructor is a method, having same name as class
       this.name = name;
   }

   public String toString(){
       return name;
    }
}

A note 一张纸条
I take you are starting with java, In java class names usually start with capital letter, thus element should be Element . 我认为您是从Java开始的,在Java类名中通常以大写字母开头,因此element应该是Element Its important that one picks up good habits early.. 重要的是要及早养成良好的习惯。

Don't get frustrated. 不要沮丧。 Please read java tutorials first and understand the concepts. 请先阅读Java教程并了解概念。 your exam question is very clear on what you need to do. 您的考试问题很清楚您需要做什么。 Atleast for this question, you need to know what is constructor, the purpose of having toString() in a class. 对于这个问题,您需要知道什么是构造函数,即在类中具有toString()的目的。

May be the below can help you. 可能以下可以帮助您。

public class Element {

private String elementName;

public Element(String elementName) {
    this.elementName = elementName;
}

@Override
public String toString() {
    return elementName;
}

}

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

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