java设计设计一个书包类(SchoolBag)及行李包类(Luggage),书包只能放书(Book),
答案:1 悬赏:80 手机版
解决时间 2021-11-24 11:43
- 提问者网友:抽煙菂渘情少年
- 2021-11-23 23:03
java设计设计一个书包类(SchoolBag)及行李包类(Luggage),书包只能放书(Book),
最佳答案
- 五星知识达人网友:低血压的长颈鹿
- 2021-11-23 23:52
图 呢追答大致结构
package bagstuff.bag;
import bagstuff.stuff.Stuff;
import java.util.ArrayList;
import java.util.List;
public class Bag {
private List stuffs = new ArrayList();
public void putStuff(Stuff stuff) {
stuffs.add(stuff);
System.out.println("包里面放进了一个东西:" + stuff);
}
public void getStuff() {
for (Stuff stuff : stuffs) {
System.out.println("从包里面拿出了一个东西:" + stuff);
}
}
}
---------------------------
package bagstuff.bag;
import bagstuff.stuff.Cloth;
public class Luggage extends Bag {
//只能放衣服
public void putCloth(Cloth cloth) {
super.putStuff(cloth);
}
}
--------------------------
package bagstuff.bag;
import bagstuff.stuff.Book;
public class SchoolBag extends Bag {
//只能放数
public void putCloth(Book book) {
super.putStuff(book);
}
}package bagstuff.stuff;public class Book extends Stuff { private Long wordsNumber; public Book(String name) { super(name); } public Long getWordsNumber() { return wordsNumber; } public void setWordsNumber(Long wordsNumber) { this.wordsNumber = wordsNumber; }}------------------------package bagstuff.stuff;public class Cloth extends Stuff { private Integer level; public Cloth(String name) { super(name); } public Integer getLevel() { return level; } public void setLevel(Integer level) { this.level = level; }}-------------------------package bagstuff.stuff;public class Stuff { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Stuff(String name) { this.name = name; } @Override public String toString() { return "Stuff{" + "name='" + name + '\'' + '}'; }}--------------------------------package bagstuff;import bagstuff.bag.Bag;import bagstuff.bag.Luggage;import bagstuff.bag.SchoolBag;import bagstuff.stuff.Book;import bagstuff.stuff.Clot--------------------------------package bagstuff;import bagstuff.bag.Bag;import bagstuff.bag.Luggage;import bagstuff.bag.SchoolBag;import bagstuff.stuff.Book;import bagstuff.stuff.Cloth;import bagstuff.stuff.Stuff;public class MainTest { public static void main(String[] args) { // 1)创建Stuff数组 Stuff[] stuffs = new Stuff[20]; Bag schoolBag = new SchoolBag(); Bag luggage = new Luggage(); for (int i = 0; i < 9; i++) { stuffs[i] = new Book("第" + (i + 1) + "本书"); //2)书包putStuff操作 schoolBag.putStuff(stuffs[i]); } //书包getStuff操作 schoolBag.getStuff(); for (int i = 9; i < 20; i++) { stuffs[i] = new Cloth("第" + (i + 1) + "件衣服"); //2)行李包putStuff操作 luggage.putStuff(stuffs[i]); } //行李包getStuff操作 luggage.getStuff(); //3)输出所有stuff for (Stuff stuff : stuffs) { System.out.println(stuff); } }}
package bagstuff.bag;
import bagstuff.stuff.Stuff;
import java.util.ArrayList;
import java.util.List;
public class Bag {
private List
public void putStuff(Stuff stuff) {
stuffs.add(stuff);
System.out.println("包里面放进了一个东西:" + stuff);
}
public void getStuff() {
for (Stuff stuff : stuffs) {
System.out.println("从包里面拿出了一个东西:" + stuff);
}
}
}
---------------------------
package bagstuff.bag;
import bagstuff.stuff.Cloth;
public class Luggage extends Bag {
//只能放衣服
public void putCloth(Cloth cloth) {
super.putStuff(cloth);
}
}
--------------------------
package bagstuff.bag;
import bagstuff.stuff.Book;
public class SchoolBag extends Bag {
//只能放数
public void putCloth(Book book) {
super.putStuff(book);
}
}package bagstuff.stuff;public class Book extends Stuff { private Long wordsNumber; public Book(String name) { super(name); } public Long getWordsNumber() { return wordsNumber; } public void setWordsNumber(Long wordsNumber) { this.wordsNumber = wordsNumber; }}------------------------package bagstuff.stuff;public class Cloth extends Stuff { private Integer level; public Cloth(String name) { super(name); } public Integer getLevel() { return level; } public void setLevel(Integer level) { this.level = level; }}-------------------------package bagstuff.stuff;public class Stuff { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Stuff(String name) { this.name = name; } @Override public String toString() { return "Stuff{" + "name='" + name + '\'' + '}'; }}--------------------------------package bagstuff;import bagstuff.bag.Bag;import bagstuff.bag.Luggage;import bagstuff.bag.SchoolBag;import bagstuff.stuff.Book;import bagstuff.stuff.Clot--------------------------------package bagstuff;import bagstuff.bag.Bag;import bagstuff.bag.Luggage;import bagstuff.bag.SchoolBag;import bagstuff.stuff.Book;import bagstuff.stuff.Cloth;import bagstuff.stuff.Stuff;public class MainTest { public static void main(String[] args) { // 1)创建Stuff数组 Stuff[] stuffs = new Stuff[20]; Bag schoolBag = new SchoolBag(); Bag luggage = new Luggage(); for (int i = 0; i < 9; i++) { stuffs[i] = new Book("第" + (i + 1) + "本书"); //2)书包putStuff操作 schoolBag.putStuff(stuffs[i]); } //书包getStuff操作 schoolBag.getStuff(); for (int i = 9; i < 20; i++) { stuffs[i] = new Cloth("第" + (i + 1) + "件衣服"); //2)行李包putStuff操作 luggage.putStuff(stuffs[i]); } //行李包getStuff操作 luggage.getStuff(); //3)输出所有stuff for (Stuff stuff : stuffs) { System.out.println(stuff); } }}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯