简体   繁体   English

在Java中将具有多个变量类型的对象传递到arraylist时遇到麻烦

[英]Having troubles of passing an object with multiple variable types into arraylist in java

I am trying to save an object, that is created from a file, into an arraylist and I am having troubles with doing exactly that in a three-class program. 我试图将由文件创建的对象保存到arraylist中,而在三类程序中很难做到这一点。 I'm getting an error telling me to create constructors, but when I auto create them I get an error saying: Constructor cannot be applied to given types . 我在告诉我创建构造函数时遇到错误,但是当我自动创建它们时,我收到一条错误消息: Constructor cannot be applied to given types I am not sure what it means. 我不确定这是什么意思。 In short - I've been at this thing for hours now and cannot figure it out. 简而言之-我已经在这件事上待了几个小时,无法弄清楚。

public class Darbuotojas {
    String vardas;
    String pareigos;
    int gm;
    Float atlyginimas;

    public Darbuotojas(String vardas, String pareigos, int gm, Float atlyginimas){
        this.vardas = vardas;
        this.pareigos = pareigos;
        this.gm = gm;
        this.atlyginimas = atlyginimas;
    }
}

Here is the code where I read the file, and try to put Objects Darbuotojas into an ArrayList: 这是我读取文件的代码,并尝试将Objects Darbuotojas放入ArrayList中:

public class Viskas extends Darbuotojas{
    String gm1;
    String atlyginimas1;
    ArrayList<Darbuotojas> darbuotojai = new ArrayList<Darbuotojas>();

    public void failas(String fl) throws IOException{
        //Failu nuskaitymas po zodi
        File file = new File(fl);
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String line = null;

            while ((line = br.readLine()) != null){
                String [] tokens = line.split("/");
                vardas = tokens[0];
                pareigos = tokens[1];
                gm1 = tokens[2];
                gm = Integer.parseInt(gm1);
                atlyginimas1 = tokens[3];
                atlyginimas = Float.parseFloat(atlyginimas1);
                System.out.print(vardas.toUpperCase() + " ");
                System.out.print(pareigos.toUpperCase() + " ");
                System.out.print(gm + " ");
                System.out.println(atlyginimas);  
                Darbuotojas drb = new Darbuotojas(vardas,pareigos,gm,atlyginimas);
                darbuotojai.add(drb);
                System.out.println(drb);
            }

            br.close();
        }
        catch(FileNotFoundException e){
        }
    }
}

And here is the main function: 这是主要功能:

public static void main(String[] args) throws IOException {
    Scanner kb = new Scanner(System.in);
    System.out.println("Iveskite duomenu vailo pavadinima su failo tipu: ");
    String fl = kb.next();

    Viskas ddd = new Viskas();

    ddd.failas(fl);
}

I'm sorry about all the variables being in Lithuanian, but I don't think it changes anything code readability wise. 我对立陶宛语中的所有变量感到抱歉,但是我认为它不会改变代码的可读性。

In your example, you are instantiating a new Viskas . 在您的示例中,您将实例化一个新的Viskas Since Viskas extends Darbuotojas and does not have its own constructor, the constructor for Darbuotojas is called with missing parameters. 由于Viskas扩展了Darbuotojas并且没有自己的构造函数,因此使用缺少的参数调用Darbuotojas的构造函数。 To resolve this, either pass in the required parameters when making a new Viskas : 要解决此问题,请在制作新的Viskas时传递所需的参数:

(String vardas, String pareigos, int gm, Float atlyginimas)

or create a parameterless constructor for Darbuotojas . 或为Darbuotojas创建一个无参数的构造Darbuotojas

Darbuotojas has a defined constructor. Darbuotojas具有定义的构造函数。 Viskas extends Darbuotojas, yet it does not invoke the constructor of its parent Darbuotojas. Viskas扩展了Darbuotojas,但并未调用其父级Darbuotojas的构造函数。 Just create a constructor for Viskas and put super() at the top. 只需为Viskas创建一个构造函数,然后将super()放在顶部。

Maybe an example helps to explain the solution better. 也许有一个例子可以帮助更好地解释解决方案。 Let's assume you have two classes called Vehicle and Car. 假设您有两个类,分别称为Vehicle和Car。 Car extends Vehicle so that Car can inherit all the features from Vehicle. Car扩展了Vehicle,以便Car可以继承Vehicle的所有功能。

Parent Class: 家长班:

public class Vehicle{
    String name;
    int age;
}

Child Class: 儿童班:

public Class Car extends Vehicle {
 String type;
}

When you don't have constructors defined in the classes it will automatically create those inside and bellow is how it will look like. 当您在类中没有定义构造函数时,它将自动在内部创建构造函数,波纹管的外观如下。

Parent Class: 家长班:

public class Vehicle{

    String name;
    int age;

    public Vehicle(){
        super();
    }
}

Child Class: 儿童班:

public Class Car extends Vehicle {
    String type;

    public Car(){
        Super();
    }
}

When you have the constructor defined inside the parent class like bellow it will no longer have default constructors with empty parameters. 当您在像下面这样的父类中定义了构造函数时,它将不再具有带有空参数的默认构造函数。

public class Vehicle{

    String name;
    int age;

    public Vehicle(String name, int age){
        this.name = name;
        this.age = age;
    }
}

So if you try to create a child class without defining constructor it will look like below. 因此,如果尝试在不定义构造函数的情况下创建子类,则其外观将如下所示。

public Class Car extends Vehicle {
    String type;

    public Car(){
        Super();
    }
}

Now above will give a compilation error because the child class (Car) is trying to call its parent class(Vehicle) constructor with empty parameters. 现在上面将给出编译错误,因为子类(Car)试图使用空参数调用其父类(Vehicle)构造函数。 So to resolve this issue you have two options. 因此,要解决此问题,您有两种选择。

Option 1: Add empty parameter constructor to the parent class. 选项1:将空参数构造函数添加到父类。

public class Vehicle{

    String name;
    int age;

    public Vehicle(){
    }

    public Vehicle(String name, int age){
        this.name = name;
        this.age = age;
    }   
}

Option 2: Define a constructor in child class which calls the parent class constructor. 选项2:在子类中定义一个构造函数,该构造函数调用父类的构造函数。

public Class Car extends Vehicle {
    String type;

    public Car(){
        String defaultName = "";
        int defaultAge = 0;
        Super(defaultName, defaultAge);
    }
}

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

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