I have learned a little experience in using Java (4)

zhaozj2021-02-17  47

After the previous article issued a message, Cheng Meng's netizens pay attention, published a lot of comments, I feel that many people have misunderstandings about me, probably I express unclear reasons. This article is a supplement to the previous, and an example sets to the importance of understanding the Collection framework.

I have written a function Printall (Vector Vector) before half a year, and the specific code is as follows.

Import java.util. *;

public class UtilTool {public static void printAll (Vector vector) {System.out.println ( "the Collection is vector"); System.out.println (vector.getClass () toString ().); Iterator iterator = vector.iterator (); While (item.hasnext ()) {system.out.println (item.next (). ToString ());}}

Public static void main (string [] arg) {Vector vector = new vector (); vector.add (new integer (1)); Vector.Add (New Integer (2)); Vector.Add (New Integer (3) ); Utiltool.printall (vector);}

}

Printall This function design is very bad - not universal, if you want to print the Hashset type data, you must overload the Printall function, the code is as follows

Public Static Void Printall (Hashset Hashset) {System.Out.println ("The Collection Is Hashset"; System.out.Println (Hashset.getClass (). Tostring ()); Iterator Iterator = HashSet.Iterator (); while (Iterator.HasNext ()) {system.out.println (item.next (). Tostring ());}}

The code reuse of the Printall function is low. In fact, Vector and HashSet are implemented by Collection, which can change the parameter type of Printall to Collection without being overloaded. code show as below

public static void printAll (Collection collection) {System.out.println ( "the Collection is collection"); System.out.println (collection.getClass () toString ().); Iterator iterator = collection.iterator (); while (Iterator.HasNext ()) {system.out.println (item.next (). toString ());}} This can delete the PrintAll (Vector Vector) and PrintAll (Hashset Hashset) functions.

When designing a function, you should use the interface, not a class. Of course, you must understand that vector is the implementation of Collection.

If the inheritance relationship to Collection is unclear, it is easy to abuse overload. The following code is a problematic code (from Effective Java Programming Language Guide) Public Class Collectionclassifier {

Public Static String Classify (Set S) {

Return "SET";

}

Public Static String Classify (List L) {

Return "List";

}

Public Static String Classify (Collection C) {

Return "Unknow Collection";

}

Public static void main (string [] args)

Collection [] Tests = New Collection [] {

New hashset (),

New arriselist (),

New hashmap (). VALUES ()

}

For (int i = 0; i

System.out.println (Classify (TEST [I]));

}

}

The program output is three "Unknown Collection" instead of printing "set", "list", "Unknown Collection" you expect. The root of this program is unfamiliar with the Collection hierarchy, and overloading is caused.

转载请注明原文地址:https://www.9cbs.com/read-30199.html

New Post(0)