简体   繁体   English

将带有字符串的数组和int传递给另一个类

[英]Pass array with a string and int to another class

I am new to Java and I have failed to find anything about this case. 我是Java的新手,我没有找到任何关于这个案例的内容。

I am basically trying to pass this array called vakken to a new class called Vak , Vak expects to receive a String and a int. 我基本上是试图通过这种阵列称为vakken到一个名为Vak上升的新类,Vak上升预计将收到一个String和int值。

        Vak[] vakken = new Vak[1];
        vakken[0] = new Vak("Test",3);

        Vak vak = new Vak(vakken[0]);

Whenever I try the code above I get this error. 每当我尝试上面的代码时,我都会收到此错误。

Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at ectsmonitor2.Vak.<init>(Vak.java:24)
at ectsmonitor2.ECTSmonitor2.main(ECTSmonitor2.java:27)
Java Result: 1

Vak.class Vak.class

public class Vak {
    public String naam;        
    public int teVerdienenEcts; 

    public Vak(String vakNaam, int vakTeVerdienenEcts){
        naam  = vakNaam;
        teVerdienenEcts = vakTeVerdienenEcts;
    }
}

You haven't actually coded your constructor that takes a Vak yet, you made it throw UnsupportedOperationException . 你还没有实际编写你的Vak构造函数, 让它抛出UnsupportedOperationException Put some code in the constructor eg 在构造函数中加入一些代码,例如

public Vak(Vak v) {
    this(v.naam, v.teVerdienenEcts);
}

This line wont work for sure 这条线肯定不会起作用

 Vak vak = new Vak(vakken[0]);//IDE will display error message here

Because you have no such constructor for this. 因为你没有这样的构造函数。

Create a new constructor that takes an object of its own type. 创建一个新的构造函数,该构造函数接受自己类型的对象。

Similar to this: 与此类似:

public Vak(Vak anObject){
//do stuffs here
}

These type of constructors are called copy constructors 这些类型的构造函数称为复制构造函数

And generally you won't want your attributes to be public . 通常,您不希望您的属性公开 Make them private . 让它们变得私密

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

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