【问题标题】:Static method in java, can I access in a non-static way to a static method?java中的静态方法,我可以以非静态方式访问静态方法吗?
【发布时间】:2014-09-01 11:26:55
【问题描述】:

我认为我的代码有问题,以静态方式或非静态方式调用(在setIstance 中)方法isSameMonth() 是否相同? 编译器建议我更改:timesheet.isSameMonth()Timesheet.isSameMonth()

我认为不是,因为我想传递 local 变量timesheet,是一样的东西还是我应该修改我的代码?

时间表类:

static private Timesheet timesheet;

static public Timesheet getIstance()
{
    if (timesheet == null || !Timesheet.isSameMonth())
    {
        timesheet = null;
        timesheet = new Timesheet();
    }
    return timesheet;
}

static public void setIstance(Timesheet timesheet)
{
    if (timesheet != null && timesheet.isSameMonth())
    {
        Timesheet.timesheet = timesheet;
    }
}

public static boolean isSameMonth()
{
    Date now = new Date();
    Calendar calendarNow = Calendar.getInstance();
    calendarNow.setTime( now );
    Date firstDay = timesheet.days[0];
    Calendar calendarFirstDay = Calendar.getInstance();
    calendarFirstDay.setTime( firstDay );
    if (calendarNow.get(Calendar.MONTH) == calendarFirstDay.get(Calendar.MONTH))
    {
        return true;
    }
    return false;
}

我在外面打这个电话:

Gson gson = new Gson();
String json = sharedPrefs.getString("timesheet", "");
if (!json.isEmpty())
{
    try
    {
        Timesheet timesheet = Timesheet.getIstance();
        if (timesheet.getDay(0)==null)
        {
            Timesheet.setIstance( gson.fromJson(json, Timesheet.class) );
        }
        refreshWidget(timesheet, context, allWidgetIds, intent.getAction());
    }
    catch (Exception e)
    {
        Log.e(TAG_LOG, e.toString());
    }
}

【问题讨论】:

  • 不是一个答案,而是 - 你问这个问题的事实应该让你思考设计。如果每次使用都与保留的实例相关联,我看不出为什么 isSameMonth 应该是静态方法。
  • isSameMonth 是静态的,因为我在 getIstance 中将其称为静态方法。但你说得对,我可以将其更改为: if (timesheet == null || !timesheet.isSameMonth()) 并将 isSameMonth 设置为非静态。你的回复是最好的答案,谢谢:)
  • 如果你的回复是一个答案,我会接受它
  • 如果你有这样的感觉 - 完成了。

标签: java static static-methods


【解决方案1】:

无论哪种方式都可以,但惯例是通过类名引用静态方法:

Timesheet.isSameMonth()

这让阅读您的代码的人清楚您正在调用静态方法。

【讨论】:

    【解决方案2】:

    您提出这个问题的事实应该让您考虑设计。如果每次使用都与保留的实例相关联,我看不出为什么 isSameMonth 应该是静态方法。

    严格来说不是对主题中问题的回答,但显然它有所帮助

    【讨论】:

      【解决方案3】:

      没有。除了方法调用样式Timesheet.isSameMonth()之外,无需更改您的代码。

      因为是静态方法,所以方便调用方法的方式是类名,而不是实例变量。

      否则,阅读您的代码的人可能会认为它是一个实例方法。这就是为什么 IDE 建议让每个人的生活更轻松。

      【讨论】:

        【解决方案4】:

        timesheet 不是局部变量,它是类的静态字段。您的 IDE 建议您将 timesheet.isSameMonth() 更改为 Timesheet.isSameMonth(),因为该方法是静态的,最好以这种(静态)方式访问它。

        如果timesheet 不是静态的,您将收到另一个编译错误,指出静态isSameMonth() 无法访问非静态变量。

        访问静态方法有两种方式:通过类名和通过实例引用,但将生成相同的代码。即使引用是null,您甚至可以访问静态方法:

        Runtime r = null;
        r.getRuntime(); // This works even though r is null
        

        【讨论】:

        • 但是如果我调用 Timesheet.isSameMonth() 我是否使用传递给 Timesheet.setIstance( gson.fromJson(json, Timesheet.class) ) 的 Gson 对象; ?
        • 编辑了我的答案。 timesheet.isSameMonth()Timesheet.isSameMonth() 在生成的字节码中是相同的。
        猜你喜欢
        • 1970-01-01
        • 2016-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-25
        • 2018-05-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多