gpt4 book ai didi

java - 为什么从非静态方法访问静态方法不好?

转载 作者:搜寻专家 更新时间:2023-11-01 00:59:30 25 4
gpt4 key购买 nike

Netbeans 告诉我从非静态方法访问静态方法是不好的。为什么这样不好?“访问静态方法 getInstance”是警告:

import java.util.Calendar;

public class Clock
{
// Instance fields
private Calendar time;

/**
* Constructor. Starts the clock at the current operating system time
*/
public Clock()
{
System.out.println(getSystemTime());
}

private String getSystemTime()
{
return this.time.getInstance().get(Calendar.HOUR)+":"+
this.time.getInstance().get(Calendar.MINUTE);
}

最佳答案

您可能正在从实例而不是直接访问静态方法。尝试使用 Calendar.getInstance() 代替:

private String getSystemTime()
{
return Calendar.getInstance().get(Calendar.HOUR)+":"+
Calendar.getInstance().get(Calendar.MINUTE);
}

关于java - 为什么从非静态方法访问静态方法不好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/405336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com