Notification.Builder notificationBuilder = new Notification.Builder(
getApplicationContext())
.setTicker(tickerText)
.setSmallIcon(android.R.drawable.stat_sys_warning)
.setAutoCancel(true)
.setContentIntent(mContentIntent)
.setSound(soundURI)
.setVibrate(mVibratePattern)
.setContent(mContentView);
在匿名[--更正:它不是匿名--]类上调用多个方法。每行都以 .<method name>
开头。关于这方面的官方 Java 文档在哪里?我希望有一个关于 http://docs.oracle.com/javase/ 的页面这解释了它。
最佳答案
你问:
So where is the official Java documentation, if any, on this concept?
如果您的方法返回一个对象,您可以在返回的对象上调用另一个方法,就这么简单。上面的所有方法都返回 this
,因此如果 method1、method2 和 method3 都返回 this
,您可以这样做:
MyClass myVar = new MyClass().method1().method2().method3();
这与:
相同MyClass myVar = new MyClass();
myVar.method1();
myVar.method2();
myVar.method3();
Java 方法调用的文档可以在 JLS 第 15.12 节中找到。
关于java - Java中调用匿名类的多个方法的语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22132997/