标签: instanceof

OCaml:类型检查对象

如果我有一个对象,我该如何确定它的类型?(是否存在与Java instanceof运算符等效的OCaml ?)

ocaml object instanceof typechecking

7
推荐指数
2
解决办法
1万
查看次数

当用于2个不兼容的类时,为什么`instanceof`错误而不是返回`false`?

我正在读这个:http:
//java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2

他们说:

考虑示例程序:

class Point { int x, y; }
class Element { int atomicNumber; }
class Test {
        public static void main(String[] args) {
                Point p = new Point();
                Element e = new Element();
                if (e instanceof Point) {       // compile-time error
                        System.out.println("I get your point!");
                        p = (Point)e;           // compile-time error
                }
        }
}
Run Code Online (Sandbox Code Playgroud)

instanceof表达不正确,因为没有的实例Element或它的任何可能的亚类(没有在这里示出)的可能可能是任子类的实例Point.

为什么这会导致错误,而不是简单地instanceof返回false?

谢谢,

JDelage

java inheritance instanceof

7
推荐指数
2
解决办法
2175
查看次数

如何检查作为Class对象的参数的instanceof?

我正在尝试构建一个通用的类加载器.我需要检查我对方法参数加载的类,以确定它们是否属于同一个类.

代码主要解释了我正在尝试做什么.

private static LinkedList<Object> loadObjectsInDirectory(Class class0, File dir) throws ClassNotFoundException {

            LinkedList<Feature> objects = new LinkedList<Object>();

            ClassLoader cl = new GenericClassLoader();

            for(String s : dir.list()) {
                Class class1 = cl.loadClass(s);
                try {
                    Object x = class1.newInstance();
                    if (x instanceof (!!! class0 !!!) ) {
                        objects.add(x);
                    }
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                }

            }

            return objects;

        }
Run Code Online (Sandbox Code Playgroud)

这是如何实现的?

java class instanceof classloader

7
推荐指数
1
解决办法
4659
查看次数

如何创建一个我无法更改的类,实现一个接口?

我有一个来自另一个闭源的库的类,但我希望能够使用它的接口.原因是我不想在任何地方进行instanceof检查或检查null,但我也不想扩展现有的类.

例如,假设我有这样的代码:

public class Example {

    // QuietFoo is from another library that I can't change
    private static QuietFoo quietFoo;
    // LoudFoo is my own code and is meant to replace QuietFoo
    private static LoudFoo loudFoo;

    public static void main(String[] args) {
        handle(foo);
    }

    private static void handle(Object foo) {
        if (foo instanceof QuietFoo)
            ((QuietFoo) foo).bar();
        else if (foo instanceof LoudFoo)
            ((LoudFoo) foo).bar();
    }
}
Run Code Online (Sandbox Code Playgroud)

我无法改变QuietFoo:

public class QuietFoo {

    public void bar() { …
Run Code Online (Sandbox Code Playgroud)

java interface proxy-classes instanceof adapter

7
推荐指数
1
解决办法
941
查看次数

带<?>或不带<?>的泛型实例

我对Java和instanceof运算符中的泛型有疑问.

做这样的检查实例是不可能的:

if (arg instanceof List<Integer>)  // immposible due to
                                   // loosing parameter at runtime
Run Code Online (Sandbox Code Playgroud)

但是可以运行这个:

if (arg instanceof List<?>)
Run Code Online (Sandbox Code Playgroud)


现在我的问题 - arg instanceof List 之间有什么区别arg instanceof List<?>吗?

java generics instanceof

7
推荐指数
1
解决办法
427
查看次数

在原语和包装类型数组的情况下的instanceof运算符

int primitivI[] = {1,1,1};
Integer wrapperI[] = {2,22,2};


1. System.out.println(primitivI instanceof Object);//true
2. System.out.println(primitivI instanceof Object[]);//Compilation Error Why ????
3. System.out.println(wrapperI  instanceof Object);//true
4. System.out.println(wrapperI  instanceof Object[]);//true
Run Code Online (Sandbox Code Playgroud)

这里我有两个整数(primitve,Wrapper)类型的数组,但我对instanceof运算符有不同的结果

看到第2行和第4行第4行将成功编译并给出结果为true但是在第2行的情况下,为什么会导致编译错误?从第1行和第3行可以清楚地看出,这两个数组是对象的实例,但如果是Object[],为什么结果会有所不同?

java arrays scjp object instanceof

7
推荐指数
1
解决办法
144
查看次数

在Java中,为什么是关键字的instanceof而不是方法?

在Java中,为什么是instanceof关键字而不是方法?

public static void main(String args[]) {
    Simple1 s = new Simple1();  
    System.out.println(s instanceof Simple); // true
}
Run Code Online (Sandbox Code Playgroud)

java instanceof

7
推荐指数
1
解决办法
313
查看次数

在 if-else 语句中使用 instanceof 将 Typescript 类型缩小为从不

当我尝试instanceof在 if-else 语句中使用派生类实例时遇到问题。考虑以下示例:

interface IBaseModel {
    id: string
}

class BaseClass {
    model: IBaseModel
    constructor() {
    }

    setModel(model: IBaseModel) {
        this.model = model
    }

    getValueByName(name: string) {
        return this.model[name];
    }
}

interface IDerived1Model extends IBaseModel {
    height: number;
}

class Derived1 extends BaseClass {
    setModel(model: IDerived1Model) {
        super.setModel(model);
        // Do something with model...
    }
}

interface IDerived2Model extends IBaseModel {
    width: number;
}

class Derived2 extends BaseClass {
    setModel(model: IDerived2Model) {
        super.setModel(model);
        // Do something with model...
    } …
Run Code Online (Sandbox Code Playgroud)

instanceof typescript type-narrowing

7
推荐指数
1
解决办法
3225
查看次数

什么是像 Java 的 instanceof 这样的结构的函数?

我正在用 Rust 制作一个 OOP 聊天客户端。模块 messages.rs 创建并处理发送到其他模块的消息作为 structs:SimpleMessageComplexMessagestructs:

//! # Messages

use time::SteadyTime;

/// Represents a simple text message
pub struct SimpleMessage<'a> {
    pub user: ...
    pub time: &'a SteadyTime<'a>,
    pub content: &'a str,
}

/// Represents attachments, like text or multimedia files.
pub struct ComplexMessage<'a> {
    pub user: ...
    pub time: &'a SteadyTime<'a>,
    //pub content: PENDING
}

impl<'a> SimpleMessage<'a> { }
impl<'a> ComplexMessage<'a> { }

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn is_simple() { …
Run Code Online (Sandbox Code Playgroud)

oop struct object instanceof rust

7
推荐指数
1
解决办法
1093
查看次数

检查变量是否属于 Typescript 中的自定义类型

我正在尝试检查变量是否属于某种类型。

代码:

type GeneralType = SubTypeA | SubTypeB;
type SubTypeA = 'type1' | 'type2';
type SubTypeB = 'type3' | 'type4';

function someFunction(arg1: GeneralType) {
  if (arg1 instanceof SubTypeA) {
    // Do something
  }
  // Continue function
  return arg1;
}
Run Code Online (Sandbox Code Playgroud)

当然,这段代码在第 6 行失败了,因为instanceof它不能用于类型。有没有我可以使用的替代选项,而无需明确检查 的每个可能值SubTypeA

types instanceof typescript

7
推荐指数
1
解决办法
9552
查看次数