枚举静态变量引用java之间 [英] enum static variable reference between java

查看:237
本文介绍了枚举静态变量引用java之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个java文件,我正在使用并使用Junit。



测试类是我在主类中的枚举的错误。



编辑



我发现这是正常工作。



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测试{

@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 []){
测试runningTest = new Tests();
runTest.test();
}
}

class Library {
public static enum STATUSES {
你,我,她,他,他们,US
}
}

你能告诉我什么我找不到我的产品类的任何值?

解决方案

您需要更好地掌握声明东西的意义 static 。我认为每个将需要自己的身份正确 - 所以你没有声明你的私人状态是正确的轨道 as static 。你的问题(正如鲍里斯指出的)是你试图通过使用 static (类)来变形实例变量 status 级别)方法。



我想你会想使用下面的东西来启动你。

  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好{

private Library.STATUS status;

public void setStatus(Library.STATUS status){
this.status = status;
}
public Library.STATUS getStatus(){
return status;
}
}

class库{

public enum STATUS {
SALE,NOSALEITEM,ITEMOOS,SHIPPING,ONORDER,INSTOCK
}
}


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?

解决方案

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

这篇关于枚举静态变量引用java之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆