Java快速实例化
在这段代码中,如何在构造函数中实例化DictionaryADT dictionary?另外,如果有人能帮助使用迭代器,那就太酷了。最后,如果你能帮助打印功能,那就太好了Java快速实例化,java,iterator,instantiation,Java,Iterator,Instantiation,在这段代码中,如何在构造函数中实例化DictionaryADT dictionary?另外,如果有人能帮助使用迭代器,那就太酷了。最后,如果你能帮助打印功能,那就太好了 import data_structures.*; import java.util.Iterator; public class ProductLookup { DictionaryADT<String,StockItem> dictionary; private int maxSize; // Const
import data_structures.*;
import java.util.Iterator;
public class ProductLookup {
DictionaryADT<String,StockItem> dictionary;
private int maxSize;
// Constructor. There is no argument-less constructor, or default size
public ProductLookup(int maxSize) {
this.maxSize = maxSize;
}
// Adds a new StockItem to the dictionary
public void addItem(String SKU, StockItem item) {
dictionary.insert(SKU,item);
}
// Returns the StockItem associated with the given SKU, if it is
// in the ProductLookup, null if it is not.
public StockItem getItem(String SKU) {
if (SKU == null)
return null;
return dictionary.getValue(SKU);
}
// Returns the retail price associated with the given SKU value.
// -.01 if the item is not in the dictionary
public float getRetail(String SKU) {
if (!dictionary.contains(SKU))
return (float) -.01;
return getItem(SKU).getRetail();
}
public float getCost(String SKU) {
if (!dictionary.contains(SKU))
return (float) -.01;
return getItem(SKU).getCost();
}
// Returns the description of the item, null if not in the dictionary.
public String getDescription(String SKU) {
if (!dictionary.contains(SKU))
return null;
return getItem(SKU).getDescription();
}
// Deletes the StockItem associated with the SKU if it is
// in the ProductLookup. Returns true if it was found and
// deleted, otherwise false.
public boolean deleteItem(String SKU) {
if (SKU == null)
return false;
return dictionary.remove(SKU);
}
// Prints a directory of all StockItems with their associated
// price, in sorted order (ordered by SKU).
public void printAll() {
Iterator<StockItem> iterator = values();
while (iterator.hasNext())
System.out.println(iterator.next().toString());
}
// Prints a directory of all StockItems from the given vendor,
// in sorted order (ordered by SKU).
public void print(String vendor) {
Iterator<StockItem> iterator = values();
if (dictionary.getItem(SKU).getVendor() == vendor)
System.out.println(tmp.toString());
}
// An iterator of the SKU keys.
public Iterator<String> keys() {
return new ;
}
// An iterator of the StockItem values.
public Iterator<StockItem> values() {
return null;
}
}
导入数据结构。*;
导入java.util.Iterator;
公共类产品查找{
字典雅特字典;
私有int-maxSize;
//构造函数。没有无参数构造函数或默认大小
公共产品查找(int-maxSize){
this.maxSize=maxSize;
}
//向字典中添加新的StockItem
公共无效附加项(字符串SKU、库存项){
字典。插入(SKU,项目);
}
//返回与给定SKU关联的库存项(如果是
//在ProductLookup中,如果不是,则为null。
public StockItem getItem(字符串SKU){
如果(SKU==null)
返回null;
返回dictionary.getValue(SKU);
}
//返回与给定SKU值关联的零售价格。
//-.01如果该项不在词典中
公共浮动零售(字符串SKU){
如果(!dictionary.contains(SKU))
返回(浮动)-.01;
返回getItem(SKU).getRetail();
}
公共浮动成本(字符串SKU){
如果(!dictionary.contains(SKU))
返回(浮动)-.01;
返回getItem(SKU).getCost();
}
//返回项的描述,如果不在字典中,则返回null。
公共字符串getDescription(字符串SKU){
如果(!dictionary.contains(SKU))
返回null;
返回getItem(SKU).getDescription();
}
//删除与SKU关联的库存项(如果是)
//在ProductLookup中。如果已找到并
//已删除,否则为false。
公共布尔删除项(字符串SKU){
如果(SKU==null)
返回false;
返回字典。删除(SKU);
}
//打印所有库存项及其关联项的目录
//价格,按排序顺序(按SKU排序)。
public void printAll(){
迭代器迭代器=值();
while(iterator.hasNext())
System.out.println(iterator.next().toString());
}
//打印给定供应商的所有库存项目的目录,
//按排序顺序(按SKU排序)。
公共作废打印(字符串供应商){
迭代器迭代器=值();
if(dictionary.getItem(SKU).getVendor()==供应商)
System.out.println(tmp.toString());
}
//SKU密钥的迭代器。
公共迭代器密钥(){
归还新的;
}
//StockItem值的迭代器。
公共迭代器值(){
返回null;
}
}
为什么它会与您使用maxSize的方式有所不同
public ProductLookup(int maxSize, DictionaryADT<String,StockItem> dictionary) {
this.dictionary = dictionary;
this(maxSize);
}
// Constructor. There is no argument-less constructor, or default size
public ProductLookup(int maxSize) {
this.maxSize = maxSize;
}
公共产品查找(int-maxSize,DictionaryADT dictionary){
这本字典=字典;
这个(最大尺寸);
}
//构造器。没有无参数构造函数或默认大小
公共产品查找(int-maxSize){
this.maxSize=maxSize;
}
是的,我想这样做,但是我不能更改这些类的函数,所以它必须是public ProductLookup(int maxSize)@FlameArc出于好奇,为什么你不能更改构造函数?这就是赋值的编写方式编写这个新构造函数并调用它(maxSize)我不明白这个类会是私有的,会从上面的公共类调用maxSize吗?