如果我有一个对象,我该如何确定它的类型?(是否存在与Java instanceof运算符等效的OCaml ?)
我正在读这个: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
我正在尝试构建一个通用的类加载器.我需要检查我对方法参数加载的类,以确定它们是否属于同一个类.
代码主要解释了我正在尝试做什么.
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)
这是如何实现的?
我有一个来自另一个闭源的库的类,但我希望能够使用它的接口.原因是我不想在任何地方进行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和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<?>吗?
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中,为什么是instanceof关键字而不是方法?
public static void main(String args[]) {
Simple1 s = new Simple1();
System.out.println(s instanceof Simple); // true
}
Run Code Online (Sandbox Code Playgroud) 当我尝试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) 我正在用 Rust 制作一个 OOP 聊天客户端。模块 messages.rs 创建并处理发送到其他模块的消息作为 structs:SimpleMessage和ComplexMessagestructs:
//! # 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) 我正在尝试检查变量是否属于某种类型。
代码:
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?
instanceof ×10
java ×6
object ×3
typescript ×2
adapter ×1
arrays ×1
class ×1
classloader ×1
generics ×1
inheritance ×1
interface ×1
ocaml ×1
oop ×1
rust ×1
scjp ×1
struct ×1
typechecking ×1
types ×1