gpt4 book ai didi

java - 在 Java 8 中使用 .class 进行转换?

转载 作者:行者123 更新时间:2023-12-01 14:07:13 25 4
gpt4 key购买 nike

我认为 .class 是它被调用的对象的类。然而,这不符合我试图用程序做的事情。我会举一些例子。

//o is of type Object
//this method throws error "cannot find symbol" for the cast method
SampleClass.cast(o);
//sampleClass is an instance of SampleClass
//this method throws error "<identifier> expected"
sampleClass.class.cast(o);
//this method works!
SampleClass.class.cast(o);
//this returns two "<identifier> expected" errors
sampleClass.class.class.cast(o);
//this works too!
((SampleClass) o)

我之前以为sampleClass.class == SampleClass,是Class类型的对象。我还认为 SampleClass.class 会返回一些难以想象的真正元数据。现在我知道我真的什么都不知道。感谢任何解决这个难题的帮助:)

编辑:谢谢大家!我很高兴学习有关 Java 的知识。我很感激你的帮助

最佳答案

我整理了一些可编译的 Java 代码,它们或许可以阐明 Class 和转换的一些用法。希望它足够 self 记录。

可以想到Sample.class以同样的方式作为“类文字”"something"是一个字符串文字。它是一个表达式,它有一个值,它可以分配给一个变量,它是一个对象。此对象的类型Class<Sample> .另一方面Sample是一种类型,而不是表达式,您不能将其分配给变量。

Class是一个元类,即它是一个描述另一个类的类。您不能使用超过 Class<Class> classClass = Class.class; 的元数据.有趣的是:还有原始类文字,如 int.class .

class Main {
public static void main(String[] args) {
// class literal
Class<Sample> sampleClassLiteral = Sample.class;

final Sample superObject = new Sample();
// dynamic class
// `? extends Sample` because the dynamic type could be a subclass
Class<? extends Sample> superClassMethod = superObject.getClass();
// classes are comparable both by == and equals, because `Class` is final and it does not override the default equals implementation
assert sampleClassLiteral == superClassMethod;
assert sampleClassLiteral.equals(superClassMethod);

Sample subObject = new SubSample();
// dynamic class can be different from the static type of the variable
Class<? extends Sample> subSampleMethod = subObject.getClass();
assert subSampleMethod != superClassMethod;

SubSample staticCast = ((SubSample) subObject);

List<Object> listOfAny = List.of(superObject, subObject, sampleClassLiteral, "some string", 12);
// static type check & casting
final List<Sample> listOfSample = listOfAny.stream()
.filter(elem -> elem instanceof Sample) // Sample.class::isInstance
.map(elem -> (Sample) elem) // Sample.class::cast
.collect(toList());

final List<Sample> samples = listOfTargetClass(listOfAny, Sample.class);
final List<? extends Sample> sameAsSamples = listOfTargetClass(listOfAny, superClassMethod);
// [superObject, subObject] : does NOT contain `sampleClassLiteral` because it's not of type `Sample`, it is of type `Class<Sample>`
System.out.println(samples);
System.out.println(sameAsSamples);
assert samples.equals(sameAsSamples);

final List<SubSample> subSamples = listOfTargetClass(listOfAny, SubSample.class);
System.out.println(subSamples);
}

static <T> List<T> listOfTargetClass(Collection<?> anyCollection, Class<T> targetClass) {
// dynamic type check and casting
return anyCollection.stream()
.filter(targetClass::isInstance) // obj -> targetClass.isInstance(obj)
.map(targetClass::cast) // obj -> targetClass.cast(obj)
.collect(toList());
}

static class Sample { }

static class SubSample extends Sample { }

}

关于java - 在 Java 8 中使用 .class 进行转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63318502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com