如何在静态类中初始化最终静态变量?

发布于 2024-12-18 20:46:14 字数 1012 浏览 1 评论 0原文

所以我使用处理从数据文件中绘制地图。我想存储类中第一行的一些信息。在处理这个类时,它是 PApplet 的内部类,因此它必须是静态类(否则我会得到一个错误:静态字段只能在静态或顶级类型中声明)。

public static class PlacesInfo{
    static final int totalCount;
    static final float xMin;
    static final float yMin;
    static final float xMax;
    static final float yMax;
    static final int populationMax;
    static final int densityMax;


    //Set all static variables once
    static{
      String[] lines =  loadStrings("population.tsv");
      String info = lines[0].substring(2); //to delete some useless char
      String[] infoInit = split(info, ','); //to parse the data of the first line

      totalCount = int(infoInit[0]);
      xMin = float(infoInit[1]);
      xMax = float(infoInit[2]);
      yMin = float(infoInit[3]);
      yMax = float(infoInit[4]);
      populationMax = int(infoInit[6]);
      densityMax = int(infoInit[8]); 
    }
}

当我运行此代码时,出现错误,因为我无法使用 loadStrings() 函数(它是非静态的)。

所以我想要的是拥有可以从“population.tsv”文件初始化的静态最终变量。您有什么想法/建议?

So I'm using Processing to draw a map from a data file. I want to stock some information of the first line inside a class. In processing this class is an inner class of PApplet, so it has to be a static class (Otherwise i get an error: static fieldscan only be declared in static or top level type).

public static class PlacesInfo{
    static final int totalCount;
    static final float xMin;
    static final float yMin;
    static final float xMax;
    static final float yMax;
    static final int populationMax;
    static final int densityMax;


    //Set all static variables once
    static{
      String[] lines =  loadStrings("population.tsv");
      String info = lines[0].substring(2); //to delete some useless char
      String[] infoInit = split(info, ','); //to parse the data of the first line

      totalCount = int(infoInit[0]);
      xMin = float(infoInit[1]);
      xMax = float(infoInit[2]);
      yMin = float(infoInit[3]);
      yMax = float(infoInit[4]);
      populationMax = int(infoInit[6]);
      densityMax = int(infoInit[8]); 
    }
}

When I run this code, I get an error, because I can't use the loadStrings() function (which is non-static).

So what I want is to have static final variables that I can initialize from the "population.tsv" file. What are your ideas/advices?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

送君千里 2024-12-25 20:46:14

1)将方法设为静态,这样就可以了——静态代码必须按顺序必须按顺序编译。将 loadStrings 函数放在静态代码块之前。

请注意:但是 - 您最好简单地创建一个静态的“init”方法,该方法在静态代码块中调用。与您当前的实现不同,这将是可命名和可单元测试的。

2)顺便说一句:你的浮点语法已关闭,必须正确转换。

  int i = (int) 1.4f; 

3) 要初始化静态变量,您可以执行以下操作:

  • 声明一个静态 init() 方法,该方法读取文件并初始化静态变量。
  • 在单独的类或静态内部类中创建一个单独的 FileReader 对象,可以调用该对象来读取变量,并从静态代码块中调用它。
  • (坏主意)将文件读取逻辑放在大静态代码块中。但这会非常难看。

1) Make the method static, and you will be fine -- static code must be in order must be compiled in order. Put the loadStrings function before the static code block.

Please note : However - you might be better off simply creating a single, static , "init" method, which is called in your static code block . This will be nameable and unit-testable, unlike your current implementation.

2) By the way : your float syntax is off, and must to be casted properly.

  int i = (int) 1.4f; 

3) To initialize the static variables you can do the following :

  • Declare a static init() method, which reads the file and initializes the static variables.
  • Create a separate FileReader object in a separate class, or a static inner class, which can be invoked to read in variables , and invoke it FROM your static code block.
  • (bad idea) Put the file reading logic inside of your big static code block . this will be very ugly however.
你在看孤独的风景 2024-12-25 20:46:14

您无法在静态上下文中运行类方法loadString。为了从此上下文中运行它,您还需要将 loadString 方法设置为静态(或者将其移到静态上下文之外)。

You cannot run class method loadString inside static context. In order to run it from this context, you need to make your loadString method static as well (or alternatively move it outside static context).

半山落雨半山空 2024-12-25 20:46:14

我推测:

String[]lines = ( new PApplet() ) 。 loadStrings("population.tsv");

I am speculating:

String[] lines = ( new PApplet() ) . loadStrings("population.tsv");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文