?继承运用的题目 。
import java.util.Arrays;
public class HelloIDEA {
public static void main(String[] args) {
jiage cs=new jiage();//调用不同类使用不同的方法
cs.add(new cosmetic("0数据结构","进口",1999));
cs.add(new cosmetic("1数据结构","国产",999));
cs.add(new cosmetic("2数据结构","进口",3999));
cs.add(new cosmetic("3数据结构","进口",6999));
cs.add(new cosmetic("4数据结构","国产",5999));
cs.print1();
System.out.println("-------------------------------------------------------------------");
cs.print1();
System.out.println("---------------------------------------------------------------");
cs.print1();
}
}
//进口化妆品的输出
class jiage extends cosmeticManager{//通过继承筛选
public void print1() {
cosmetic[] llo = null;
llo = Arrays.copyOf(cs, s);
System.out.println("进口的有");
for (int i = 0; i < s; i++) {
if ("进口" ==llo[i].getType()){
System.out.println(llo[i].print());
}
}
}
}
//价格排序
class paixv extends cosmeticManager{//继承排序
@Override
public void print1() {
cosmetic[] llo=null;
llo=Arrays.copyOf(cs,s);
cosmetic tecp;
for (int i=0;i<s;i++){
for (int j=0;j<s-1;j++){
if (llo[j].getPrice()>llo[j+1].getPrice()){
tecp=llo[j];
llo[j]=llo[j+1];
llo[j+1]=tecp;
}
}
}
for (cosmetic z:llo){
System.out.println(z.print());
}
}
}
class cosmeticManager{
protected cosmetic[] cs=new cosmetic[5];
protected int s=0;
public cosmeticManager(){}
public cosmeticManager(int ccl){
if (ccl>0) {
cs = new cosmetic[ccl];
}else {
cs=new cosmetic[3];
}
}
public void add(cosmetic llo){
if (s> cs.length){
s= cs.length*3/2+1;
}
cs[s]=llo;
s++;
}
public void print1(){
for (int i=0;i<s;i++){
System.out.println(cs[i].print());
}
}
}
//化妆品类
class cosmetic{
private String name;
private String type;
private int price;
public cosmetic(){}
public cosmetic(String name,String type,int price){
this.name=name;
this.type=type;
this.price=price;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setPrice(int price) {
this.price = price;
}
public int getPrice() {
return price;
}
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public String print(){
return "产品名称:"+name+" 生产地方:"+type+" 价格:"+price;
}
}
|