Java 如何使用显示方法并调用构造函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27362654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:14:06  来源:igfitidea点击:

How do I use display method and call constructor?

javaconstructorinstance

提问by New2Java

I'm required to use a variable for x and y. I must set up a method to get x, y. I must use a method to set x and y. I must use a display method to display the points of x and y. Then use a constructor that accepts user input and sets them to x and y. Finally create a main class that creates 2 instances of x and y. I think my problem is occurring my display method. My program compiles/builds with no errors; however nothing displays or prompts user for input. Do I possibly need to call my constructor in the first class before trying to call the first class in the main method?

我需要为 x 和 y 使用一个变量。我必须设置一个方法来获取 x, y。我必须使用一种方法来设置 x 和 y。我必须使用显示方法来显示 x 和 y 的点。然后使用接受用户输入并将它们设置为 x 和 y 的构造函数。最后创建一个主类,创建 x 和 y 的 2 个实例。我认为我的问题出在我的显示方法上。我的程序编译/构建没有错误;但是没有任何显示或提示用户输入。在尝试调用 main 方法中的第一个类之前,我是否可能需要在第一个类中调用我的构造函数?

First File:

第一个文件:

public class Point2D extends JFrame
{
   Scanner input = new Scanner(System.in);
   private String x;
   private String y;


   public String getX()
   {
       return x;

   }
   public String getY()
   {
       return y;

   }

   public void setValue(String whatIsX, String whatIsY)
   {
      x = whatIsX;
      y = whatIsY;

   }

   public void display()
   {
      System.out.println(x);
      System.out.println(y);
   }

   public void Point2D()
   {
      System.out.println("Please enter value for X >>");
      input.nextLine();
      x = input.nextLine();
      System.out.println("Please enter value for Y >>");
      input.nextLine();
      y = input.nextLine();
   }

}

2nd File:

第二个文件:

public class MainPoint2D 
{
    public static void main(String[] args)
    {
       Point2D a = new Point2D();
       Point2D b = new Point2D();
    }
}

采纳答案by mdnghtblue

Remove voidfrom your "constructor":

void从“构造函数”中删除:

 public Point2D()
 {
  System.out.println("Please enter value for X >>");
  input.nextLine();
  x = input.nextLine();
  System.out.println("Please enter value for Y >>");
  input.nextLine();
  y = input.nextLine();
 }

Constructors don't have return types, and the compiler is treating this as a class method.

构造函数没有返回类型,编译器将其视为类方法。

Also, don't forget to call any methods you want to execute after you create your object:

另外,不要忘记在创建对象后调用任何要执行的方法:

   Point2D a = new Point2D();
   Point2D b = new Point2D();
   a.display();
   b.display();

回答by outdev

Remove the voidin the public void Point2D()

删除无效公共无效的Point2D()

回答by Bill the Lizard

public void Point2D()defines a method named "Point2D" with a voidreturn type, not a constructor. Constructors don't have any return type, not even void. Change that to public Point2D().

public void Point2D()定义了一个名为“Point2D”的方法,带有void返回类型,而不是构造函数。构造函数没有任何返回类型,甚至void. 将其更改为public Point2D().

Currently, your mainmethod is calling the default Point2Dconstructor, which is not defined, so the compiler provides an empty one for you.

当前,您的main方法正在调用Point2D未定义的默认构造函数,因此编译器为您提供了一个空构造函数。

回答by ControlAltDel

I thik you want to change this

我想你想改变这个

public void Point2D()
   {
      System.out.println("Please enter value for X >>");
      input.nextLine();
      x = input.nextLine();
      System.out.println("Please enter value for Y >>");
      input.nextLine();
      y = input.nextLine();
   }

to this

对此

public Point2D()
   {
      System.out.println("Please enter value for X >>");
      input.nextLine();
      x = input.nextLine();
      System.out.println("Please enter value for Y >>");
      input.nextLine();
      y = input.nextLine();
   }