java中有关static使用的一个小问题,大家帮帮忙。
static方法不是调用非static方法和属性,但是请大家看看我程序问题处在哪里?
//本程序是对static进行测试
class Beijing
{
//封装属性
private String name;
private int age;
private static String city = "北京"; //公共属性
//构造方法
public Beijing(String name, int age)
{
this.name = name;
this.age = age;
}
public String getInfo()
{
return "姓名:" + this.name + ",年龄:"+ this.age + ",城市:" + this.city;
}
public static void setCity(String c)
{
this.city = c; //static方法不能调用非static属性方法
}
public static String getCity()
{
return city;
}
}
public class TestStatic
{
public static void main(String[] args)
{
System.out.println(Beijing.getCity());
}
}
this.city = c总是提示出问题说static不能调用非static,请问哪里是非static了?我的this,city肯定是static呀?