可以从Java中的参数返回值吗?
-
08-10-2019 - |
题
众所周知,Java始终通过“通过逐个值”调用一种方法。但是我看到了Android的NotificationManager.notify(字符串,int,Notification)的参考:
返回
the id of the notification that is associated with the string
可用于取消通知的标识符
请参考参考:http://developer.android.com/reference/android/app/notificationmanager.html
这怎么可能发生?我有些不理解吗?
BR,亨利
解决方案
看起来像 通知管理器的API参考 有点混乱。
这是通过 Google代码搜索NotificationManager和Android:
/**
* Persistent notification on the status bar,
*
* @param tag An string identifier for this notification unique within your
* application.
* @param notification A {@link Notification} object describing how to
* notify the user, other than the view you're providing. Must not be null.
* @return the id of the notification that is associated with the string identifier that
* can be used to cancel the notification
*/
public void notify(String tag, int id, Notification notification)
{
int[] idOut = new int[1];
INotificationManager service = getService();
String pkg = mContext.getPackageName();
if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
try {
service.enqueueNotificationWithTag(pkg, tag, id, notification, idOut);
if (id != idOut[0]) {
Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
}
} catch (RemoteException e) {
}
}
显然,参数不返回值。他们打算有类似的Javadoc,但可能犯了一个错误。
查看其他变体的代码 notify
:
/**
* Persistent notification on the status bar,
*
* @param id An identifier for this notification unique within your
* application.
* @param notification A {@link Notification} object describing how to
* notify the user, other than the view you're providing. Must not be null.
*/
public void notify(int id, Notification notification)
{
notify(null, id, notification);
}
如您所见,此超载版本仅调用主要实现 tag
字符串值的 null
.
关于按价值传递和参考的总体问题,简单/俗气的解释是:
- Java按价值传递原语,
- 但是通过引用传递对象。
请参阅Arnivan和Patrick的评论以进行澄清。
其他提示
关于这个说法:
“ Java按价值传递原语,但通过引用传递对象。”
这不是 精确的. 。 Java按值通过所有内容,并且根本不会传递对象。
- 对于原始内容:副本传输到方法(别忘了字符串不是原始的) - 这是您所说的正确的
- 对于参考变量:它们也通过值传输: 参考变量 传输到该方法。因此,对象本身永远不会传输。可以在方法中更改对象(通过调用其一些方法),您将在从该方法返回后看到修改(例如,您从一个人对象更改“名称”成员),但是如果您更改了参考在方法之外不会可见更改:
更改引用是由“新”运算符或诸如param = some_other_reference之类的分配(其中some_other_referece指向堆上的其他对象)。更改引用不会影响“原始”参考,而只会影响“复制引用”(方法中使用的引用)。
该文件对我来说似乎是错误的。声明说:
public void notify (String tag, int id, Notification notification)
但是与此同时,它说它返回了东西。
我将其解释为: id
唯一地映射到有关通知,可以在取消通知时使用。
同意以前的海报:DOC似乎是不正确的。该方法需要三个参数,但在Javadoc中仅提及其中两个。只需通过@param替换@return,就可以使用ID参数的描述:
与字符串标识符关联的通知的ID,可用于取消通知
编辑:您可以自己定义此ID,并以后使用它来取消通知。