Java挑战中列出的Java挑战绝对初学者

时间:2012-08-22 21:34:21

标签: java oop vector overloading override

这可能是一个基本问题。但是,我已经完成了阅读Absolute Beginner的Java编程的第5章,并且已经接近挑战部分。我不太明白这个问题。

问题是:

  

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

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

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

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

供您参考:
第二个问题是:

  

“创建Holder类的子类。运用你的想象力。考虑一个持有食物并保持冷藏的冰箱类,或者拿钱的钱包。尝试重写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);
}
}

Vector Victor:

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] + “ “);
}
}
}

2 个答案:

答案 0 :(得分:4)

因此,鉴于本书已超过10年,我觉得这个术语可能有点过时了。我怀疑他们希望你继承Vector类或者创建一个类,它是一个Vector的包装器(即:一个有Vector作为成员的类)。泛型使得在你的书中使用的Vector类无关紧要。使用Java添加了泛型我相信2004年。请参阅:Java Generics Tutorial

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

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

答案 1 :(得分:1)

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

问题的第二部分似乎强调泛型的概念,在某种意义上你应该用实例化的泛型类型扩展Holder“qualified”(FoodItem,Money)

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