创建上衣类
上衣类继承服装类,并满足以下要求:
1、上衣类增加风格的私有属性
2、增加相应的getXxx()和setXxx()方法,增加相应的构造方法。
3、重写父类的输出方法,输出的服装信息要增加风格的说明。
4、增加新的无参方法,根据服装的风格输出:通勤风:适用于日常上下班穿着;学院风:适用于在校学生;百搭:适用于任何场合穿着。
public class Clothes {
}
public class Jacket extends Clothes{
//风格
private String style;
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
@Override
public String toString() {
return "Jacket{" +
"上衣风格为='" + style + '\'' +
'}';
}
public void print(){
if("通勤风".equalsIgnoreCase(style)){
System.out.println("通勤风");
}else if("适用于日常上下班穿着".equalsIgnoreCase(style)){
System.out.println("适用于日常上下班穿着");
}else if("学院风".equalsIgnoreCase(style)){
System.out.println("学院风");
}else if("适用于在校学生".equalsIgnoreCase(style)){
System.out.println("适用于在校学生");
}else if("百搭".equalsIgnoreCase(style)){
System.out.println("百搭");
}else {
System.out.println("适用于任何场合穿着");
}
}
}
import java.util.*;
import java.lang.Math;
public class Main {
static class ClothingDemo {
private String brand;
private String type;
private int price;
private String style;
public ClothingDemo(String brand, String type, int price, String style) {
this.brand = brand;
this.type = type;
this.price = price;
this.style = style;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
@Override
public String toString() {
return "ClothingDemo{" +
"brand='" + brand + '\'' +
", type='" + type + '\'' +
", price='" + price + '\'' +
", style='" + style + '\'' +
'}';
}
}
static class Shangyi extends ClothingDemo {
private String shangyiStyle;
private String brand;
private String type;
private int price;
private String style;
public Shangyi(String brand, String type, int price, String style, String shangyiStyle) {
super(brand, type, price, style);
this.brand = brand;
this.type = type;
this.price = price;
this.style = style;
this.shangyiStyle = shangyiStyle;
}
public String getShangyiStyle() {
return shangyiStyle;
}
public void setShangyiStyle(String shangyiStyle) {
this.shangyiStyle = shangyiStyle;
}
@Override
public String toString() {
return "Shangyi{" +
"brand='" + brand + '\'' +
", type='" + type + '\'' +
", price=" + price +
", style='" + style + '\'' +
", shangyiStyle='" + shangyiStyle + '\'' +
'}';
}
public void chuanda() {
switch (shangyiStyle) {
case "通勤风":
System.out.println("适用于日常上下班穿着");
break;
case "学院风":
System.out.println("适用于在校学生");
break;
case "百搭":
System.out.println("适用于任何场合穿着");
break;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList arrayList = new ArrayList();
Shangyi demo1 = new Shangyi("优衣库", "风衣", 399, "通勤风", "通勤风");
arrayList.add(demo1);
Shangyi demo2 = new Shangyi("优衣库", "卫衣", 199, "百搭风", "百搭");
arrayList.add(demo2);
Shangyi demo3 = new Shangyi("太平鸟", "女装", 1399, "连衣裙", "学院风");
arrayList.add(demo3);
for (Object object : arrayList) {
System.out.println(object.toString());
((Shangyi)object).chuanda();
}
}
}
这个不是很简单吗
服装类也没有说明包含什么方法,那么上衣类怎么重写服装类的输出方法
```
这个就是简单的衣服类,就跟学生类一样,私有的一些属性,get.set方法,连快捷键都有,很简单
工厂设计模式可以帮到你