Java程序语句:如何使计算机类具有可用的打印机列表
Java问题陈述:如何使计算机类具有可用的打印机列表。Java程序语句:如何使计算机类具有可用的打印机列表,java,collections,Java,Collections,Java问题陈述:如何使计算机类具有可用的打印机列表。 我想有一个计算机类(类似这样的)来填满问题陈述 public class Computers{ String computername; //printerlist public static ArrayList<String> printers=new ArrayList<String>(); // computername-Printerlist public Map<
我想有一个计算机类(类似这样的)来填满问题陈述
public class Computers{
String computername;
//printerlist
public static ArrayList<String> printers=new ArrayList<String>();
// computername-Printerlist
public Map<String,ArrayList<String>> printerDB=newHashMap<String,ArrayList<String>>();
public Computers(String computername){
this.computername=computername;
// now what to put here so that each computer having a
// different computer name will have a list of printers available to it>
}
}
公共类计算机{
字符串名称;
//印刷工
公共静态ArrayList打印机=新建ArrayList();
//计算机名打印列表
publicmap printerDB=newHashMap();
公用计算机(字符串计算机名){
this.computername=computername;
//现在把什么放在这里,让每台计算机都有一个
//不同的计算机名将有一个可用打印机列表>
}
}
例如:
Computername--打印机列表
惠普--惠普打印机,三星,佳能 创建您的计算机类,并在计算机类中维护计算机对象列表。 计算机课是这样的
public class Computer{
private String computername;
private ArrayList<String> printernames;
public Computers(String computername,List<String> printernames){
this.computername=computername;
this.printernames=printernames;
}
//setters //gettters //
}
公共类计算机{
私有字符串名称;
私人ArrayList printernames;
公用计算机(字符串计算机名、列表打印机名){
this.computername=computername;
this.printernames=printernames;
}
//setters//getter//
}
用法:
String compname ="Apple";
List<String> printernames = new Arraylist<String>();
printernames.add("AplrPrinter1");
printernames.add("AplrPrinter2");
printernames.add("AplrPrinter3");
Computers comp = new Computers(compname,printernames);
String compname=“苹果”;
List printernames=new Arraylist();
printernames.add(“APLRInter1”);
printernames.add(“APLRInter2”);
printernames.添加(“APLRPINT3”);
Computers comp=新计算机(compname,printernames);
首先,让我们定义一个打印机类
package com.ggl.modeltest;
public class Printer {
private String companyName;
private String printerName;
public Printer(String companyName, String printerName) {
this.companyName = companyName;
this.printerName = printerName;
}
public String getCompanyName() {
return companyName;
}
public String getPrinterName() {
return printerName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((companyName == null) ? 0 : companyName.hashCode());
result = prime * result
+ ((printerName == null) ? 0 : printerName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Printer other = (Printer) obj;
if (companyName == null) {
if (other.companyName != null)
return false;
} else if (!companyName.equals(other.companyName))
return false;
if (printerName == null) {
if (other.printerName != null)
return false;
} else if (!printerName.equals(other.printerName))
return false;
return true;
}
public Printer copy() {
return new Printer(companyName, printerName);
}
}
这个类有一个构造函数来定义值,还有两个getter方法来检索值。一旦创建了此类的实例,就无法对其进行更改
这些类型的不可修改类使调试更简单
hashCode和equals方法看起来非常可怕。我让Eclipse为我生成这些方法。我们重写这些方法的原因是我们将在计算机
类中使用它们
package com.ggl.modeltest;
import java.util.ArrayList;
import java.util.List;
public class Computer {
private List<Printer> printerNames;
private String computerName;
public Computer(String computerName) {
this.computerName = computerName;
this.printerNames = new ArrayList<Printer>();
}
public void addPrinter(Printer printerName) {
this.printerNames.add(printerName);
}
public void removePrinter(Printer printerName) {
for (int i = printerNames.size() - 1; i >= 0; i--) {
if (printerNames.get(i).equals(printerName)) {
printerNames.remove(i);
}
}
}
public List<Printer> getPrinterNames() {
return printerNames;
}
public String getComputerName() {
return computerName;
}
}
public Printer copy() {
return new Printer(companyName, printerName);
}
public List<Printer> getPrinterNames() {
List<Printer> list = new ArrayList<Printer>();
for (Printer printer : printerNames) {
list.add(printer.copy());
}
return list;
}
现在,我们定义计算机
类
package com.ggl.modeltest;
import java.util.ArrayList;
import java.util.List;
public class Computer {
private List<Printer> printerNames;
private String computerName;
public Computer(String computerName) {
this.computerName = computerName;
this.printerNames = new ArrayList<Printer>();
}
public void addPrinter(Printer printerName) {
this.printerNames.add(printerName);
}
public void removePrinter(Printer printerName) {
for (int i = printerNames.size() - 1; i >= 0; i--) {
if (printerNames.get(i).equals(printerName)) {
printerNames.remove(i);
}
}
}
public List<Printer> getPrinterNames() {
return printerNames;
}
public String getComputerName() {
return computerName;
}
}
public Printer copy() {
return new Printer(companyName, printerName);
}
public List<Printer> getPrinterNames() {
List<Printer> list = new ArrayList<Printer>();
for (Printer printer : printerNames) {
list.add(printer.copy());
}
return list;
}
因为字符串是不可更改的,所以我们不必复制字符串。如果我们有可变的值,我们也必须复制它们。这就是为什么这被称为深度复制。要在第一次正确地复制所有内容可能很困难。这就是测试的目的
然后在Computer
类的getPrinterNames
方法中的循环中调用这个复制方法
package com.ggl.modeltest;
import java.util.ArrayList;
import java.util.List;
public class Computer {
private List<Printer> printerNames;
private String computerName;
public Computer(String computerName) {
this.computerName = computerName;
this.printerNames = new ArrayList<Printer>();
}
public void addPrinter(Printer printerName) {
this.printerNames.add(printerName);
}
public void removePrinter(Printer printerName) {
for (int i = printerNames.size() - 1; i >= 0; i--) {
if (printerNames.get(i).equals(printerName)) {
printerNames.remove(i);
}
}
}
public List<Printer> getPrinterNames() {
return printerNames;
}
public String getComputerName() {
return computerName;
}
}
public Printer copy() {
return new Printer(companyName, printerName);
}
public List<Printer> getPrinterNames() {
List<Printer> list = new ArrayList<Printer>();
for (Printer printer : printerNames) {
list.add(printer.copy());
}
return list;
}
public List getPrinterNames(){
列表=新的ArrayList();
用于(打印机:打印机名称){
list.add(printer.copy());
}
退货清单;
}
您到底想要什么?还有这个语句:this.computername=computername;没有道理。我同意特哈斯的观点——很难理解你到底想要什么。您的计算机
类已经有一个可用的“打印机列表”——打印机
字段。您是否在询问如何在构造函数中填充此内容?您是否询问其他类如何查询此字段?你是在问“有一份清单”的其他含义吗?我真的没有得到你想要的帮助。是的..一个计算机名“苹果”怎么能有自己的打印机列表,一个计算机名“惠普”怎么能有自己的打印机列表。。。见下面BaadShaah的回答…他很接近,但现在没有回答。。bdw这个。computername=computername
。有道理!!this.computername=name@user2416728抱歉输入错误。请编辑。最好创建一个类打印机并向其添加属性。纯OOP:)如打印机名称、公司、纸张..等等。这些打印机是否仅对计算机名“Apple”可用?或者每个计算机对象都可以访问这些打印机?嗯,我不明白。。。只有名为“苹果”的计算机才能访问这些打印机。。。你的代码能做到这一点吗?好的答案似乎不错,但无法理解“hash”和“equals”方法。。。此外,问题使用的是ArrayList
而不是List
。您可能需要考虑一下…@Altair规则:哈希方法为公司名称和打印机名称的组合生成一个有点唯一的数字。你可以使用任何你想要的算法。我们的想法是为公司名称和打印机名称的组合提供尽可能唯一的一组数字。equals方法确定打印机的两个实例是否相等。大多数代码检查实例是否为打印机实例,并且不为null。我创建了一个Printer类,因为其中一个答案中有一条注释。