我正在尝试编写一个函数,因此我可以将函数作为参数传递,例如
public class HashFunction {
private Function f;
public HashFunction(Function f) {
this.f=f;
}
public Integer hash(String s){
return f(s);
}
}
Run Code Online (Sandbox Code Playgroud)
所以我可以写代码
new HashFunction(function(String s){ return s.charAt(0)+0; });
Run Code Online (Sandbox Code Playgroud)
就像在 javascript 中一样。我怎样才能做到这一点?
Class clazz = new Object(){}.getClass();
Run Code Online (Sandbox Code Playgroud)
为什么这可能,这意味着什么?有人可以提醒我吗?
例如:
public class Testing {
public static void main(String[] args) {
Class clazz = new Object(){}.getClass();
System.out.println(clazz);
}
}
Run Code Online (Sandbox Code Playgroud)
结果是: class Testing$1
这可能是一个愚蠢的问题,但我已经考虑了一段时间,我不知道它真正被称为什么.
因此,对于Android,OnClickListeners,OnTouchListeners等,您可以执行以下操作:
bio.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface arg0) {
// TODO Auto-generated method stub
}
});
Run Code Online (Sandbox Code Playgroud)
几乎是一个新的内联类.这在Java中称为什么,还是Android特定的东西?基本上,当这种情况发生时会发生什么?您是否正在创建一个实现OnCancelListener的内部类,因为On [blah] Listeners是接口?
谢谢!
我知道抽象类无法实例化.但我对下面的代码有疑问.此代码是android位图趣味演示的一部分(http://commondatastorage.googleapis.com/androiddevelopers/shareables/training/BitmapFun.zip).
// ImageWorkerAdapter class is nested in another abstract class ImageWorker
public static abstract class ImageWorkerAdapter
{
public abstract Object getItem(int num);
public abstract int getSize();
}
//this snippet is seen in Images.java
public final static ImageWorkerAdapter imageWorkerUrlsAdapter = new ImageWorkerAdapter() {
@Override
public Object getItem(int num) {
return Images.imageUrls[num];
}
Run Code Online (Sandbox Code Playgroud)
我无法理解如何创建抽象类的实例.请帮我理解这段代码.
这很好用.
package abstracttest;
public abstract class AbstractClass implements NewInter {
public abstract void doStuff();
public void doStuff2() {
System.out.println("in doStuff2");
}
/*
* public static void main(String a[]) { AbstractClass ab = new
* AbstractClass() {
*
* @Override public void doStuff() { // TODO Auto-generated method stub
* System.out.println(" doStuff "); }
*
* @Override public void doInter() { // TODO Auto-generated method stub
*
* } };
*
* ab.doStuff2(); ab.doStuff();
*
* NewInter ni = new NewInter() …Run Code Online (Sandbox Code Playgroud) 作为番茄钟技术的粉丝,我正在制作一个倒数计时器,让我完成我的作业任务.然而,这个特殊的项目不是功课.:)
Stack有很多关于在用户输入之前使用定时器来控制延迟的问题,但在独立的定时器上却没有太多问题.我从朋友那里遇到了这段代码,并且已经学习了Java Documentation的课程.
public class Stopwatch {
static int interval;
static Timer timer;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Input seconds => : ");
String secs = sc.nextLine();
int delay = 1000;
int period = 1000;
timer = new Timer();
interval = Integer.parseInt( secs );
System.out.println(secs);
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
System.out.println(setInterval());
}
}, delay, period);
}
private static final int setInterval()
{
if( interval== 1) timer.cancel();
return --interval;
}
} …Run Code Online (Sandbox Code Playgroud) 这是什么意思?
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String location = GUI.Custom.QuickDialogs.selectFile(false);
try
{
PrintWriter pw = new PrintWriter(new File(location));
String text = textArea.getText();
pw.println(text);
pw.flush();
pw.close();
}
catch(Exception ex)
{
textArea.append("Could not save this debug output");
}
}
});
Run Code Online (Sandbox Code Playgroud)
新的ActionListener(){} {}发生了什么?在对象?类中声明一个方法?是ActionListener内部类?
与普通类相比,匿名类中是否有任何特殊功能,因为我在这些类中没有看到任何特殊内容?
我可以在Web上的几个例子中看到一个新的语法,这是一个例子:
Accumulator<Integer> sum = new Accumulator<Integer>(){
public Integer accumulate(Integer t1, Integer t2) {
return t1+t2;
}
};
Run Code Online (Sandbox Code Playgroud)
一般来说,{ a method }在创建一个类的实例之后写一个是什么意思?是什么东西的某种旧语法?
谢谢
在Java中的main方法中有一个方法在语法上是否正确?例如
class Blastoff {
public static void main(String[] args) {
//countdown method inside main
public static void countdown(int n) {
if (n == 0) {
System.out.println("Blastoff!");
} else {
System.out.println(n);
countdown(n - 1);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)