Java挑战中列出的Java挑战绝对初学者
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java挑战中列出的Java挑战绝对初学者,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7824字,纯文字阅读大概需要12分钟。
内容图文
![Java挑战中列出的Java挑战绝对初学者](/upload/InfoBanner/zyjiaocheng/800/be07efbf65274deb9963233984d5cbc0.jpg)
这可能是一个基本问题.但是,我已经完成了阅读Absolute Beginner的Java编程的第5章,并且已经接近挑战部分.我不太明白这个问题.
问题是:
“Create a Holder class so that it maintains a list of objects, possibly represented by Strings, which you can add to or take from by using its methods. Hint: try overriding the Vector class.”
我查看了int [] [] convert –to–> Vector
我认为我的主要问题是理解如何覆盖Holder类.
这个问题的答案可能有助于许多新的Java程序员理解向量和覆盖.
供您参考:
第二个问题表明:
“Create a subclass of the Holder class. Use your imagination. Consider a Refrigerator class that holds food and keeps it cold, or a Wallet that holds money, perhaps. Try overriding methods defined in the Holder class. Try creating some overloaded methods as well.
这是一个完整的问题,我将在本章中添加一些示例实例,以便您更好地了解我的问题所在.我不想要编码答案(除非必要),只是用更基本的术语解释这个问题.
这些是本章提供的例子:这些是我必须在我的答案中加入的技巧吗?
简单的类定义:
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] + “ “);
}
}
}
解决方法:
我认为这个问题的目的是帮助你习惯继承和泛型的概念.
与此同时,“取自”措辞并不是最合适的措辞,这使我认为他们不想强调问题的集合/容器相关方面.
问题的第二部分似乎强调了泛型的概念,从某种意义上说,你应该用实例化的泛型类型扩展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>{
}
内容总结
以上是互联网集市为您收集整理的Java挑战中列出的Java挑战绝对初学者全部内容,希望文章能够帮你解决Java挑战中列出的Java挑战绝对初学者所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。