问题
I have three java files I'm working with and using Junit.
The test class is where I'm getting my error in regard to an enum that is in the main class.
EDIT
I found out that the this was working as is. Just not in a larger scale implementation.
Goods.java
class Good {
private static StaticTest.THESES name;
static void setStatusName(StaticTest.THESES status) {
name = status;
}
static StaticTest.THESES getStatusName() {
return name;
}
}
Test.class
import org.junit.Test;
import static org.junit.Assert.*;
public class Tests {
@Test
public void test() {
Good good = new Good();
good.setStatusName(Library.STATUSES.HIM);
String actual = good.getStatusName().toString();
String expected = Library.STATUSES.HIM.toString();
assertEquals(expected, actual);
}
public static void main(String args[]) {
Tests runningTest = new Tests();
runningTest.test();
}
}
class Library {
public static enum STATUSES {
YOU, ME, HER, HIM, THEM, US
}
}
can you tell me what I can't find any values from my products class?
回答1:
You need to get a better grasp of what it means to declare something static
. I think each Good
will need to have its own status right - so you are on the right track by not declaring your private status
as static
. Your problem (as Boris pointed out) is that you are trying to mutate the instance variable status
by using static
(class level) methods.
I think you will want to use something like the below to start you off.
public class Test {
public static void main(String[] args) {
Good good = new Good();
good.setStatus(Library.STATUS.SALE);
System.out.println("Good's status: " + good.getStatus());
}
}
class Good {
private Library.STATUS status;
public void setStatus(Library.STATUS status) {
this.status = status;
}
public Library.STATUS getStatus() {
return status;
}
}
class Library {
public enum STATUS {
SALE, NOSALEITEM, ITEMOOS, SHIPPING, ONORDER, INSTOCK
}
}
来源:https://stackoverflow.com/questions/39134283/enum-static-variable-reference-between-java