简体   繁体   中英

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.

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.

        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

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 . 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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