简体   繁体   English

Java-为什么此初始化无效?

[英]Java - Why does this initializing not work?

This is my code 这是我的代码

class TestResepter {
    public static void main(String[] args) {
        Legemiddel legemiddel = new Legemiddel("Ibuton", 200, 30.5);
        Lege lege = new Lege("Petter");
        Militærresepter militærresepter = new Militærresepter();
        Presepter presepter = new Presepter();
        BlaaResepter blaaresepter = new BlaaResepter();
        Resept resept=new Resept(legemiddel, lege, 650, 21);
    }
}

Legemiddel , Lege , Militærresepter , Presepter , BlaaResepter and Resept are other classes . LegemiddelLegeMilitærresepterPresepterBlaaResepterResept其他classes However, I get this error: 但是,我收到此错误:

testresepter.java:3: error: constructor Legemiddel in class Legemiddel cannot be applied to given types;
    Legemiddel legemiddel=new Legemiddel("Ibuton", 200, 30.5);
                          ^
  required: no arguments
  found: String,int,double
  reason: actual and formal argument lists differ in length
testresepter.java:4: error: constructor Lege in class Lege cannot be applied to given types;
    Lege lege=new Lege("Petter");
              ^
  required: no arguments
  found: String
  reason: actual and formal argument lists differ in length
testresepter.java:8: error: constructor Resept in class Resept cannot be applied to given types;
    Resept resept=new Resept(legemiddel, lege, 650, 21);
                  ^
  required: no arguments
  found: Legemiddel,Lege,int,int
  reason: actual and formal argument lists differ in length
3 errors

Why is that? 这是为什么? What do I have to do to make it work? 我必须怎么做才能使其正常工作?

This is the class Legemiddel 这是类Legemiddel

class Legemiddel {
    static int Id=-1;
    static String navnet;
    static double prisen;
    static double virkestoffet;

    public static void main(String navn, double pris, double virkestoff) {
        Id++;
        navnet = navn;
        prisen = pris;
        virkestoffet=virkestoff;
    }
}

As of now, your class Legemiddel doesn't have a constructor that takes 3 arguments. 到目前为止,您的LegemiddelLegemiddel采用3个参数的构造函数。 I think you're confusing how to create a constructor and how to write a main method. 我认为您在混淆如何创建构造函数以及如何编写main方法。

You're calling new Legemiddel("Ibuton", 200, 30.5); 您正在呼叫new Legemiddel("Ibuton", 200, 30.5); , so your class should have a constructor that looks like this: ,因此您的类应具有一个如下所示的构造函数:

public Legemiddel(String navn, double pris, double virkestoff) {
    ...
}

Also, another potential source of error is, that you probably want to have instance variables instead of all static class variables . 另外,另一个潜在的错误源是,您可能希望拥有实例变量而不是所有static 类变量 Otherwise, when you're creating multiple instances of Legemiddel , it would change all values of all previous created instances. 否则,当您创建Legemiddel多个实例时,它将更改以前创建的所有实例的所有值。

For example: 例如:

Legemiddel l1 = new Legemiddel("Test", 10, 20);
Legemiddel l2 = new Legemiddel("Oops", 0, 0);

System.out.println(l1.Id); // Would print 1 instead of 0
System.out.println(l1.navnet); // Would print Oops instead of Test
System.out.println(l1.prisen); // Would print 0.0 instead of 10.0
System.out.println(l1.virkestoffet); // Would print 0.0 instead of 20.0

So your class should instead look like 所以您的课程应该看起来像

class Legemiddel {
    private static int ID_GEN = -1;

    private final int id;
    private final String navnet;
    private final double prisen;
    private final double virkestoffet;

    public Legemiddel(String navn, double pris, double virkestoff) {
        id = ++ID_GEN;
        navnet = navn;
        prisen = pris;
        virkestoffet = virkestoff;
    }

    public int getId() {
        return id;
    }
    public String getNavnet() {
        return navnet;
    }
    public double getPrisen() {
        return prisen;
    }
    public double getVirkestoffet() {
        return virkestoffet;
    }
}

ID_GEN is now a class variable , which is "shared" among all instances of Legemiddel . 现在, ID_GEN是一个类变量 ,在Legemiddel所有实例之间“共享”。 In this case it is desirable, because you want to have a common generator for each instance that retains its state between different instantiations. 在这种情况下,这是理想的,因为您希望每个实例都有一个公共生成器,以在不同实例之间保留其状态。 If you want each instance to have its own id though, you need a separate instance variable id , that is not shared, but unique to each instance. 如果您希望每个实例都有自己的ID,则需要一个单独的实例变量 id ,该变量不共享,但对于每个实例都是唯一的。

All other fields should be instance variables (no static ), ie they belong to one and only one instance. 所有其他字段应为实例变量 (非static ),即它们属于一个且仅一个实例。 If they remain static you'll run into trouble when creating multiple instances of Legemiddel , because they would share the same values of navnet , prisen and virkestoffet across all instances like I've shown in the code before. 如果它们保持static ,则在创建多个Legemiddel实例时会遇到麻烦,因为它们将在所有实例之间共享相同的navnetprisenvirkestoffet值,就像我之前在代码中所示。

Now, with the changed class, you don't have this kind of behaviour anymore: 现在,随着类的更改,您不再有这种行为:

Legemiddel l1 = new Legemiddel("Test", 10, 20);
Legemiddel l2 = new Legemiddel("Oops", 0, 0);

System.out.println(l1.getId()); // Prints 0
System.out.println(l1.getNavnet()); // Prints "Test"
System.out.println(l1.getPrisen()); // Prints 10.0
System.out.println(l1.getVirkestoffet()); // Prints 20.0

Here you can read more about class members. 在这里,您可以阅读有关班级成员的更多信息。

尝试使用给定的3个参数实现Legemiddel构造函数:)

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

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