请你看看这个代码,你运行一下试试。
package com.itheima;
import java.util.*;
class SQL {
private static Map<String, String> sql = new HashMap<String, String>();
private SQL() {
}
private static SQL Sql = new SQL();
public static SQL getInstance() {
return Sql;
}
public boolean addSQL(String name, String h) {
if (sql.containsKey(name))
return false;
sql.put(name, h);
return true;
}
public void printSQL() {
Iterator<Map.Entry<String, String>> it = sql.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
System.out.println(me.getKey()+":"+me.getValue());
}
}
}
public class SqlTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// SQL s = SQL.getInstance();
SQL sql=SQL.getInstance();
sql.addSQL("zhangsan", "1");
sql.addSQL("lisi", "2");
sql.addSQL("wangwu", "3");
sql.printSQL();
System.out.println("-----------------------");
SQL sql2=SQL.getInstance();
sql2.addSQL("wenbaoxing", "4");
sql2.printSQL();
}
}
|