4

I'm currently learning to program on Android using the book Android Programming: The Big Nerd Ranch Guide, and I've encountered this line of code

Intent i = new Intent(getActivity(),CrimeActivity.class);

I can't seem to understand why the Intent's constructor need that second argument. If my knowledge serves me right, classes in Java are only a blueprint of an object..

So I'm confused why a literal class is passed as an argument in the Intents constructor?

What's happening right there?

4

3 回答 3

6

在 Java 中,除了原始类型之外的一切都是对象。我们编写的类定义被包装在一个 Object of Class 类中。例如:

class Foo{
}

Foo.classClass类的一个实例。

类对象包含有关类信息的信息,例如:名称、实例变量列表、方法列表等。

此信息可以在运行时通过反射使用。

文档

于 2013-09-01T12:13:31.537 回答
2

根据官方开发者指南 -

这提供了一种方便的方法来创建旨在执行硬编码类名的意图,而不是依赖系统为您找到合适的类。

于 2013-09-01T12:13:58.013 回答
1

你是对的,这个类就像一个对象的蓝图。你给你创建那个“蓝图”的意图,因为意图本身(当最终为你的意图提供服务时,Android系统)将创建你传递给它的类的一个实例(一个对象)。

这就是你只将类而不是实例传递给 Intent 的原因。

于 2013-09-01T12:18:36.553 回答