简体   繁体   English

关于java的事实问题

[英]Factual questions about java

My teacher gave out a practice exam on java recently and I'm curious to see how I did on the true/false part. 我的老师最近给出了关于java的练习考试,我很想知道我是如何对真/假部分做的。 I'm least confident about number one, so any explanation there would help a lot. 我对第一名最不自信,所以任何解释都会有所帮助。

  1. In Java, a class can extend any number of abstract classes. 在Java中,类可以扩展任意数量的抽象类。

    false. 假。 I don't quite understand why, is it just because in an inheriting class the the parent classes could cause a conflict? 我不太明白为什么,是因为在继承类中,父类可能会导致冲突?

  2. In Java, it is illegal to pass a parameter whose type is an abstract class. 在Java中,传递类型为抽象类的参数是非法的。

    False, abstract classes don't even have constructors... 虚假的抽象类甚至没有构造函数......

  3. In Java, an abstract class can have any number of subclasses. 在Java中,抽象类可以包含任意数量的子类。

    True. 真正。 I can't think of anything that would restrict this. 我想不出任何会限制这种情况的事情。

  4. In Java, there is no restriction on the number of interfaces a class can implement. 在Java中,类可以实现的接口数量没有限制。

    True 真正

  5. It is not possible to implement a stack as a doubly-linked list, because a stack requires access to only one end of the list, and a doubly-linked list provides access to both ends of the list. 无法将堆栈实现为双向链接列表,因为堆栈只需要访问列表的一端,而双向链接列表则提供对列表两端的访问。

    true. 真正。 but it wouldn't be very efficient. 但效率不高。

1) It is false because Java does not support multiple inheritance. 1)它是错误的,因为Java不支持多重继承。 A class can only extend one class, whether it is abstract or not. 一个类只能扩展一个类,无论它是否为抽象类。 It can transitively extend multiple classes (eg, it extends B which extends C so indirectly it also extends C). 它可以传递地扩展多个类(例如,它扩展了B,它扩展了C,因此间接地扩展了C)。 A class can implement multiple interfaces. 一个类可以实现多个接口。 There are many reasons why Java does not support multiple inheritance, but it does support multiple interfaces, so it is better in many ways. Java不支持多重继承有很多原因,但它确实支持多个接口,因此它在很多方面都更好。

2) First of all, abstract classes can have constructors. 2)首先,抽象类可以有构造函数。 The claim is false because you are allowed to pass abstract types as parameters. 声明是错误的,因为您可以将抽象类型作为参数传递。 Because of polymorphism, you will be passing a concrete subtype that you instantiated already. 由于多态性,您将传递已实例化的具体子类型。

3) That is true 3)那是真的

4) That is true to a degree (there is some limit by the JVM implementation, but you'll never hit it in practice) 4)这在某种程度上是正确的(JVM实现存在一些限制,但在实践中你永远不会遇到它)

5) You can easily implement a stack as a doubly linked list, it's a good exercise. 5)您可以轻松地将堆栈实现为双向链表,这是一个很好的练习。 It is even efficient since you're still doing everything at O(1). 它甚至是高效的,因为你仍然在O(1)做所有事情。

1) Correct. 1)正确。 Java (like C#) rejected multiple inheritance of implementation, mostly for complexity reasons I believe. Java(如C#)拒绝了多次实现继承,主要是出于复杂性的原因,我相信。

2) Half-correct: the actual answer was right (false; you certainly can declare parameters, variables etc of abstract classes) but your reasoning is wrong (and I suspect you meant "true" which would have been entirely wrong). 2)半正确:实际答案是正确的(错误;你当然可以声明抽象类的参数,变量等),但你的推理是错误的(我怀疑你的意思是“真的”,这本来是完全错误的)。 Abstract classes can have constructors which are called by derived classes. 抽象类可以具有由派生类调用的构造函数。 Besides that, constructors are somewhat irrelevant to whether the type of a parameter can be an abstract class. 除此之外,构造函数与参数的类型是否可以是抽象类有些无关。

3) Correct. 3)正确。

4) Correct. 4)正确。

5) Well, you're wrong in that it is possible (whereas you claimed that it's true that it's not possible). 5)好了,你错了,它可能的(而你声称,这是真的,这是不可能的)。 The fact that an implementation has access to both ends of the list doesn't mean that it has to expose both ends. 实现可以访问列表的两端并不意味着它必须公开两端。

  1. Java has single inheritance; Java有单继承; a class can inherit from a single parent class, whether abstract or not. 一个类可以从单个父类继承,无论是否抽象。 You can, of course, have a chain of multiple classes inheriting from each other, eg abstract A --> abstract B -> C . 当然,您可以拥有一系列彼此继承的多个类,例如abstract A - > abstract B - > C
  2. False, but the constructor issue has nothing to do with it. 错,但构造函数问题与它无关。 Passing an argument whose type is constrained to that of an abstract class guarantees the interface for incoming objects, and is a fundamental tenet of polymorphism. 传递一个类型受限于抽象类的参数保证了传入对象的接口,并且是多态的基本原则。
  3. True. 真正。 Abstract classes tend to define partial implementations of an interface, so it would generally be a waste of time having only one implementation. 抽象类倾向于定义接口的部分实现,因此通常只有一个实现会浪费时间。
  4. True. 真正。
  5. False, surely? 是的,当然可以吗? You could just push/pop off one end, ignoring the reverse link. 你可以只推/弹一端,忽略反向链接。

1) is false, because in Java "extends" is a specific operation that inherits both all the data structures and the implementation of all methods. 1)是假的,因为在Java中“extends”是一个特定的操作,它继承了所有数据结构和所有方法的实现。 In Java, a class can only extend one superclass. 在Java中,类只能扩展一个超类。 however, in Java you can implement any number of interfaces , which only "bring in" the name and signature of functions you must implement. 但是,在Java中,您可以实现任意数量的接口 ,这些接口只会“引入”您必须实现的函数的名称和签名。

The reason for this is that extended multiple base classes — which is called "multiple inheritance" — has been considered confusing. 原因是扩展的多个基类 - 称为“多重继承” - 被认为是令人困惑的。 It's a little bit of a religious war issue; 这是一个宗教战争问题; C++ does multiple inheritance, Java and Smalltalk don't, and Ruby instead lets you mix in modules to much the same effect as multiple inheritance. C ++执行多重继承,而Java和Smalltalk则没有,而Ruby则允许您将模块混合到与多重继承相同的效果。

2) You CAN pass something as an abstract class. 2)您可以将某些东西作为抽象类传递。 Consider for example an abstract class Shape, with concrete subclasses Circle, Square, and Triangle. 例如,考虑一个抽象类Shape,具有圆形,方形和三角形的具体子类。 All of them are "kinds of" shape, so you would pass a Shape to any operation that should work for hem all. 所有这些都是“种类”的形状,所以你可以将一个Shape传递给任何应该适合所有人的操作。

3) The only restruction is that you can make a class final, which prevents it from neing subclassed. 3)唯一的重建是你可以使一个类最终,这可以防止它进入子类。 It doesn't make sense to make an abstract class final; 使抽象类最终成功是没有意义的; you would never be able to use it. 你将永远无法使用它。

4) Yup, and that's how you get away from needing to be able to extend multiple classes; 4)是的,这就是你如何摆脱需要能够扩展多个类; interfaces are the way you get something like multiple inheritance. 接口就是你获得多重继承的方式。

5) It is certainly possible to implement a stack as a doubly-linked list. 5)当然可以将堆栈实现为双向链表。 What's more, in certain situations you'd want to. 更重要的是,在你想要的某些情况下。 Consider, for example, something where you'd like to be able to display the whole stack without disturbing it. 例如,考虑一下你希望能够在不打扰整个堆栈的情况下显示整个堆栈的东西。 It could then be useful to extend that stack with a non-destructive "show all" operation. 然后,使用非破坏性的“全部显示”操作扩展该堆栈是有用的。 In a Stack implemented on a linked list, then, you could "walk" the whole list, and "walk" it back to the head. 然后,在链接列表上实现的堆栈中,您可以“遍历”整个列表,并将其“移动”回头部。

1: It is false because in Java you simply can't extend multiple classes. 1:这是错误的,因为在Java中你根本无法扩展多个类。

2: I think your wrong: Abstract classes can have a constructor! 2:我认为你错了: 抽象类可以有一个构造函数!

3: True, you're right. 3: 没错,你是对的。 The abstract class doesn't even "know" if it gets a subclass. 抽象类甚至不知道它是否获得子类。

4: True, you're right, there is only a theoretical limit (nothing on computers is unlimited). 4: 没错,你是对的,只有理论上的限制(计算机上没有任何东西是无限制的)。

5: I think you missunderstood the question: If you got a Double-Linked-List you can in fact take it and make a Stack out of it . 5:我认为你错过了这个问题: 如果你有一个Double-Linked-List,你实际上可以把它拿出来并制作一个Stack In the List you can get the first element so you can take this method to implement the stack 在List中,您可以获取第一个元素,以便您可以使用此方法来实现堆栈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM