Java程序语句:如何使计算机类具有可用的打印机列表 [英] Java Program Statement : How to make a computer class having a list of printers available to it

查看:65
本文介绍了Java程序语句:如何使计算机类具有可用的打印机列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java问题语句:如何使计算机类具有可用的打印机列表。

我想有一个Computer类(类似这样的东西),它完全填充问题语句

Java Problem Statement : How to make a computer class having a list of printers available to it.
I want to have a Computer class (something like this) which fullfills the problem statement

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>

    }


}

示例:

计算机名 - 打印机列表

Computername -- List of Printers

HP - HP打印机,三星,佳能。

HP -- Hp Printer, Samsung , Canon .

推荐答案

首先,定义一个Printer类。

First, lets define a Printer class.

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方法来检索值。一旦创建了此类的实例,就不能更改它。

This class has a constructor to define the values, and two getter methods to retrieve the values. Once you create an instance of this class, you can't change it.

这些类型的不可修改类使得调试更简单。

These types of non modifiable classes make debugging simpler.

hashCode和equals方法看起来很可怕。我有Eclipse为我生成这些方法。我们重写这些方法的原因是我们要在 Computer 类中使用它们。

The hashCode and equals methods look pretty scary. I had Eclipse generate those methods for me. The reason that we override these methods is that we're going to use them in the Computer class.

现在,我们定义 Computer 类。

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;
    }

}

构造函数使用电脑。一旦构建实例,就无法更改计算机的名称。

The constructor takes the name of the computer. Once an instance is constructed, there's no way to change the name of the computer.

打印机已添加到计算机中或从计算机中删除。已定义两种方法将打印机添加到计算机,并从计算机中删除打印机。

Printers are added to and removed from a computer. Two methods have been defined to add a printer to the computer, and remove a printer from a computer.

remove方法从<$ c中删除打印机的所有实例

The remove method removes all instances of the printer from the List, in case more than one was entered.

获取打印机列表的方法返回<$ c>的实例

The method that gets the printer list returns an instance of the List. The calling program can change the contents of this List. If you want to make sure that the calling program can't change the contents of the list, you make a deep copy of the List.

要进行深度复制,请为打印机类写一个复制方法。

To make a deep copy, you write a copy method for the Printer class.

public Printer copy() {
    return new Printer(companyName, printerName);
}  

由于字符串是不可更改的,因此我们不必复制字符串。如果我们有可变的价值,我们也必须制作一份副本。这就是为什么这被称为深拷贝。它可能很难得到一切正确复制第一次。

Since strings are unchangeable, we don't have to make a copy of the strings. If we had changeable values, we would have to make a copy of them as well. That's why this is called a deep copy. It can be difficult to get everything copied correctly the first time. That's what testing is for.

然后在 getPrinterNames 方法的循环中调用此复制方法电脑

Then you call this copy method in a loop in the getPrinterNames method of the Computer class.

public List<Printer> getPrinterNames() {
    List<Printer> list = new ArrayList<Printer>();

    for (Printer printer : printerNames) {
        list.add(printer.copy());
    }

    return list;
}

这篇关于Java程序语句:如何使计算机类具有可用的打印机列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆