java:定义Dictionary类(属性:eng,type,chinese,example)对各属性分别实现get/put功能;使用Map存储生成的若干Dictionary词条;实现查询并显示词条信息。
时间: 2023-06-06 17:05:12 浏览: 60
好的,这是一个关于Java编程的问题,我可以帮您解答。以下是代码示例:
```
import java.util.*;
public class Dictionary {
private String eng;
private String type;
private String chinese;
private String example;
public String getEng() {
return eng;
}
public void setEng(String eng) {
this.eng = eng;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getChinese() {
return chinese;
}
public void setChinese(String chinese) {
this.chinese = chinese;
}
public String getExample() {
return example;
}
public void setExample(String example) {
this.example = example;
}
public static void main(String[] args) {
//创建字典词条
Dictionary d1 = new Dictionary();
d1.setEng("hello");
d1.setType("n.");
d1.setChinese("你好");
d1.setExample("Hello, how are you?");
Dictionary d2 = new Dictionary();
d2.setEng("world");
d2.setType("n.");
d2.setChinese("世界");
d2.setExample("The world is big.");
//使用Map存储字典词条
Map<String, Dictionary> map = new HashMap<String, Dictionary>();
map.put(d1.getEng(), d1);
map.put(d2.getEng(), d2);
//查询并显示词条信息
String word = "hello"; //待查询的单词
Dictionary d = map.get(word);
if(d != null) {
System.out.println("单词:" + d.getEng());
System.out.println("词性:" + d.getType());
System.out.println("中文释义:" + d.getChinese());
System.out.println("例句:" + d.getExample());
}else {
System.out.println("找不到该单词。");
}
}
}
```
在上面的代码中,我们首先定义了一个Dictionary类,并在其中添加了eng、type、chinese、example四个属性以及相应的get/set方法。接着,我们创建了两个字典词条,并使用Map存储它们。最后,我们通过查询Map中的元素来显示指定单词的词条信息。
希望这个例子能对您有所帮助!