Java:从不相关的类访问公共成员?

发布于 2024-11-04 06:39:25 字数 542 浏览 1 评论 0原文

以下 Java 代码是我需要的代码的精简示例。我的问题是,如何从 Second 类内部访问 someInt ?请注意,Second 实现了另一个类,因此我不能只传递 someInt 进去。

package test1;

public class First {
    public int someInt;

    public static void main(String[] args) {
        First x = new First();
    }

    public First(){
        someInt = 9;
        Second y = new Second();
    }
}


class Second implements xyz{
    public Second(){}

    public void doSomething(){
        someInt = 10; // On this line, the question lies.
        System.out.println(someInt);
    }
}

The following Java code is a stripped down example of the code I need. My question is, how do I access someInt from inside class Second? Note that Second implements another class, so I can not just pass someInt in.

package test1;

public class First {
    public int someInt;

    public static void main(String[] args) {
        First x = new First();
    }

    public First(){
        someInt = 9;
        Second y = new Second();
    }
}


class Second implements xyz{
    public Second(){}

    public void doSomething(){
        someInt = 10; // On this line, the question lies.
        System.out.println(someInt);
    }
}

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

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

发布评论

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

评论(6

私野 2024-11-11 06:39:25

您无法访问 SecondFirstsomeInt 字段,因为 Second 不是 Second 的内部类代码>首先。以下更改将解决您的问题。

package test1;

public class First {
    public int someInt;

    public static void main(String[] args) {
        First x = new First();
    }

    public First(){
        someInt = 9;
        Second y = new Second();
    }

    class Second {
        public Second(){
            someInt = 10;
            System.out.println(someInt);
        }
    }
}

You can't access First's someInt field in Second because Second isn't an inner class of First. The changes below would fix your problem.

package test1;

public class First {
    public int someInt;

    public static void main(String[] args) {
        First x = new First();
    }

    public First(){
        someInt = 9;
        Second y = new Second();
    }

    class Second {
        public Second(){
            someInt = 10;
            System.out.println(someInt);
        }
    }
}
你爱我像她 2024-11-11 06:39:25

如果您需要访问 First 中的字段(而不是在 Second 中创建新字段),则需要传递对 First 实例的引用code> 当您创建 Second 的实例时。

        Second y = new Second(this);
    }
}


class Second {
    public Second(First f){
        f.someInt = 10;

根据您的问题“从不相关的类访问公共成员”,问题是通过创建关系来解决的。如果不允许,这个答案就是错误的。

If you need to access the field in First (and not create a new one in Second), you need to pass a reference to the instance of First when you create the instance of Second.

        Second y = new Second(this);
    }
}


class Second {
    public Second(First f){
        f.someInt = 10;

In the terms of your question, "Access public member from a non-related class", the problem is solved by creating a relation. If that isn't allowed, this answer is wrong.

苏别ゝ 2024-11-11 06:39:25

访问公共成员遵循与访问公共方法相同的语法规则(只是没有括号)

但是在类中拥有公共成员通常不是一个好主意

Accessing a public member follows the same syntax rules as accessing a public method (just without the brackets)

But having a public member in a class is usually not a good idea

心的憧憬 2024-11-11 06:39:25

最直接的方法是

1) 实例化 First

First f = new First()

2) 直接访问它,因为您将实例变量 someInt 设为 public

f.someInt = 10

更好的方法是为 First 中的 someInt 提供访问器,并以这种方式执行。

First f = new First();
f.setSomeInt( 10 );
...
int x = f.getSomeInt();

The most direct way would be to

1) instantiate First

First f = new First()

2) access it directly because you made the instance variable someInt public

f.someInt = 10

A better way would be to provide accessors for someInt in First, and do it that way.

First f = new First();
f.setSomeInt( 10 );
...
int x = f.getSomeInt();
漫雪独思 2024-11-11 06:39:25

在你的第二类中,你必须有一个第一类对象。在第二个类中创建该对象,然后您将能够访问 someInt。

Within your second class, you must have a first class object. Create that object in your second class, then you will be able to access someInt.

讽刺将军 2024-11-11 06:39:25

您需要获取对 First 的实例的引用,因为 someInt 不是静态的。

You need to get a reference to an instance of First since someInt is not static.

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