我可以以某种方式看到元组内容的类型和大小吗?我希望输出类似于:
(str, list, int) 或者任何可能的东西(只是打印它们很难,因为有嵌套列表)
x = someSecretTupleFromSomewhereElse
print type(x)
<(type 'tuple')>
Run Code Online (Sandbox Code Playgroud) 我有以下 JavaScript 代码:
function testClass() {
this.SaveValue = function (value) {
var isInstance = value instanceof TestEnum;
if (!isInstance) {
return;
}
}
}
TestEnum = {
VALUE_0: 0,
VALUE_1: 1,
VALUE_2: 2
}
Run Code Online (Sandbox Code Playgroud)
我通过以下方式创建该对象的实例:
$(function () {
var a = new testClass();
a.SaveValue(TestEnum.VALUE_1);
});
Run Code Online (Sandbox Code Playgroud)
我想做的就是测试传递给函数的值SaveValue实际上是TestEnum. 但是,当我运行此代码时,出现以下错误:Uncaught TypeError: Expecting a function in instanceof check, but got 1
我以正确的方式处理这件事吗?我尝试了 typeof 但它只返回,number这对我来说不是特别有用。
我正在创建某种成就系统,我有一个类KillXEnemies,然后我继承了它的类,如KillXEnemiesWeapon(用某种武器杀死的敌人).当你杀死敌人时,我会通过成就对象循环并添加一个敌人已被杀死:
if(object instanceof KillXEnemies)
((KillXEnemies)object).addEnemyKilled();
Run Code Online (Sandbox Code Playgroud)
但随后KillXEnemiesWeapon也会被跟踪,因为它继承自KillXEnemies.我知道一个解决方案是:
if(object instanceof KillXEnemies && !(object instanceof KillXEnemiesWeapon))
((KillXEnemies)object).addEnemyKilled();
Run Code Online (Sandbox Code Playgroud)
但是我将要从KillXEnemies继承许多类,并且它似乎是一个糟糕的解决方案,有20个!(对象instanceof(---))
所以我想知道是否有更简单的方法来检查对象是否只是KillXEnemies而不是KillXEnemiesWeapon
不使用instanceof 相当于什么?使用 true、false 或 else 语句时也许更简单?
public static void p (Object...ar)
{
for (int i=0; i < ar.length; i++)
{
if (ar[i] instanceof int[])
{
Run Code Online (Sandbox Code Playgroud) 我现在正在寻找解决我问题的方法一段时间,但我找不到一个特定于我的问题的答案.我有一个抽象的A类和扩展A类的B和C类.A和B是具体的类.A类实现了将由V和C继承的函数.在这个函数中我想创建B或C的新对象 - 问题是我不知道哪个对象是那个.
我怎样才能做到这一点?
public void colision(List<Organism> organisms) {
List<Organism> temp = new ArrayList<Organism>(organisms);
temp.remove(this);
for (Organizm organism : temp){
if (this.location == organizm.getLocation()){
if (this.getClass().equals(organism.getClass())){
//here is what I need to figure out
}
else{
...
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试为压力测试目的创建任何类的随机生成器。
我的生成器目标是使用默认构造函数创建任何类并填充其原始字段和字符串字段(通过反射)
我还想填充数字类型(整数、长、双....)的字段,因为它们很容易转换为基元。
但是我instanceof在类型之间找不到简单的方法 o 。
这是方法
public static <V> Collection<V> createMassiveCollection(Class<V> clazz, int amount, Class<?> collectionType) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
Random r = new Random();
RandomGenerator rg = new RandomGenerator();
@SuppressWarnings("unchecked")
Collection<V> collection = (Collection<V>) collectionType.newInstance();
for (int i = 0; i < amount; i++) {
V item = clazz.newInstance();
for (Field field : item.getClass().getDeclaredFields()) {
if (java.lang.reflect.Modifier.isStatic(field.getModifiers()) || java.lang.reflect.Modifier.isFinal(field.getModifiers()))
continue;
field.setAccessible(true);
if (field.getType().equals(String.class))
field.set(item, rg.nextCompliment());
else if (field.getType().isPrimitive() && (field.getType() != boolean.class …Run Code Online (Sandbox Code Playgroud) 我有这些嵌套的哈希集,其中内部包含String值。
{{a,b},{b,c},{c,e}}
在我的代码中有一点,我不知道我是在处理内部哈希集还是外部哈希集。我试图通过使用以下代码行来确定:
System.out.println(loopIterator3.next() instanceof String);
//(FYI :Iterator <HashSet> loopIterator3 = hsConc2.iterator();)
Run Code Online (Sandbox Code Playgroud)
这行代码似乎产生了一个错误:
prog.java:61: 错误:类型不兼容:HashSet 无法转换为 String System.out.println(loopIterator3.next() instanceof String);
当loopIterator3确实遍历内部哈希集时,我希望它会采用字符串值。为什么编译器认为它是一个哈希集?此外,为什么编译器认为我正在尝试转换?
有什么想法/建议吗?
import java.util.Arrays;
import java.util.HashSet;
class Scratch {
public static void main(String[] args) {
HashSet<HashSet<String>> hashSets = new HashSet<>(Arrays.asList(newSet("a", "b"), newSet("b", "c"), newSet("c", "e")));
System.out.println(hashSets.iterator().next() instanceof String); //error
System.out.println(hashSets.iterator().next().iterator().next() instanceof String);
}
private static HashSet<String> newSet(String... str) {
return new HashSet<>(Arrays.asList(str));
}
}
Run Code Online (Sandbox Code Playgroud) 我想创建一个名为 的函数createInstance,该函数接收一个实例a并创建一个c与 类型相同的新实例a。请注意,我不知道里面的createInstance类型是什么a,我只知道它继承自某个类A。但我想c成为一个类型B,这是真正的类型a。这是我到目前为止所得到的:
class A {
constructor(public ref: string) {}
}
class B extends A {
}
const createInstance = (a: A): void => {
const t = a.constructor
const c = new t("c")
console.log(c)
console.log(c instanceof B)
}
const b = new B("b")
createInstance(b)
Run Code Online (Sandbox Code Playgroud)
我已经在打字稿操场上尝试过了,它有效,我得到true了c instanceof B。但它在行中显示了一条警告new t("c"),即:“此表达式不可构造。类型‘函数’没有构造签名。”
这样做的正确方法是什么?谢谢
在我尝试重构的那一刻,我已经有了这个方法。
public static boolean isEmpty(Object value) {
boolean empty = false;
if (value instanceof String && ((String) value).isEmpty()) {
empty = true;
} else if (value instanceof List && ((List<?>) value).isEmpty()) {
empty = true;
} else if (value instanceof String[] && ArrayUtils.isEmpty((String[]) value)) {
empty = true;
} else if (value == null) {
empty = true;
}
return empty;
}
Run Code Online (Sandbox Code Playgroud)
有没有明显更好的方法来做到这一点,我错过了?
我知道我可以if使用链式||语句将所有条件放在一个语句上,但我真的不知道这比我现在得到的更好。
我有一个叫做Document可以通过Page或扩展的类Image.对于每一个我在数据库中有一个插入方法.用于插入Page和Image包括用于插入Document.
我怀疑的是,确定将调用哪种方法的方法是首先验证类,如下所示:
if (doc instanceof Document){
insertDoc(doc);
}
if (doc instanceof Page){
insertPage(doc);
}
if (doc instanceof Image){
insertImage(doc);
}
Run Code Online (Sandbox Code Playgroud)
或者还有另一种方式,因为我总是听说使用instanceof不是一件好事.
编辑:多态在这种情况下不起作用,对吗?像insert(Document doc),insert(Page doc)和insert(Image doc).
instanceof ×10
java ×7
javascript ×2
collections ×1
constructor ×1
generics ×1
hashset ×1
if-statement ×1
instance ×1
is-empty ×1
object ×1
parents ×1
python ×1
reflection ×1
tuples ×1
typeof ×1
types ×1
typescript ×1