本文介绍了java值对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,我必须创建一个值对象(在Java中可能称为映射对象),但是我的代码似乎不起作用,这是值对象:

I'm new to Java and I have to create a value object, (maybe it's called mapped object in Java) but my code doesn't seem to work, here is the value object:

package ....;

public class User {
    private int id;
    private int uid;
    private String name;

    public User()
    {
        // do something here
    }
}

然后我分配一个新的值对象,如下所示:

and I assign a new value object like this:

public boolean some_function()
{
    User u = new User();

    return true; // got a breakpoint here
}

因此,如果我注释掉"User u = new User();"我将转到断点,但是如果我像上面那样保持断点,它将停止运行.

So if I comment out "User u = new User();" I will go to the breakpoint but if I keep it like above it will just stop running.

顺便说一句,我将两个文件都保存在同一个文件夹中,因此eclipse不会导入文件,这是正确的还是应该导入?

On a side note, I keep both the files in the same folder so eclipse doesn't import the file, is this correct or should I import it?

一段时间后,我发现必须手动导入文件,我以为我尝试过,但是显然没有.

After some time I found out that I had to import the file manually, I thought I tried that but apparently I didn't.

推荐答案

丹尼斯(Dennis),如果您发布的代码与您正在运行的代码完全相同,则这没有任何意义-"User u = new User( );调用会返回一个新的User对象,而不会出现任何问题,因为您的构造函数为空.

Dennis, if the code as you posted it is the exact code you're running, then this makes no sense -- the "User u = new User();" call would return you a new User object without any issues, since your constructor is empty.

要向自己证明这一点,请将构造函数更改为:

To demonstrate that to yourself, change your constructor to:

public User() {
  System.out.println("I'm inside the User constructor!");
}

,然后再次调用some_function()函数.您应该会看到该行已打印到控制台.

and call your some_function() function again. You should see that line printed out to your console.

鉴于您要报告的内容和所显示的代码,我怀疑包含some_function()的类没有看到" User类-您正在导入其他一些User类,而不是一个您创建的.这两个类(用户类和包含some_function()的类)是否在同一包中?如果不是,那么包含some_function()的类顶部的哪个import语句正在处理User类的导入?

Given what you're reporting and the code you're showing, I suspect that the class that contains some_function() isn't "seeing" the User class -- you're importing some other User class rather than the one you created. Are the two classes -- the User class and the class which contains some_function() -- in the same package? If not, what import statement at the top of the some_function()-containing class is handling the import of your User class?

这篇关于java值对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:59