不能引用非静态变量

时间:2019-06-17 11:53:42

标签: java

我无法创建Dog对象,我应该能够 我不理解编译错误,这是非静态变量,无法从静态上下文中引用

ALTER TEXT SEARCH CONFIGURATION public.russian_extended
        ALTER MAPPING
            FOR asciiword, asciihword, hword_asciipart, hword, hword_part, word
            WITH russian_extended;

2 个答案:

答案 0 :(得分:1)

您需要将内部类移至Program类之外,否则必须使用new Program().new Dog();实例化它们

public class Program {

   public static void main(String[] args) {
      Dog d = new Dog();
      d.pet();
   }
}

abstract class Animal{
    private String name;
    String latin;
}

class Dog extends Carnivore implements Petable{
    public void pet() {
       System.out.println("bark");
    }
}

abstract class Carnivore extends Animal{}
abstract class Herbivore extends Animal{}

interface Petable{
    public void pet();
}

感谢@ Blokje5和@MC Emperor提供引用和嵌套实例类实例化语法,请对其进行投票。

答案 1 :(得分:0)

您的嵌套类不是静态的,它们仅属于该类的成员。要解决此问题,请将import cv2 height, width = input_image.shape[:2] # for horizontal line horizontal_left = (0, int(height / 2)) horizontal_right = (int(width), int(height / 2)) cv2.line(input_image, horizontal_left, horizontal_right, (0, 255, 0), 1) # for vertical vertical_top = (int(width / 2), 0) vertical_bottom = (int(width / 2), int(height)) cv2.line(input_image, vertical_top, vertical_bottom, (0, 255, 0), 1) 添加到类声明中。

watch: {
data(newVal) {
  this.columns = newVal.columns;
  this.rows = newVal.rows;
},

更改为

static

现在,您可以使用它们而无需初始化类的成员。