在非静态和静态上下文之间未引用

时间:2018-12-20 16:51:05

标签: this getter-setter

为什么我会从此变量获取错误:

import java.util.*;

class Dcoder
{
    private static String name;

    private static String model;

    private static String color;

    private static int year;

    public static void increaseSpeed()    
    {
        System.out.println("increase the speed");
    }

    public static String getModel()
    {
        return this.model;
    }

    public static void setModel(String model)
    {
        this.model=model;
    }

    public static int getYear()
    {
        return this.year;
    }

    public static void setYear(int year)
    {
        this.year=year;
    }

}

class Car
{
    public static void main(String[] args)
    {
        Dcoder bmw=new Dcoder();
        bmw.setModel("B-5");
        System.out.println(bmw.getModel());
        Dcoder benz=new Dcoder();
        benz.setModel("S-69");
        System.out.println(benz.getModel());        
    }
}

错误消息:

  

输出:source_file.java:16:错误:非静态变量,不能从静态上下文中引用           返回this.model;                  ^   source_file.java:20:错误:非静态变量,不能从静态上下文中引用           this.model = model;           ^   source_file.java:24:错误:非静态变量,不能从静态上下文中引用           返回今年                  ^   source_file.java:28:错误:非静态变量,不能从静态上下文中引用           this.year = year;           ^   4个错误

0 个答案:

没有答案