<Java.JavaEE面试整理>(10) --"标签"接口,重载vs覆盖........
Q 13: Java中为什么有些接口里并没有定义任何方法? LF FAQ
A 13:Java中的那些没定义任何方法的接口当做Marker来用的,利用它可以告诉编译器对这类接口的实现类另行对待.
Java中像java.io.Serializable,java.lang.Cloneable,java.util.EventListener等这些接口也称为"tag"接口,因为用它们可以将其实现类基于某种特性再分类.
Q 14: 谈谈你对Java里覆盖与重载的理解?它们有什么不同?
Q 14:
?? ?Method Overloading:
?? ??? ?方法重载说的是,在同一类中可以有同名但不同方法签名的方法,利用方法重载,我们可以针对不同的数据以不同的实现方法来定义the same operation.
?? ??? ?如下类所示:
?? ??? ?class MyClass {
?? ??? ??? ?public void getInvestAmount(int rate) {...}
?? ??? ??? ?public void getInvestAmount(int rate,long principal) {...}
?? ??? ?}
?? ??? ?在上例中,两个方法的名字相同,不过它们的method signatures,这就是我们所说的方法重载.
?? ??? ?
?? ?Method Overriding:
?? ??? ?方法覆盖是指,在有父子关系的两个类中方法可以有同样的名字和签名,利用方法覆盖,我们可以针对不同类型的对象以不同的方式定义the same operation.
?? ??? ?如下类所示:
?? ??? ?class BaseClass {
?? ??? ??? ?public void getInstanceAmount (int rate) {...}
?? ??? ?}
?? ??? ?
?? ??? ?class MyClass extends BaseClass {
?? ??? ??? ?public void getInvestAmount(int rate) {...}
?? ??? ?}
?? ??? ?
?? ??? ?上例中,两个类中的方法具有相同的名字和签名,不过子类中的方法覆盖了父类中的相应方法.
?? ??? ?
Q 15: Java中的ArrayList和Vector有什么区别?HashMap与Hashtable呢?stack与queue又有什么不同呢?
What is the main difference between an ArrayList and a Vector? What is the main difference between HashMap
and Hashtable? What is the difference between a stack and a queue??? LF DC PI CI FAQ
A 15:
?? ?Vector/Hashtable:
?? ??? ?这两个类是在没有引入Collections API前已有的,Vector和Hashtable这两个类都是sychronized,Any method that touches their contents is thread-safe.
?? ?ArrayList/HashMap:
?? ??? ?不过若我们并不想用线程安全这个特性时(毕竟这个线程安全是很影响性能的),就可以选ArrayList或HashMap.
?? ?
Q. 哪一个更好呢? 通常来说,ArrayList/HashMap优于Vector/Hashtable.若项目是基于多线程并且其中至少有一个线程要么add要么delete collection中的entry的话,我们选现在Java中新collection API里的external synchronization功能,下面例子中我们就可以根据需要临时地获得一个synchronized的collection:
?? ?Map myMap = Collections.synchronizedMap(myMap);
?? ?List myList = Collections.synchronizedList(myList);
?? ?
?? ?Note:
?? ??? ?1,若使用J2SE5的话,为了提高性能,我们可以用那个新的"java.util.concurrent"包中的API,这样...because the concurrent package collections are not governed by a single synchronized lock as shown above. The “java.util.concurrent” package collections like ConcurrentHashMap is threadsafe and at the same time safely permits any number of concurrent reads as well as tunable number of concurrent writes. The “java.util.concurrent” package also provides an efficient scalable thread-safe non-blocking FIFO queue like ConcurrentLinkedQueue.
?? ??? ?
?? ??? ?2,The “java.util.concurrent” package also has classes like? CopyOnWriteArrayList,? CopyOnWriteArraySet, which gives you thread safety with the added benefit of immutability to deal with data that changes infrequently. The? CopyOnWriteArrayList? behaves much like the ArrayList class, except that when the list is modified, instead of modifying the underlying array, a new array is created and the old array is discarded. This means that when a caller gets an iterator (i.e.? copyOnWriteArrayListRef.iterator() ), which internally holds a reference to the underlying CopyOnWriteArrayList object’s array, which is immutable and therefore can be used for traversal without requiring either synchronization on the list? copyOnWriteArrayListRef or need to clone() the copyOnWriteArrayListRef list before traversal (i.e. there is no risk of concurrent modification) and also offers better performance.? ?
?? ?
?? ?Array:
?? ??? ?1,Java中的arrays要比ArrayList/Vector快的多,因此若我们提前已知道了array的长度的话就尽多地使用arrays.
?? ??? ?2,array里的任何item都可以直接地访问到.
?? ?List/Stack等:
?? ??? ?1,ArrayList/Vector are specialized data structures,其底层是应用了array不过是另外又加了许多方便实用的方法,如add(...),remove(...)等,这样其底层array的大小就会"自动"地伸缩.ArrayList另外也支持基于index的search,如indexOf(Object obj)和lastIndexOf(Object obj).
?? ??? ?2,这些类(接口)与其底层的array相比是更为abstact,对其元素的访问也更为restricted.如对stack添加元素时只能从最后一个元素的位置进行.(These are more abstract than arrays and access is restricted.? For example, a stack allows access to only last item inserted. )
?? ?
?? ?
?? ?Queue<E>(J2SE5.0中引入):
?? ??? ?1,先进先出.
?? ??? ?2,This mechanism is called First In First Out(FIFO);
?? ??? ?3,Placing an item in the queue is called “enqueue or insertion” and removing an item from a queue is called “dequeue or deletion”. Pre J2SE 5.0, you should write your own Queue class with enqueue() and dequeue() methods using an ArrayList or a LinkedList class. ?
?
?? ??? ?J2SE 5.0 has a java.util.Queue<E> interface. ?
?? ?
?? ?Stack:
?? ??? ?1,元素只能在最后一个元素处添加.
?? ??? ?2,LIFO
?? ??? ?3,If you want to reverse "XYZ" -->"ZYX",then you can use a java.util.Stack.
?
?? ?
???