使用Get和Set时是否可以应用搜索功能?
抱歉,我是 Java 新手,对此问题可能有一个非常简单的答案。
目前,我正在单独的 JOptionPane
中打印所有结果。
我想设置一个搜索功能,以允许我根据给定的条件显示某些结果。
到目前为止,这是我的代码:
public class Main {
public static void main(String[] args) {
//Create new Person objects
Address p1 = new Address("27","Abbey View","Hexham","NE46 1EQ");
Address p2 = new Address("15", "Chirdon Crescent", "Hexham", "NE46 1LE");
Address p3 = new Address("6", "Causey Brae", "Hexham", "NE46 1DB");
Details c1 = new Details();
Details c2 = new Details();
Details c3 = new Details();
//Send some messages to the objects
c1.setBeds("3 ");
c2.setBeds("6");
c3.setBeds("4");
c1.setPrice("175,000");
c2.setPrice("300,00");
c3.setPrice("250,000");
c1.setType("Terraced");
c2.setType("Bungalow");
c3.setType("Detached");
//Set up the association
p1.ownsDetails(c1);
p2.ownsDetails(c2);
p3.ownsDetails(c3);
//Print result
p1.printDetails();
p2.printDetails();
p3.printDetails();
//Finally quit
System.exit(0);
}
}
任何帮助将不胜感激,谢谢。
Sorry, I'm new to Java and there's probably a very simple answer to this.
At the moment, I'm printing all the results out in individual JOptionPane
s.
I would like to set up a search function to allow me to show certain results based on a given criteria.
Here's my code so far:
public class Main {
public static void main(String[] args) {
//Create new Person objects
Address p1 = new Address("27","Abbey View","Hexham","NE46 1EQ");
Address p2 = new Address("15", "Chirdon Crescent", "Hexham", "NE46 1LE");
Address p3 = new Address("6", "Causey Brae", "Hexham", "NE46 1DB");
Details c1 = new Details();
Details c2 = new Details();
Details c3 = new Details();
//Send some messages to the objects
c1.setBeds("3 ");
c2.setBeds("6");
c3.setBeds("4");
c1.setPrice("175,000");
c2.setPrice("300,00");
c3.setPrice("250,000");
c1.setType("Terraced");
c2.setType("Bungalow");
c3.setType("Detached");
//Set up the association
p1.ownsDetails(c1);
p2.ownsDetails(c2);
p3.ownsDetails(c3);
//Print result
p1.printDetails();
p2.printDetails();
p3.printDetails();
//Finally quit
System.exit(0);
}
}
Any help would be appreciated, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会发现拥有一个数组比使用三个单独的变量更有帮助:
现在可以编写一个循环来检查每个变量:
Instead of having three separate variables, you may find it helpful to have an array:
Now it's possible to write a loop to examine each one:
是的,您可以创建一个自定义
JDialog
或JFrame
,它具有多个输入字段和由于搜索而显示的多个结果。理想情况下,您的对象需要位于列表或数组中才能进行搜索,
这样你就可以遍历所有这些并找到匹配项。
Yes, you can create a custom
JDialog
orJFrame
that has multiple input fields and multiple results that appear due to searching.Ideally, your objects would need to be in a list or array to do the searching,
so that you can loop over all of them and find matches.