我有以下代码:

public class UserRepository
{
    private MyDataSource myDataSource = new MyDataSource();

    public static User CreateUser( int id, String firstName, String lastName )
    {
         myDataSource.propertyOfThis...
         // myDataSource is not accessible and yet i have declared it as a property of UserRespository?
    }
...


我在这里想念什么?

最佳答案

myDataSource不是static

private MyDataSource myDataSource = new MyDataSource();


但是CreateUserstatic

public static User CreateUser( int id, String firstName, String lastName )


因此,使myDataSource static或删除CreateUserstatic修饰符。

哦,还是new UserRepository()

关于java - 从类方法中访问类属性-Java,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17426434/

10-08 20:35