Java 类与 C++课程
我刚刚开始学习 Android 开发,而且我有严格的 C++ 背景。我发现Java不支持多重继承,但是我们真的必须为每个新的Activity创建一个单独的.java文件吗?
我有一个 TabActivity,还有 3 个 Activity 组成选项卡。我想在其中一个选项卡上创建列表视图,但我开始感到困惑。我需要制作另一个扩展 ListActivity 的 .java 文件吗?如果我需要为每个活动创建一个单独的 .java 文件,我什至可以看到一个相对较小的应用程序变得非常大。
旁注
如果我想将 ListView 添加到我的一个选项卡中,我该怎么做?到目前为止,我没有发现任何提及如何向其他活动添加新活动的内容。
I just started learning to develop for the Android, and I come from a strictly C++ background. I found that Java does not support Multiple Inheritance, but do we really have to create a seperate .java file for every new Activity?
I have a TabActivity, with 3 more Activitys that make up the tabs. I want to create a List View on one of the tabs, but I am starting to get confused. Do I need to make another .java file that extends ListActivity? I can see even a relatively small application becoming extremely large if I am going to need to create a seperate .java file for every activity.
SIDE NOTE
If I want to add the ListView to one of my tabs, how would I do that? Nothing I have found thus far mentions how to add new activities to other Activities.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
其他答案给出了有关接口的很好的观点,这可能正是您所寻找的。但是,有关更多信息,您不必为每个新类创建一个新的 .java 文件,有 替代方案。然而,请记住,更多的课程不一定是坏事。您的选择是...
嵌套类:
B 的实例可以访问 A 的私有变量,但如果没有 A 的实例则无法构造(这是按钮处理程序等常用的方法)
嵌套静态类:
B 的实例无法访问 A 的私有变量,但它们不需要 A 的实例即可构造。如果 B 被声明为 public,则 B 的实例可以在任何地方实例化;如果 B 被声明为 private,则只能在 A 的方法中实例化。
匿名类:
这种奇怪的语法创建了一个没有名称的类,其功能与嵌套类相同(例如可以访问私有变量)。这是编写嵌套类的另一种形式,有时更短,但会导致非常奇怪的语法。
The other answers give good points about interfaces and that is probably more what you are looking for. However, for further information, no you don't have to create a new .java file for every new class, there are alternatives. However, keep in mind that more classes is not necessarily a bad thing. Your options are...
Nested Class:
Instances of B can access private variables of A but cannot be constructed without an instance of A (this is an approach often used for button handlers, etc.)
Nested Static Class:
Instances of B cannot access private variables of A but they do not require an instance of A in order to be constructed. Instances of B can be instantiated anywhere if B is declared public, or only in A's methods if B is declared private.
Anonymous Class:
This strange syntax creates a class that has no name and functions the same as a nested class (e.g. can access private variables). It's an alternative form of writing nested classes that is sometimes shorter but leads to very strange syntax.
Java 不支持抽象类或普通类的多重继承,而仅支持接口的多重继承。
您可以做的是创建扩展特定
Activity
的抽象类,并继续创建抽象类,直到您拥有一个继承所有 Activity 功能的类。或者,有一个继承多个接口的类。
Java doesn't support multiple inheritance on abstract or normal classes but on interfaces only.
What you can do is create abstract classes that extends a particular
Activity
and keep creating abstract classes until you have a class that inherit all your activity features.Alternatively, is have a class that inherits multiple interfaces.
如果使用 IDE 来执行此操作,创建大量文件的开销会小得多。
然而,有几种方法可以在同一个文件中包含多个类。这包括 a) 使用内部/嵌套类 b) 匿名类 c) 枚举 d) 包本地类。
您唯一不能做的就是将类的定义分散到多个文件中。
If you use an IDE to do this, the overhead of creating lots of file is much smaller.
However there are a few ways to have many classes in the same file. This includes a) using inner/nested classes b) anonymous classes c) enums d) package local classes.
The only thing you cannot do is easily if spread the definition of a class across multiple files.
解决方法是使用接口
?如果您的活动可以在现有类中完成或者可以在现有类中进行子类化,则不需要。 Java 是围绕为每个活动创建类而构建的。
Workaround is you use interfaces
No, if your activity can be done in an existing class or can be sub classed in an existing class. Java is built around creating classes for each activity.
Java 不允许多重继承。但它确实允许实现多个接口。您不需要为每个
类
创建单独的文件(尽管这是一个很好的做法)。您可以将子类封装在文件的主class
中。ListView
可以简单地扩展
,您可以在xml等中使用相应的扩展类。Java doesn't allow multiple inheritance. But it does allow implementing multiple
interface
s. You don't need to create a seperate file for eachclass
(though it's a good practice). You can encapsulate sub-classes in the mainclass
of your file.ListView
can be simplyextend
ed and you can use the corresponding extended class in the xml etc.Java 确实支持多重继承,如果你只是将基类抽象并称它们为
interface
而不是class
。Java does kind of support multiple inheritance, if you just make the base classes abstract and call them
interface
instead ofclass
.您可以简单地在 XML 中声明一个
ListView
并在Activity
中调用它,不需要单独的ListActivity
,只需为ListView
。当您的整个Activity
是一个ListView
时。考虑到更多课程的担忧,这并不是一件坏事。更多的类意味着更多的面向对象,但并不严格(一切都有限制)。
由于死亡钻石的问题,多重继承被认为不好。因此,解决方法是使用 Java 中称为“接口”的“纯虚拟类”(尽管“接口”在 Objective-C 中是指普通类)。
我建议您阅读 HeadFirst Java 第二版,以便更好地掌握术语及其使用。对于初学者来说这是一本好书。
You can simply declare a
ListView
in XML and call it in theActivity
, no need to have a separateListActivity
, just forListView
. It's when your wholeActivity
is aListView
.And as per the concerns about more classes, it's not a bad thing. More classes means more object oriented, not strictly though (Everything has a limit).
Multiple Inheritence was considered not good due to problem of diamond of death. So the work-around was to use
Pure Virtual Classes
known asInterface
in Java (Interface means a normal class in Objective-C though).I suggest you to read HeadFirst Java second edition, for better grip on terminology and their use. It's a good book for beginners.