packagekongjian;interfaceCloth{publicStringgetName();}classPantsimplementsCloth{privateStringname;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.nam...
package kongjian;
interface Cloth{
public String getName();
}
class Pants implements Cloth{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Pants(String name){
this.setName(name);
}
}
class Shop{
private Cloth[] clothes;
private int foot;
public Shop(int len){
if(len>0){
this.clothes=new Cloth[len];
}else{
this.clothes=new Cloth[1];
}
}
public boolean add(Cloth cloth){
if(this.foot
this.clothes[this.foot]=cloth;
this.foot++;
return true;
}else{
return false;
}
}
public Cloth[] getContent(){
return clothes;
}
public Cloth[] search(String keyword){
Cloth c[]=new Cloth[2];
int f=0;
for(int i=0;i
if(this.clothes[i]!=null){
if(this.clothes[i].getName().indexOf(keyword)!=-1){
c[f]=this.clothes[i];
f++;
}
}
}
return c;
}
}
public class A{
public static void main(String[] args){
Shop shop=new Shop(6);
shop.add(new Pants("背带裤"));
print(shop.search("背带裤"));
}
public static void print(Cloth[] cloths) {
for(int i=0;i
if(cloths[i]!=null){
System.out.println("商品名称:"+cloths[i].getName());
}
}
}
}
展开