我很好奇地问这个,也许这是毫无意义的.
当我们在java中使用instanceof时,如:
if (a instanceof Parent){ //"Parent" here is a parent class of "a"
}
Run Code Online (Sandbox Code Playgroud)
为什么我们不能使用如下:
if (a instanceof Parent.class){
}
Run Code Online (Sandbox Code Playgroud)
从严格编程的角度来看,第二个'instanceof'是否更有意义?"Parent"和"Parent.class"有什么区别?
我在JSF EL中找到了instanceof运算符,但它在使用时会引发异常.它显然是保留但没有实现?什么时候(可能)可用,如果还没有比我现在使用的JSF 1.2更新的版本?
是否使用instanceof关键字反对的本质object oriented programming?我的意思是这是一个糟糕的编程习惯吗?我在某处读到使用instanceof关键字意味着设计可能不那么好.有更好的解决方法吗?
我试图看看Java中的instanceof运算符是如何工作的,并且面临着一个非常奇怪的问题.
public static void main(String[] args) {
Map m = new HashMap();
System.out.println("m instanceof Date: " + (m instanceof Date));
}
Run Code Online (Sandbox Code Playgroud)
以上内容按预期返回false.然而,
public static void main(String[] args) {
HashMap m = new HashMap();
System.out.println("m instanceof Date: " + (m instanceof Date));
}
Run Code Online (Sandbox Code Playgroud)
这甚至都没有编译.我收到一个错误
inconvertible types
found : java.util.HashMap
required : java.util.Date
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?我正在使用IntelliJ Idea 11.
我正在编写一个方法,我想将一个类传递给一个方法,其中一部分代码包括检查对象是否属于某种类型.这就是我想要的(但显然不起作用):
private static class MyClass1 { /***/ }
private static class MyClass2 { /***/ }
private void someFunc() {
/* some code */
methodName(MyClass1);
methodName(MyClass2);
}
private void methodName(Class myClass) {
Object obj;
/* Complicated code to find obj in datastructure */
if (obj instanceof myClass) {
/* Do stuff */
}
}
Run Code Online (Sandbox Code Playgroud)
有关如何做到这一点的任何提示?谢谢!
我的Java类代表数据库中的实体,我发现覆盖equals我的类的方法以通过id进行比较是切实可行的.所以例如在我的Transaction课堂上我有这段代码
@Override
public boolean equals(Object other){
if (other == null) return false;
if (other == this) return true;
if (!(other instanceof Transaction))return false;
Transaction otherTrans = (Transaction) other;
if (id == null || otherTrans.id == null) return false;
return id.equals(otherTrans.id);
}
Run Code Online (Sandbox Code Playgroud)
现在,对我来说似乎有点难看,每个类都拥有相同的代码片段,只更改了类的名称.我想让我的类扩展一个超类MyEntity,我会编写上面的方法,替换instanceof Transaction为类似的东西instanceof this.getClass(),但这似乎不可能.我也想过用它代替它instanceof MyEntity,但这意味着两个对象可以被认为是相等的,即使它们属于不同的类,只要它们具有相同的id.还有其他方法吗?
如果我尝试使用错误的类的instanceof运算符我得到一个编译错误("动物不能转换为字符串"),但有一个接口,我没有得到编译时错误.
例如:在第10行,我收到编译错误,因为Animal不是String的子类.但是在第14行中,即使Animal没有实现List接口,我也没有收到编译错误.
class Animal {
}
public class InstanceOf {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Animal a = new Animal();
if (a instanceof String ){ //line 10
System.out.println("True");
}
if (a instanceof List ){ //line 14
System.out.println("True");
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在阅读 Oracle 的官方文档来了解Java 17 中的模式变量范围。在以下示例中,该方法testScope1按照文档中的说明工作,但该方法testScope2给出了编译错误。我无法弄清楚为什么void该方法的返回类型会导致问题?
interface Vehicle{}
class Car implements Vehicle{}
class Pattern{
public int testScope1(Vehicle v){
if(!(v instanceof Car c)){
return 1;
}
System.out.println(c.toString()); //WORKS FINE
return 2;
}
public void testScope2(Vehicle v){
if(!(v instanceof Car c)){
}
System.out.println(c.toString()); //COMPILE TIME ERROR: Cannot resolve symbol c
}
}
Run Code Online (Sandbox Code Playgroud) 通过一些研究我发现,虽然ArrayBufferView最初没有公开(通过[NoInterfaceObject]),但由于我描述的用例,似乎应该有广泛的一致意见.
最初的协议是ArrayBufferView在DOMWindow命名空间上公开构造函数,该命名空间在Safari(并且仍然在6.1.1中)和Chrome中实现,但随后从Chrome中提取,转而使用静态方法ArrayBuffer.isView().
与此同时,Mozilla(仍在)谈论实施 ArrayBuffer.isView().
Safari公开了ArrayBufferView构造函数
Chrome有 ArrayBuffer.isView()
Firefox什么都没有
IE - 我还没有接近......
所以,我的问题.检查对象是否是ArrayBufferView实例的最简洁方法是什么?
我有很多类,我希望用户键入一个名称,他将获得一个特定对象(类)的同名实例.我用这段代码简化了它:
public class Animal {...}
public class lion extends Animal{...}
public class zebra extends Animal{...} // and so on for a lot of animals
String name = input from user
Animal something = new Animal(instance of the input name)
Run Code Online (Sandbox Code Playgroud)
在最后一行,我实际上想将字符串名称转换为类名的实例.有什么办法吗?会有很多动物,所以我不想写很多开关案例:"如果输入等于狮子"或斑马或蛇或......
instanceof ×10
java ×8
arraybuffer ×1
class ×1
el ×1
java-17 ×1
javascript ×1
jsf ×1
object ×1
string ×1
typedarray ×1