是否有可能动态声明一个类?是否有可能在C#中使用匿名类创建通用列表?任何代码片段都会有所帮助.谢谢
我注意到了:
class A {
ClassB b = new ClassB() { // anonymous class
/* some expression using this */
}
}
Run Code Online (Sandbox Code Playgroud)
每当我this在匿名类中使用关键字时,都会this引用封闭的外部类/枚举而不是匿名类.
这是否意味着this永远不能代表一个匿名类?只是"正常"的类和枚举?
还可以this或super代表一个界面?
abstract class Two {
Two() {
System.out.println("Two()");
}
Two(String s) {
System.out.println("Two(String");
}
abstract int display();
}
class One {
public Two two(String s) {
return new Two() {
public int display() {
System.out.println("display()");
return 1;
}
};
}
}
class Ajay {
public static void main(String ...strings ){
One one=new One();
Two two=one.two("ajay");
System.out.println(two.display());
}
}
Run Code Online (Sandbox Code Playgroud)
我们无法实例化一个抽象类,那么为什么函数 二二(String s)能够创建一个抽象类的实例Two ????
有人能帮助我理解以下构造吗?我无法理解这是初始化程序还是匿名类.我不熟悉这种语法.
JTable jt = new JTable(data, fields) **{
public TableCellRenderer getCellRenderer(int row, int column) {
// TODO Auto-generated method stub
return renderer;
}
};**
Run Code Online (Sandbox Code Playgroud) 以下面的例子为例.有一个我想要使用的对象,称之为Doodad.Doodad元素处理浏览器事件的能力很差.a的典型实例化Doodad将是Doodad someDoodad = new Doodad();.显然,由于事件处理不当,这不符合我的需要.我是否适合覆盖该onBrowserEvent()方法,如下所示:
Doodad someDoodad = new Doodad() {
@Override
public void onBrowserEvent(Event event) {
switch (DOM.eventGetType(event)) {
case Event.ONDBLCLICK:
case Event.ONFOCUS:
case Event.ONCLICK:
if (!isEnabled()) {
return;
}
break;
}
super.onBrowserEvent(event);
}
};
Run Code Online (Sandbox Code Playgroud)
显然,这是一个简单的例子,但我当可能不希望使用匿名内部类?这是明确禁止还是不可能?
我看到第一个问题有很多答案,但到目前为止,答案都没有回答第二个问题:是否明确禁止或不可能使用匿名内部类?
我有一个简单的GUI,其中包含:
现在我想听听这些按钮中的每一个.我做的是这样的:
public class TestApp implements ActionListener {
private JFrame frame;
private JButton btn;
private JRadioButton rdb1;
private JRadioButton rdb2;
public static void main(String[] args) { /*....*/ }
private void initialize() {
//Each time I add a button, I add it to the listener:
btn = new JButton("Button");
btn.addActionListener(this);
//..
rdb1 = new JRadioButton("Value1");
rdb1.addActionListener(this);
//And so on...
}
//The ActionEvents
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn)
//...
if(e.getSource()==rdb1)
//...
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想知道这是否被视为好/坏风格?
我使用以下类来模仿PHP中的匿名对象:
class AnonymousObject
{
protected $methods = array();
public function __construct(array $options) {
$this->methods = $options;
}
public function __call($name, $arguments) {
$callable = null;
if (array_key_exists($name, $this->methods))
$callable = $this->methods[$name];
elseif(isset($this->$name))
$callable = $this->$name;
if (!is_callable($callable))
throw new BadMethodCallException("Method {$name} does not exist");
return call_user_func_array($callable, $arguments);
}
}
Run Code Online (Sandbox Code Playgroud)
(https://gist.github.com/Mihailoff/3700483)
现在,只要声明的函数代表它们自己一切正常,但每当我尝试从另一个函数调用这样的函数时......
$anonymous = new AnonymousObject(array(
"foo" => function() { $this->bar(); },
"bar" => function() { }
));
Run Code Online (Sandbox Code Playgroud)
然后我当然得到了 Fatal error: Using $this when not in object context
有没有办法解决这个问题?
在无限循环的上下文中有什么更好的:匿名类或嵌套类?正如我所看到的,如果我们使用匿名类,它会为每次迭代重新定义,但是使用嵌套类我们仍然会创建一个实例,但是类本身并没有重新定义.不确定这是否正确......在这种情况下,使用一个与另一个有什么优势吗?
public class MainThread {
public static void main(String[] args){
while (true) {
final int data = rand.nextInt();
//Runnable task = new MyRunnable(data);
Runnable task = new Runnable() {
public void run() {
printData(data);
}
};
new Thread(task).start();
}
}
private static class MyRunnable implements Runnable {
private int data;
MyRunnable(int data){
this.data = data;
}
@Override
public void run() {
printData(data);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我已经看过很多次,但是我有点困惑这是否会被称为匿名类?
public class Test {
public static void main(String[] args) {
new Thread(){
@Override
public void run() {
System.out.println("##");
}
}.start();
}
}
Run Code Online (Sandbox Code Playgroud)
我很困惑的原因是因为匿名类没有名称,但是这显然我们已经在Java API中有一个"Thread"类,所以这意味着它有一个名称,如果它有一个名称,那么它是如何匿名的如果它不是一个匿名类,那么它是什么.
我知道这有点愚蠢,但我无法推理自己的确定性 - 因为我看到双方都有有效的论据.
另外,在上面我可以清楚地覆盖类的run方法Thread,现在如果我创建自己的类,让我们说MyClass,在其中定义一些方法,然后尝试做同样的事情,然后为什么我不能覆盖方法MyClass,他们的方式我能够覆盖类的run方法Thread.
public class MyClass {
private void myMethod1() {
}
private void myMethod2() {
}
}
public class Test {
public static void main(String[] args) {
new MyClass(){
// why I cannot override "myMethod1" and "myMethod1" of `MyClass`, they way …Run Code Online (Sandbox Code Playgroud) 我尝试使用 lambda 更改并获取局部变量。我知道我应该对 lambda 中的局部变量有效地使用final。当我使用 AtomicReference 局部变量更改失败时:
public class Lamb {
public static void main(String[] args) throws InterruptedException {
Lamb lamb = new Lamb();
GlobalL globalL = new GlobalL();
lamb.a(globalL);
for (int i = 0; i < 100; i++) {
new Thread(() -> {
globalL.printSum();
}).start();
}
Thread.sleep(3000);
System.out.println("--------After Work--------");
globalL.printSum();
}
public void a(GlobalL globalL) {
AtomicReference<Integer> number = new AtomicReference<>(0);
Work work = () -> {
number.getAndSet(number.get() + 1);
return number.get();
};
globalL.setWork(work);
} …Run Code Online (Sandbox Code Playgroud) anonymous-class ×10
java ×8
swing ×2
.net ×1
abstract ×1
c# ×1
coding-style ×1
initializer ×1
jtable ×1
jvm ×1
lambda ×1
overriding ×1
php ×1
reflection ×1
super ×1
this ×1