英文:
Does aliasing make any difference on thread safety?
问题
以下是您提供的代码的翻译部分:
以下代码首先通过向下转型来访问一个变量,我想知道如果方法的返回类型与我正在使用的变量(ContainerPieceVolumesBinding binding
)返回相同的别名,或者与作为方法参数传入的变量返回的别名是否有任何区别。
我已经测试过这段代码,似乎工作得很好,问题在于同一个变量可能有第二个访问点(这个第二个访问点是可选的)。
理论上,这个第二个访问点将使用 viewBind
别名,而不是我在这里使用的向下转型版本。
如果我返回向下转型的版本,而不是返回方法参数抛出的相同别名,是否会有任何区别?这样做会更安全吗?
@SuppressWarnings("unchecked")
@Override
public ViewDataBinding setData(ViewDataBinding viewBind, @NonNull String bindingTag) {
BindingVisitorsBaseAdapter adapter = new BindingVisitorsBaseAdapter(
TAG
);
adapter.setCompletePendingBindings(
pendingBindings
);
// 这是向下转型后的别名
ContainerPieceVolumesBinding binding = (ContainerPieceVolumesBinding) viewBind;
binding.pieceVolumesContainer.setAdapter(adapter);
observer = new ObjectNotifier(
) {
@Override
public Notifier onNotified() {
return (response, responseType) -> {
switch (responseType) {
case SUBMIT_LIST :
List<BindingVisitor> pieceVolumes = (List<BindingVisitor>) response[0];
((BindingVisitorsBaseAdapter)binding.pieceVolumesContainer.getAdapter()).submitList(pieceVolumes);
break;
case QUANTITY_CHANGE:
onQuantityChanged.onChanged(getValues(binding.pieceVolumesContainer));
break;
}
};
}
};
observer.notifyResponse(SUBMIT_LIST, new ArrayList<>());
// 在这里返回原始版本
return viewBind;
}
请注意,上述内容是您提供的代码的翻译部分。如果您有任何其他问题或需要进一步的帮助,请随时提问。
英文:
The following code accesses a variable by downcasting it first, and I'm wondering if it makes any difference if the return type of the method returns the same alias as the one I'm working with (ContainerPieceVolumesBinding binding
), or the one that is passed in as a method parameter.
I've tested the code, and it seems to be working well, the problem is that there could be a second access point to the same variable (this second access point is optional).
This second access point, in theory would be working with the viewBind alias and not the downcasted version I'm working with here.
Would it make any difference if instead of returning the same alias thrown by the method parameter, I return the downcasted version, would it be safer?
@SuppressWarnings("unchecked")
@Override
public ViewDataBinding setData(ViewDataBinding viewBind, @NonNull String bindingTag) {
BindingVisitorsBaseAdapter adapter = new BindingVisitorsBaseAdapter(
TAG
);
adapter.setCompletePendingBindings(
pendingBindings
);
//This is the downcasted alias
ContainerPieceVolumesBinding binding = (ContainerPieceVolumesBinding) viewBind;
binding.pieceVolumesContainer.setAdapter(adapter);
observer = new ObjectNotifier(
) {
@Override
public Notifier onNotified() {
return (response, responseType) -> {
switch (responseType) {
case SUBMIT_LIST :
List<BindingVisitor> pieceVolumes = (List<BindingVisitor>) response[0];
((BindingVisitorsBaseAdapter)binding.pieceVolumesContainer.getAdapter()).submitList(pieceVolumes);
break;
case QUANTITY_CHANGE:
onQuantityChanged.onChanged(getValues(binding.pieceVolumesContainer));
break;
}
};
}
};
observer.notifyResponse(SUBMIT_LIST, new ArrayList<>());
// returning the original version here
return viewBind;
}
答案1
得分: 1
除非您通过对象调用引用静态方法,否则向上转型和向下转型不会影响调用的安全性。
public class A {
public void foo() {
System.out.println("A");
}
public static void bar() {
System.out.println("As");
}
}
public class B extends A {
private void foo() {
System.out.println("B");
}
public static void bar() {
System.out.println("Bs");
}
}
A var = new B();
var.foo(); //将输出 B
var.bar(); //将输出 A
((B)var).foo(); //将输出 B
((B)var).bar(); //将输出 B
英文:
Unless you are referencing a static method through an object call, upcasting and downcasting won't make a difference the safety of the call
public class A {
public void foo() {
System.out.println("A");
}
public static void bar() {
System.out.println("As");
}
}
public class B extends A {
private void foo() {
System.out.println("B");
}
public static void bar() {
System.out.println("Bs");
}
}
A var = new B();
var.foo(); //will print B
var.bar(); //will print A
((B)var).foo(); //will print B
((B)var).bar(); //will print B
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论