它是否可以注入一个匿名类?我有以下错误:
java.lang.IllegalArgumentException:没有为members/com.acme.MyFragment $ 1注册注入.您必须将其显式添加到其中一个模块的"注入"选项中.
例:
public class MyFragment extends Fragment {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new MyTrask(getActivity()) {
protected void onPostExecute(String result) {
// Stuff
}
}.execute();
}
}
public class MyTask extends AsyncTask<Void, Void, String> {
@Inject
UserApi userApi;
public MyTask(Context context) {
App.getInstance().inject(this);
}
@Override
protected String doInBackground(Void... params) {
return "Hello World!";
}
}
Run Code Online (Sandbox Code Playgroud) (Java问题)
如果我引用内部类中的字段,这是否会导致封闭类和内部类之间的循环依赖?
我怎么能避免这个?
这是一个例子:
public class Outer {
private Other o;
private Inner i;
public Outer() {
o = new Other();
i = new Inner() {
public void doSomething() {
o.foo();
}
};
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个适配器,我加载我的用户和一个按钮/ textfiled发送邀请.在适配器中我这样做:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
UserAgenda ua = getItem(position);
ViewHolder holder;
if (convertView == null) {
//...
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(ua.getInvited() != null){
if(ua.getInvited().equals("true")){
addTextViewInvited(convertView);//add a textview as the user is invited
}
else if(ua.getInvited().equals("false")){
addButton2Invite(convertView,ua);//add the button to invite
}
}
return convertView;
}
Run Code Online (Sandbox Code Playgroud)
在addButton2Invite的方法上,我发送邀请为:
private void addButton2Invite(final View convertView, UserAgenda ua) {
bt2Invited.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
final Button bt2Invited …
Run Code Online (Sandbox Code Playgroud) 我有一个界面
public interface Foo<T> {
public void bar(String s, T t);
}
Run Code Online (Sandbox Code Playgroud)
我想写一个方法
public void baz() {
String hi = "Hello";
String bye = "Bye";
Foo<String> foo = new Foo() {
public void bar(String s, String t) {
System.out.println(s);
System.out.println(s);
}
};
foo.bar(hi,bye);
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误
<anonymous Test$1> is not abstract and does not override abstract method bar(String,Object) in Foo
Foo<String> foo = new Foo() {
Run Code Online (Sandbox Code Playgroud)
我对Java很新,我确信这是一个简单的错误.我怎么写这个?
如何instance variables
从匿名类的方法中访问?
class Tester extends JFrame {
private JButton button;
private JLabel label;
//..some more
public Tester() {
function(); // CALL FUNCTION
}
public void function() {
Runnable r = new Runnable() {
@Override
public void run() {
// How do I access button and label from here ?
}
};
new Thread(r).start();
}
}
Run Code Online (Sandbox Code Playgroud) 我去搜索学习如何在Java中使用lambda表达式,但对我来说却出现了混乱.所以我对匿名类的理解是这样的:
public class SomeObject {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add(new SomeObject());
}
}
Run Code Online (Sandbox Code Playgroud)
我之前看过匿名内部类这个术语,但那个时候,我不知道普通的匿名类是什么.我看到很多线程和视频似乎只是将匿名内部类称为"匿名类".他们是同义词吗?我对匿名内部类的理解是:
public class Rectangle {
private double length;
private double width;
private double perimeter;
public void calculatePerimeter() {
perimeter = (2*length) +(2*width);
}
public static void main(String[] args) {
Rectangle square = new Rectangle() {
public void calculatePerimeter() {
perimeter = 4*length;
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,不是必须为Square编写子类,然后重写calculatePerimeter()方法,我只能创建一次性方形类,并覆盖它们的方法.它是否正确?
因此,匿名内部类与继承有关.我不理解它的使用.也许,这是因为我之前从未使用过它们,或者因为我没有太多的编程经验.你可以给我一些例子或解释它什么时候有用吗?
更新:当我将匿名内部类的代码移动到IDE时,我了解到存在错误; 显然,"正方形"甚至没有继承矩形的字段.这不是没有用吗?
相当于:
public class Rectangle {
private double length;
private double …
Run Code Online (Sandbox Code Playgroud) 我正在使用Intellij IDEA学习JavaFX.编译以下代码时:
public class Main extends Application implements EventHandler<ActionEvent>{
//More code
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World");
}
//More code
});
Run Code Online (Sandbox Code Playgroud)
我收到错误消息"Class must either be declared abstract or implement abstract method"
.但通过观察代码,我显然是使用匿名内部类实现功能接口.
当我handle
在Main
类中构造一个空方法时,代码工作正常,但我不相信我应该这样做.到底是怎么回事!
如何呈现匿名类的关联组成在UML 2中的类本身中定义?
谢谢
我正在使用Eclipse,并且我使用SWT编写了一个Java应用程序.当Eclipse编译我的程序时,它将我的主文件重命名为4个不同的文件,如下所示:
当我从命令行运行这个程序时,我得到了
找不到主类:MainFile.class.程序将会退出.
我真的不明白为什么会这样.
我理解这段代码不合法:
class Popcorn {
public void pop() {
System.out.println("popcorn");
}
}
class Food {
Popcorn p = new Popcorn() {
public void sizzle() {
System.out.println("anonymous sizzling popcorn");
}
public void pop() {
System.out.println("anonymous popcorn");
}
};
public void popIt() {
p.pop(); // OK, Popcorn has a pop() method
p.sizzle(); // Not Legal! Popcorn does not have sizzle()
}
}
Run Code Online (Sandbox Code Playgroud)
那么什么是一种调用嘶嘶声方法的方法呢?