当使用下面的匿名类时,我们调用的变量x没有问题
interface Age {
int x = 21;
void getAge();
}
class AnonymousDemo {
public static void main(String[] args) {
Age oj1 = new Age() {
@Override
public void getAge() {
// printing age
System.out.print("Age is "+x);
}
};
oj1.getAge();
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用下面的 lambda 表达式相同的代码时,出现了异常:
interface Age {
int x = 21;
void getAge();
}
class AnonymousDemo {
public static void main(String[] args) {
Age oj1 = () -> { System.out.print("Age is "+x); };
oj1.getAge();
}
}
Run Code Online (Sandbox Code Playgroud)
这里会出现什么问题呢?知道lambda表达式只是实现匿名类的缩写。
有没有办法像在 Java 中一样覆盖 Scala 3 枚举中的方法?
public enum Test {
ONE {
@Override
public int calc() {
return 1;
}
},
TWO {
@Override
public int calc() {
return 2;
}
};
public abstract int calc();
}
Run Code Online (Sandbox Code Playgroud)
我试过这样的事情,但没有结果。在文档中也没有找到关于枚举方法覆盖的任何内容。
enum Test {
def calc(): Int ={
0
}
case One
override def calc(): Int ={
1
}
case Two
override def calc(): Int ={
2
}
}
Run Code Online (Sandbox Code Playgroud)
也许有另一种方法可以实现类似的功能?
根据示例中的“well ground rubyist”,c“包含”或“指向”一个匿名类。我对吗,C 不是匿名类?这确实有点令人困惑。
>> c = Class.new
=> #<Class:0x0000000006c4bd40>
>> o1 = c.new
=> #<#<Class:0x0000000006c4bd40>:0x0000000006b81ec8>
>> o1.class
=> #<Class:0x0000000006c4bd40>
>>
>> C = Class.new
=> C
>> o2 = C.new
=> #<C:0x0000000006494480>
>> o2.class
=> C
Run Code Online (Sandbox Code Playgroud) 静态块和静态变量在java中的匿名内部类中工作吗?
你好,我正在关注 YouTube 上的一个视频解释,它正在谈论 thestatic blocks和 the static variables,并且你不能在anonymous inner classes
代码
package Anonymous;
public class main {
public static class TestClass {
public void TestMethod() {
System.out.println("test");
}
}
public static void main(String[] args) {
TestClass test = new TestClass() {
static int p = 5;
final static int y = 5;
static {
}
@Override
public void TestMethod() {
System.out.println("test 2");
}
};
test.TestMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
现在当我在我的计算机上运行时(不会给出错误)
但是在YouTube视频或在线编译器中(它给出了这个错误)
那么造成这种差异的原因是什么?
是否在室内static blocks工作?static …
例如,
int myResult= (new UnmanagedResourceUsingMemorySuckingPig()).GetThingsDone(id);
Run Code Online (Sandbox Code Playgroud)
没有使用块,没有明显的方法来使用using块,没有明显的方法来调用Dispose().当然,UnmanagedResourceUsingMemorySuckingPig确实实现了IDisposable.
在Common Lisp中,是否有一种白话用于以Java的方式定义匿名类,用于一次性"小接口实现者"?
例如,
this.addListener(new Listener() {
public void listen() {...}
});
Run Code Online (Sandbox Code Playgroud)
如:
(defgeneric listen (object))
(add-listener #<this>
(make-anonymous-instance
(listen (object) ...)))
Run Code Online (Sandbox Code Playgroud) 我有一个类Action,它有一个叫做doAction()的方法.此方法仅用于覆盖我添加到名为allActions的操作列表的每个新操作.
public void doAction() {
System.out.println("Overload this method.");
}
Run Code Online (Sandbox Code Playgroud)
操作列表存储在以下列表中:
public static List<Action> allActions = new ArrayList<Action>();
Run Code Online (Sandbox Code Playgroud)
并通过以下调用添加到此列表中:
allActions.add(
new Action(id){
public void doAction(Word w1, Word w2) {
//perform some action
}
}
);
Run Code Online (Sandbox Code Playgroud)
但是这段代码对我不起作用.当我尝试从allActions列表中访问覆盖的doAction()方法时,它只执行默认方法.
我怀疑问题是allActions是一个Action对象列表,所以当它被添加到列表中时,类型可能会从匿名类更改,并恢复为默认方法.
有关如何维护Action类的不同匿名版本列表的任何想法?我希望能够根据赋予操作的id调用这些不同的doAction()方法.
顺便说一下,这是我第一次遇到匿名课程.我想我理解这个概念,但实际上使用它们是另一个故事.
我正在为Java中的View分配一个OnClick监听器,如下所示:
public class MenuButton extends Fragment {
MenuButton self = this;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
MenuButton button = self;
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望获得对菜单按钮的引用,并将其用于我的onTouch方法中的内容(与我认为的问题无关).我知道我可以调用类的方法(例如getApplication())但我特别想要在匿名OnTouchListener中声明对象的引用.
正如你所看到的,我找到了一个解决方案!MenuButton self = this; 线.
有没有一种正确的方法可以做到这一点,还是我的奇怪解决方案唯一的方法呢?
与普通类相比,匿名类中是否有任何特殊功能,因为我在这些类中没有看到任何特殊内容?
我想知道是否可以在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) anonymous-class ×10
java ×6
lambda ×2
android ×1
c# ×1
class ×1
common-lisp ×1
enums ×1
idisposable ×1
oop ×1
overloading ×1
overriding ×1
ruby ×1
scala ×1
scala-3 ×1
static ×1