我想知道是否可以在JAVA中将属性添加到匿名类吗?maby是一个简单的问题,但是这里的窍门是,我在google上看到了一些这样做的代码,但是当我在Netbeans中尝试时却没有发生。
我以匿名的方式创建了该类的实例,但没有覆盖该方法,而是添加了other和一个变量,并且该实例很简单,在google中,其他帅哥使所有此类都即时生成了,我为什么不能呢?
public class Cat(){
public void sayMew(){}
}
Cat gato = new Cat{
@Override
public void sayMew(){System.out.println("mew");
};
gato.sayMew(); //This works fine.
//Some code over there do this and it didn't work for me:
Cat gato = new Cat(){
int legs = 4;
public void scratch(){System.out.println("scratch");
};
//Even I saw this king of instances be pointed by variables types :/ what
am I doing wrong?
Run Code Online (Sandbox Code Playgroud)