简体   繁体   English

java构造函数错误(无法将int传递给构造函数)

[英]java constructor error (cannot pass int to the constructor)

hi I'm new to java and I've been trying to pass an int form main method to a constructor in another class but there is some kind of error occurring. 嗨,我是java的新手,我一直在尝试将int形式的main方法传递给另一个类中的构造函数,但是会发生某种错误。 I can't understand what i did wrong. 我不明白我做错了什么。

class with the main method : 类的主要方法是:

import java.util.Scanner;

public class _01 {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your : ");
        String name = input.nextLine();
        int size = name.length();

        //System.out.println(size);


        _02 process = new _02(size);

    }

}

class that has the constructor: 具有构造函数的类:

public class _02 {

    int maxsize;
    int top;
    String arrayStack[];

    public void _02(int size) {

        maxsize = size;
        arrayStack = new String[maxsize];
        top = -1;
    }

    public void push(String... letters) {

        arrayStack[++top] = letters;

    }

    public String pop() {

        return arrayStack[top--];


    }
}

error message i'm getting : 我收到的错误消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor _02(int) is undefined 线程“主”中的异常java.lang.Error:未解决的编译问题:构造函数_02(int)未定义

at _01.main(_01.java:16) 在_01.main(_01.java:16)

That's because you are using void . 那是因为您正在使用void Java thinks those are methods, not constructors. Java认为这些是方法,而不是构造函数。 First of all stop giving your classes awful names. 首先,停止为您的班级命名。 And then do this: 然后执行以下操作:

public class Whatever {
   private Integer size;
   public Whatever(Integer size) {
      this.size = size;
      System.out.println("I am a constructor");
   };
};

Example

public class Article {
    private String title;
    private String content;
    private String author;
    private DateTime publishDate;

    public Article(String title, String content, String author, DateTime publishDate) {
        this.title = title;
        this.content = content;
        this.author = author;
        this.publishDate = publishDate;
    };

};

Assume we are working together on a newspaper project. 假设我们正在一起开展报纸项目。 If I read your code and see _01 , it means nothing to me. 如果我阅读了您的代码并看到_01 ,对我来说就没有任何意义。 If instead I see Article , with title , content , etc. I can immediately understand what you wrote and use it as I please. 相反,如果看到带有titlecontent等的Article ,我可以立即理解您写的content ,并按我的意愿使用它。

If you come back to your own code two weeks later, you won't have a clue what you meant by _01 . 如果两周后返回自己的代码,您将不了解_01含义。 As many developers, you won't remember. 作为许多开发人员,您不会记得。 There's even a joke about it, done through a comment. 通过评论,甚至有个笑话。 It goes like this: 它是这样的:

/**
 * At the time of this writing, only God and I knew what I was doing.
 * Now, only God knows.
 * /

Remeber, code is easy to write but very hard to read. 记住,代码易于编写,但很难阅读。

A constructor is a method without return type, so your constructor declaration should be: 构造函数是没有返回类型的方法,因此您的构造函数声明应为:

public _02(int size)

A good practice will be to use follow java style guide, using names with capitalize letrer as class names, for example Two 一个好的做法是使用遵循Java样式指南,将大写字母的名称用作类名,例如, Two

A constructor doesn't have a return type. 构造函数没有返回类型。 So remove void from public void _02(int size) if you want to use it as a constructor. 因此,如果您想将其用作构造函数,请从public void _02(int size)删除void

Your class naming is a little odd though... 你的班级命名有点奇怪...

从公共void _02(int size){...}中删除“ void”

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

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