简体   繁体   English

使用 String 时,构造函数不能应用于给定类型

[英]Constructor cannot be applied to given type when using String

Can anyone tell me where i have gone wrong in creating a construtor to call a method in another class file.谁能告诉我在创建构造函数以调用另一个类文件中的方法时出错的地方。 It tells me constructor cannot be applied to given type它告诉我构造函数不能应用于给定类型

If you remove your constructor declaration from the Student class, then the implicit empty constructor will exist.如果从 Student 类中删除构造函数声明,则隐式空构造函数将存在。 But if you declared explicitly a constructor like your public Student(String firstname, String surname, String total) then the empty constructor won't be available, unless you add it explicitly to your class.但是,如果您显式声明了一个像public Student(String firstname, String surname, String total)这样的构造函数,那么空构造函数将不可用,除非您将它显式添加到您的类中。

public Student() { };

Then you call unexistant methods :然后你调用不存在的方法:

newStudent.firstName("my name");

The method you want to call, as declared in your Student object is setFirstName(String firstName)您要调用的方法,如在您的 Student 对象中声明的那样是setFirstName(String firstName)

Finally in your println calls, you are trying to access the attribut of the object directly instead of using the getters methods.最后,在 println 调用中,您尝试直接访问对象的属性,而不是使用 getter 方法。

You Student class don't have an empty constructor like youd do :你 Student 类没有像你那样的空构造函数:

Student newStudent = new Student();

So you have a Constructor which should take three String :所以你有一个构造函数,它应该接受三个 String :

public Student(String firstname, String surname, String total) {

So to solve your problem you have two ways :因此,要解决您的问题,您有两种方法:

1st第一

Create an empty constructor that not take any attribute like this :创建一个不接受任何属性的空构造函数,如下所示:

public Student() {
}

and you can put your values with setAtt for example :并且您可以将您的值与 setAtt 例如:

newStudent.setFirstName("my name");

2ed 2次

You should to call your constructor with the three value like this :你应该像这样用三个值调用你的构造函数:

Student newStudent = new Student("name 1", "name 2", "name 3");

Note笔记

You can't set a value to an attribute in another class like this :您不能像这样为另一个类中的属性设置值:

newStudent.firstName("my name");

You can make it like this :你可以这样做:

newStudent.firstName = "my name";

Some change I made to correct the code :我为更正代码所做的一些更改:

public class Main {
    public static void main(String[] args) {
        Student newStudent = new Student("Toto", "FromStack");
        newStudent.setFirstName("Jack");
        System.out.println(newStudent.getFirstName());
        System.out.println(newStudent.getSurname());
        System.out.println(newStudent.getFullName());
    }
}
  • You have to call the constructor with the good number of parameters您必须使用大量参数调用constructor函数

  • Prefer set the visibility of the parameter as private (look in Student ) and use Getters to acces to them最好将参数的可见性设置为private (查看Student )并使用Getters来访问它们

  • Also use Setters to changes the values还可以使用Setters来更改值

And here too :这里也是:

public class Student {
    private String firstName;
    private String surname;

    public Student(String firstname, String surname) {
        this.firstName = firstname;
        this.surname = surname;
    }
    // First Name GETTER
    public String getFirstName() {
        return firstName;
    }
    // First Name SETTER
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    // Surname GETTER
    public String getSurname() {
        return surname;
    }
    // Surname SETTER
    public void setSurname(String surname) {
        this.surname = surname;
    }
    //Full Name GETTER
    public String getFullName() {
        return firstName+" "+surname;
    }
}
  • If you're using Eclipse IDE it cans creat for you the Getters and Setters for you (in Source )如果您使用 Eclipse IDE,它可以为您创建 Getter 和 Setter(在Source

  • You don't need a total attribut because it's just a concatenation of the 2 others, you will never change it alone, if it changes it will be because of a change in one of the 2 attributs, so just a getFulName() which will get back both names您不需要total属性,因为它只是其他 2 个属性的串联,您永远不会单独更改它,如果它发生更改,那将是因为 2 个属性之一发生了更改,因此只需一个getFulName()取回两个名字

And for end you can create an empty constructor if you want to sets the value after, ="" is not get "null" value for each最后,如果您想在之后设置值,则可以创建一个空的构造函数, =""不会为每个获得“空”值

 public Student() {
            this.firstName = "";
            this.surname = "";
    }

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

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