문제

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);

invokes multiple methods on the anonymous[--correction: it's NOT anonymous--] class. On each line it starts with .<method name>. Where is the official Java documentation on this? I'm hoping there is a page on http://docs.oracle.com/javase/ that explains it.

PS. I just learned this is called "method chaining". So where is the official Java documentation, if any, on this concept?

도움이 되었습니까?

해결책

You ask:

So where is the official Java documentation, if any, on this concept?

If your method returns an object, you can call another method on the returned object, simple as that. All of the methods above return this, and so if method1, method2, and method3 all return this you can do this:

MyClass myVar = new MyClass().method1().method2().method3();

which is the same as:

MyClass myVar = new MyClass();
myVar.method1();
myVar.method2();
myVar.method3();

The documentation for Java method invocation can be found in the JLS Section 15.12.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top