如何从子类访问主类的公共常量?

发布于 2024-10-29 23:52:32 字数 1067 浏览 5 评论 0原文

我有一个带有几个公共常量变量的主类,并且我有一个自定义类,我想知道如何从自定义类访问主类的常量?

主类代码:

import processing.core.*;
import toxi.geom.*;
import toxi.math.*;

public class VoronoiTest extends PApplet {

    // this are the constants I want to access from the Site class
    public static int NUM_SITES         = 8;
    public static int SITE_MAX_VEL      = 2;
    public static int SITE_MARKER_SIZE  = 6;

    Site[] sites;

    public void setup() {
        size( 400, 400 );

        sites = new Site[NUM_SITES];
        for ( int i = 0; i < sites.length; i++) {
            sites[i] = new Site( this );
        }
    }
}

这是站点类代码:

import processing.core.*;


public class Site {
    PApplet parent;

    float x, y;
    PVector vel;

    int c;

    Site ( PApplet p ) {
            parent = p;
            // here I try to get the constants from the main class
            vel = new PVector( parent.random(-parent.SITE_MAX_VEL, SITE_MAX_VEL), parent.random(-SITE_MAX_VEL, SITE_MAX_VEL) );     
    }   
}

任何帮助将不胜感激!

I have a main class with a couple of public constant variables, and I have a custom class, I would like to know how can I access the constants of the main class from the custom class?

Main Class code:

import processing.core.*;
import toxi.geom.*;
import toxi.math.*;

public class VoronoiTest extends PApplet {

    // this are the constants I want to access from the Site class
    public static int NUM_SITES         = 8;
    public static int SITE_MAX_VEL      = 2;
    public static int SITE_MARKER_SIZE  = 6;

    Site[] sites;

    public void setup() {
        size( 400, 400 );

        sites = new Site[NUM_SITES];
        for ( int i = 0; i < sites.length; i++) {
            sites[i] = new Site( this );
        }
    }
}

And this is the Site class code:

import processing.core.*;


public class Site {
    PApplet parent;

    float x, y;
    PVector vel;

    int c;

    Site ( PApplet p ) {
            parent = p;
            // here I try to get the constants from the main class
            vel = new PVector( parent.random(-parent.SITE_MAX_VEL, SITE_MAX_VEL), parent.random(-SITE_MAX_VEL, SITE_MAX_VEL) );     
    }   
}

Any help will be much appreciated!

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

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

发布评论

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

评论(3

相思碎 2024-11-05 23:52:32

你不能。由于 parent 的类型为 PApplet,而不是 VoronoiTest,因此您无法保证它具有静态成员 SITE_MAX_VEL。

相反,如果 parent 的类型为 VoronoiTest,则通过实例访问静态变量就没有什么意义,因为不可能它要改变。

如前所述,要访问静态成员,请使用 ClassName.STATIC_MEMBER 表示法(在本例中为 VoronoiTest.SITE_MAX_VEL)。

更好的是,只需将常量存储在 Site 类中即可。毕竟,这对他们来说似乎是最合乎逻辑的地方。

import processing.core.*;

public class Site {
    public static final int COUNT       = 8;
    public static final int MAX_VEL     = 2;
    public static final int MARKER_SIZE = 6;

    PApplet parent;

    float x, y;
    PVector vel;

    int c;

    Site(PApplet p) {
        parent = p;
        vel = new PVector(
            parent.random(-MAX_VEL, MAX_VEL),
            parent.random(-MAX_VEL, MAX_VEL)
        );     
    }   
}

You can't. Because parent is of type PApplet, not VoronoiTest, you cannot guarantee that it has the static member SITE_MAX_VEL.

Conversely, if parent were of type VoronoiTest, there would be little point in accessing the static variable through the instance, as it would be impossible for it to change.

As already mentioned, to access static members, use the ClassName.STATIC_MEMBER notation (in this case, VoronoiTest.SITE_MAX_VEL).

Better yet though, just store the constants in the Site class instead. After all, that seems the most logical place for them.

import processing.core.*;

public class Site {
    public static final int COUNT       = 8;
    public static final int MAX_VEL     = 2;
    public static final int MARKER_SIZE = 6;

    PApplet parent;

    float x, y;
    PVector vel;

    int c;

    Site(PApplet p) {
        parent = p;
        vel = new PVector(
            parent.random(-MAX_VEL, MAX_VEL),
            parent.random(-MAX_VEL, MAX_VEL)
        );     
    }   
}
若有似无的小暗淡 2024-11-05 23:52:32

使用 VoronoiTest 参考。例如,VoronoiTest.SITE_MAX_VEL。当您使用 PApplet 引用时,编译器没有任何方法知道静态变量是否存在。

Use the VoronoiTest reference. VoronoiTest.SITE_MAX_VEL, for example. When you use a PApplet reference, the compiler doesn't have any way of knowing that the static variables exist.

眼眸里的快感 2024-11-05 23:52:32

静态字段通过类名访问。使用VoronoiTest.SITE_MAX_VEL

Static fields are accessed via the class-name. Use VoronoiTest.SITE_MAX_VEL.

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