4

这大概是个初级问题。但是,我已经阅读了面向绝对初学者的 Java 编程的第 5 章,并进入了挑战部分。我不太明白这个问题。

问题问:

“创建一个 Holder 类,以便它维护一个对象列表,可能由字符串表示,您可以使用它的方法添加或从中获取。提示:尝试覆盖 Vector 类。”

我查看了int [] [] convert --to--> Vector<Vector<Double>>页面,并在最后一个响应中看到了一个简单 Vector 的示例,但我仍然难以理解这个问题。

我认为我的主要问题是理解如何覆盖 Holder 类。

这个问题的答案可能有助于许多新的 Java 程序员理解向量和覆盖。

供您参考:
第二个问题指出:

“创建 Holder 类的子类。发挥你的想象力。考虑一个可以存放食物并保持冷藏的 Refrigerator 类,或者一个存放钱的 Wallet。尝试覆盖 Holder 类中定义的方法。尝试创建一些重载方法.

这是完整的问题,我将在本章中添加一些示例,以便您更好地了解我的问题所在。我不想要编码答案(除非必要),只是用更基本的术语解释这个问题。

这些是本章中提供的示例:这些是我必须纳入我的答案的技巧吗?

简单类定义:

public class SimpleCardDeck {
String[] cards = {“2C”, “3C”, “4C”, “5C”, “6C”, “7C”, “8C”,
“9C”, “10C”, “JC”, “QC”, “KC”, “AC”,
“2D”, “3D”, “4D”, “5D”, “6D”, “7D”, “8D”,
“9D”, “10D”, “JD”, “QD”, “KD”, “AD”,
“2H”, “3H”, “4H”, “5H”, “6H”, “7H”, “8H”,
“9H”, “10H”, “JH”, “QH”, “KH”, “AH”,
“2S”, “3S”, “4S”, “5S”, “6S”, “7S”, “8S”,
“9S”, “10S”, “JS”, “QS”, “KS”, “AS”};
public void list() {
for (int c=0; c < cards.length; c++) {
System.out.print(cards[c] + “ “);
}
}
}

创建和测试对象:

public class SimpleCardDeckTest {
public static void main(String args[]) {
SimpleCardDeck deck = new SimpleCardDeck();
System.out.println(deck.cards[0]);
System.out.println(deck.cards[10]);
System.out.println(deck.cards[51]);
deck.list();
}
}

一个类的例子:

public class Automobile {
public static final String DEFAULT_COLOR = “white”;
public String name;
public boolean running;
public String color;
public int numMiles;
public Automobile() {
this(false, DEFAULT_COLOR, 0);
}
public Automobile(boolean running, String color, int numMiles) {
this.running = running;
this.color = color;
this.numMiles = numMiles;
name = null;
}
public void start() {
if (running) {
System.out.println(“Can’t start, already running.”);
}
else {
running = true;
System.out.println(“The automobile has been started.”);
}
}
public void shutOff() {
if (!running) {
System.out.println(“Can’t shut off, not running.”);
}
else {
running = false;
System.out.println(“The automobile has been shut off.”);
}
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void drive() {
if (running) {
numMiles += 10;
System.out.println(“You have driven 10 miles”);
}
else {
System.out.println(“You need to start the automobile first.”);
}
}
public int getNumMiles() {
return numMiles;
}
public String toString() {
String str;
str = “name = “ + name
+ “, running = “ + running
+ “, color = “ + color
+ “, numMiles = “ + numMiles;
return str;
}
}

类测试示例:

public class AutomobileTest {
public static void main(String args[]) {
Automobile auto1 = new Automobile();
System.out.println(“Auto 1: “ + auto1.toString());
Automobile auto2 = new Automobile(true, “green”, 37000);
auto2.name = “INGRID”;
System.out.println(“Auto 2: “ + auto2.toString());
System.out.println(“Driving Auto 1...”);
auto1.drive();
System.out.println(“Driving Auto 2...”);
auto2.drive();
System.out.println(“Starting Auto 1...”);
auto1.start();
System.out.println(“Starting Auto 2...”);
auto2.start();
System.out.println(“Giving Auto 1 a paint job...”);
auto1.setColor(“red”);
System.out.println(“Auto 1 is now “ + auto1.getColor());
System.out.println(“Renaming Auto 1...”);
auto1.name = “CHRISTINE”;
System.out.println(“Auto 1 is named “ + auto1.name);
System.out.println(“Shutting off Auto 2...”);
auto2.shutOff();
System.out.println(“Shutting off Auto 2 AGAIN...”);
auto2.shutOff();
System.out.println(“Auto 1: “ + auto1.toString());
System.out.println(“Auto 2: “ + auto2.toString());
}
}

汽车子类:

public class BigTruck extends Automobile {

protected boolean trailer;
public BigTruck() {
this(false, DEFAULT_COLOR, 0);
}
public BigTruck(boolean running, String color, int numMiles) {
super(running, color, numMiles);
trailer = false;
}
public void attachTrailer() {
if (trailer) {
System.out.println(“There is already a trailer attached.”);
}
else {
trailer = true;
System.out.println(“Attached a trailer.”);
}
}
public void detachTrailer() {
if (trailer) {
trailer = false;
System.out.println(“Detached the trailer.”);
}
else {
System.out.println(“There is no trailer attached.”);
}
}
public void haul() {
if (trailer) {
drive();
}
else {
System.out.println(“There is nothing to haul.”);
}
}
public String toString() {
String str = super.toString();
str += “, trailer = “ + trailer;
return str;
}
}

子类测试:

public class BigTruckTest {
public static void main(String args[]) {
BigTruck truck = new BigTruck();
System.out.println(truck);
System.out.println(“Starting...”);
truck.start();
System.out.println(“Driving...”);
truck.drive();
System.out.println(“Attaching Trailer...”);
truck.attachTrailer();
System.out.println(“Hauling...”);
truck.haul();
System.out.println(“Detaching trailer...”);
truck.detachTrailer();
System.out.println(“Shutting off...”);
truck.shutOff();
System.out.println(“Painting...”);
truck.setColor(“black”);
System.out.println(truck);
}
}

矢量维克多:

import java.util.Vector;
public class VectorVictor {
public static void main(String args[]) {
Vector v = new Vector(5, 5);
System.out.println(“size, capacity”);
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Fuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Wuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“was”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“a”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“bear”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Fuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Wuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“had”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“no”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“hair”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Fuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Wuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“wasn’t”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“fuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“was”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“he”));
System.out.println(v.size() + “, “ + v.capacity());
v.trimToSize();
System.out.println(v.size() + “, “ + v.capacity());
String[] str = new String[v.size()];
v.copyInto(str);
for (int s=0; s < str.length; s++) {
System.out.print(str[s] + “ “);
}
}
}
4

2 回答 2

4

因此,鉴于这本书已有 10 多年的历史,我觉得术语可能有点过时了。我怀疑他们希望您将 Vector 类子类化或创建一个类,该类是 Vector 的包装器(即:具有一个 Vector 作为成员的类)。泛型使您的书中使用的 Vector 类变得无关紧要。我相信在 2004 年 Java 中添加了泛型。请参阅:Java 泛型教程

我还强烈建议您选择一本新的 Java 书籍来学习。Java 编程语言自 2002 年以来发生了相当大的变化,学习这种过时的语言版本并没有真正让自己受益。

我强烈建议您查看 Java 教程。它们是介绍一般编程和 Java 语言的绝佳方式:Java 教程

于 2012-08-22T21:56:25.940 回答
1

我认为这个问题的目的是帮助您习惯继承和泛型的概念。同时,“取自”措辞并不是最合适的措辞,这让我认为他们不想强调与 Collections/Containers 相关的问题方面。

问题的第二部分似乎强调了泛型的概念,因为您应该使用实例化的泛型类型(FoodItem,Money)扩展 Holder “合格”

     public class Holder<T> extends Vector<T>{

           public addTo(T item){
               super.add(item);
           }

           public takeFrom(T item){
                super.remove(item);
           }
     }


     public class Refrigerator extends Holder<FoodItem>{
     }

     public class Wallet extends Holder<Money>{
     }
于 2012-08-22T21:51:27.883 回答