enum static variable reference between java

风格不统一 提交于 2019-12-25 06:28:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!