}/** *测试类 * @author Administrator * */ public class FruitTest { public static void main(String[] args) { //水果箱子 FruitBox fruitBox = new FruitBox(); //苹果 Fruit a = new Apple("23", "红色"); //苹果 Fruit d = new Apple("29", "青色"); //橘子 Fruit b = new Orange("24","黄色"); //梨 Fruit c = new Pear("28","黄色"); //把水果都放入箱子中 fruitBox.addFruit(a).addFruit(b).addFruit(c).addFruit(d);
//把箱子中的水果拿出来
ArrayList<Fruit> fruitList = fruitBox.getFruit(); for(Fruit fruit :fruitList) { System.out.println(fruit); }
} } lz 这是我写的 你参照下吧
package com.djk.celue;import java.util.ArrayList;/** * 水果接口 * @author Administrator * */ interface Fruit { String toString(); } /** * 苹果 * @author Administrator * */ class Apple implements Fruit { private StringBuffer sb = new StringBuffer(); /** * 重量 */ private String weight;
}/** *测试类 * @author Administrator * */ public class FruitTest { public static void main(String[] args) { //水果箱子 FruitBox fruitBox = new FruitBox(); //苹果 Fruit a = new Apple("23", "红色"); //苹果 Fruit d = new Apple("29", "青色"); //橘子 Fruit b = new Orange("24","黄色"); //梨 Fruit c = new Pear("28","黄色"); //把水果都放入箱子中 fruitBox.addFruit(a).addFruit(b).addFruit(c).addFruit(d);
//把箱子中的水果拿出来
ArrayList<Fruit> fruitList = fruitBox.getFruit(); for(Fruit fruit :fruitList) { System.out.println(fruit); }
} }
package com.djk.celue;import java.util.ArrayList;/** * 水果接口 * @author Administrator * */ interface Fruit { String toString(); } /** * 苹果 * @author Administrator * */ class Apple implements Fruit { private StringBuffer sb = new StringBuffer(); /** * 重量 */ private String weight;
}/** *测试类 * @author Administrator * */ public class FruitTest { public static void main(String[] args) { //水果箱子 FruitBox fruitBox = new FruitBox(); //苹果 Fruit a = new Apple("23", "红色"); //苹果 Fruit d = new Apple("29", "青色"); //橘子 Fruit b = new Orange("24","黄色"); //梨 Fruit c = new Pear("28","黄色"); //把水果都放入箱子中 fruitBox.addFruit(a).addFruit(b).addFruit(c).addFruit(d);
//把箱子中的水果拿出来
ArrayList<Fruit> fruitList = fruitBox.getFruit(); for(Fruit fruit :fruitList) { System.out.println(fruit); }
} }呵呵这样好看点
class Box{ private Fruit fruit; private List<Fruit> fruitList = new ArrayList<Fruit>(); Box(Fruit fruit){ this.fruit = fruit; } public void addFruit(Fruit fruit){} public void getFruit(Fruit fruit){} public String showFruit(Fruit fruit){return fruit.getColor() + fruit.getWeight();} } class Fruit{ private String color; private double weight; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } }
呵呵 楼主我改下代码,这样的话你更方便了,你如果想要添加一种水果比如桃子的话,你要改的代码很少,就只要继承Fruit类然后new一个桃子就OK了其他代码都不用动,程序会自动帮你识别重量,颜色以及名字的 package com.djk.celue;import java.lang.reflect.Field; import java.util.ArrayList;/** * 水果接口 * @author Administrator * */ abstract class Fruit {
private StringBuffer sb = new StringBuffer(); public String toString() { //水果的重量 String weigth = null;
//水果的颜色 String color = null;
//水果的全称包括包名字 String name =null;
//水果的英语名字 String fruitname = null; try { name = this.getClass().getName();
}/** *测试类 * @author Administrator * */ public class FruitTest { public static void main(String[] args) { //水果箱子 FruitBox fruitBox = new FruitBox(); //苹果 Fruit a = new Apple("23", "红色"); //苹果 Fruit d = new Apple("29", "青色"); //橘子 Fruit b = new Orange("24","黄色"); //梨 Fruit c = new Pear("28","黄色"); //把水果都放入箱子中 fruitBox.addFruit(a).addFruit(d).addFruit(b).addFruit(c);
我这个可能不是你想要的,我只是一时兴起,回顾了一下生产者与消费者的问题。 package com.zf.test;import java.util.ArrayList;public class Test7 { public static void main(String[] args) throws Exception{ final FruitBasket basket = new FruitBasket(); final Fruit apple = new Apple("红色", 300); final Fruit pear = new Pear("黄色", 200); for(int i = 0 ; i < 10 ; i++){ //模拟 10 人取 , 10人放
new Thread(new Runnable() { //添加 @Override public void run() { int x = 0 ; while(true) try { basket.addFruit(++x % 2 == 0 ? apple : pear); } catch (Exception e) { e.printStackTrace(); } } }).start();
} }}//果篮 class FruitBasket extends ArrayList<Fruit>{ private static final long serialVersionUID = 1L; private int max = 10 ; public FruitBasket(){}; public FruitBasket( int max ){ this.max = max ; }
public synchronized void addFruit(Fruit fruit) throws Exception{ while(size() >= max){ System.out.println("篮子已经满了,请稍等!"); wait(); } add(fruit); notifyAll(); System.out.println("放入水果成功,当前水果总数:" + size()); } public synchronized Fruit getFruit() throws Exception{ while(size() <= 0){ System.out.println("篮子已经空了,请稍等!"); wait(); } Fruit fruit = remove(0); System.out.println("取出水果成功\t:" + fruit + "\t当前水果总数:" + size()); notifyAll(); return fruit ; }}//抽象父类 abstract class Fruit{ protected String color ; protected double weight ; abstract void setColor(String color) ; abstract String getColor() ; abstract double getWeight(); abstract void setWeight(double weight) ;
}class Apple extends Fruit{ public Apple(){} public Apple(String color , double weight){ this.color = color ; this.weight = weight ; } @Override public void setColor(String color) { this.color = color ; } @Override public String getColor() { return this.color; } @Override public double getWeight() { return this.weight; } @Override public void setWeight(double weight) { this.weight = weight; } @Override public String toString() { return "苹果\t" + super.toString(); } }class Pear extends Fruit{ public Pear(){} public Pear(String color , double weight){ this.color = color ; this.weight = weight ; }
@Override public void setColor(String color) { this.color = color ; } @Override public String getColor() { return this.color; } @Override public double getWeight() { return this.weight; } @Override public void setWeight(double weight) { this.weight = weight; } @Override public String toString() { return "梨子\t" + super.toString(); }}
简化一点,应该为这样 //抽象父类 abstract class Fruit{ private String name ; private String color ; private double weight ; public Fruit(String name){ this.name = name ; } public String getName() { return name; }
public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } @Override public String toString() { return "类型:" + this.name + ",颜色:" + this.color + " , 重量:" + this.weight; }}class Apple extends Fruit{ public Apple(){ super("苹果"); }}class Pear extends Fruit{ public Pear(){ super("梨子"); } }
package fruitBean;import java.util.ArrayList;public class FruitBoxDemo { public static void main(String args[]){ //水果箱子 FruitBox fruitBox = new FruitBox(); Fruit a = new Apple(23,"红色"); Fruit d = new Apple(29,"青色"); Fruit b = new Orange(24,"黄色"); Fruit c = new Pear(28,"黄色"); //把水果都放入箱子中 fruitBox.addFruit(a).addFruit(d).addFruit(b).addFruit(c); //把箱子中的水果拿出来 ArrayList<Fruit> fruitList = fruitBox.getFruit(); for(Fruit fruit:fruitList){ System.out.println(fruit); } } } class Fruit{ private Integer weight; private String color; public Fruit(){
} public Fruit(Integer weight,String color){ this.color = color; this.weight = weight; } public String toString(){ StringBuffer sb = new StringBuffer(); sb.append("这个").append(this.getClass().getName()).append("的颜色是:").append(color).append(" 重量是:").append(weight); return sb.toString(); }
public Integer getWeight() { return weight; } public void setWeight(Integer weight) { this.weight = weight; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }class Apple extends Fruit{ public Apple(Integer weight,String color){ super(weight,color); } }class Orange extends Fruit{ public Orange(Integer weight,String color){ super(weight,color); } }class Pear extends Fruit{ public Pear(Integer weight,String color){ super(weight,color); } }/*水果箱子*/ class FruitBox{ //集合用来装水果的 private ArrayList<Fruit> fruitList = new ArrayList<Fruit>(); //用来放水果 public FruitBox addFruit(Fruit fruit){ fruitList.add(fruit); return this; } //用来取水果的集合 public ArrayList<Fruit> getFruit(){ return fruitList; } }
* 水果接口
* @author Administrator
*
*/
interface Fruit
{
String toString();
} /**
* 苹果
* @author Administrator
*
*/
class Apple implements Fruit
{
private StringBuffer sb = new StringBuffer();
/**
* 重量
*/
private String weight;
/**
* 颜色
*/
private String color; public Apple(String weight,String color)
{
this.weight = weight;
this.color = color;
}
public String getWeight() {
return weight;
} public void setWeight(String weight) {
this.weight = weight;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return sb.append("这个苹果的颜色是:").append(this.color).append(",").append("重量是:").append(this.weight).toString();
}
}
/**
* 橘子
* @author Administrator
*
*/
class Orange implements Fruit
{
private StringBuffer sb = new StringBuffer();
/**
* 重量
*/
private String weight;
/**
* 颜色
*/
private String color; public Orange(String weight,String color)
{
this.weight = weight;
this.color = color;
}
public String getWeight() {
return weight;
} public void setWeight(String weight) {
this.weight = weight;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return sb.append("这个橘子的颜色是:").append(this.color).append(",").append("重量是:").append(this.weight).toString();
}
}
/**
* 梨
* @author Administrator
*
*/
class Pear implements Fruit
{
private StringBuffer sb = new StringBuffer();
/**
* 重量
*/
private String weight;
/**
* 颜色
*/
private String color; public Pear(String weight,String color)
{
this.weight = weight;
this.color = color;
}
public String getWeight() {
return weight;
} public void setWeight(String weight) {
this.weight = weight;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return sb.append("这个梨的颜色是:").append(this.color).append(",").append("重量是:").append(this.weight).toString();
}
}
/**
* 水果箱子
* @author Administrator
*
*/
class FruitBox
{
//集合用来装水果的
private ArrayList<Fruit> fruitList = new ArrayList<Fruit>();
//用来放水果
public FruitBox addFruit(Fruit fruit)
{
return fruitList.add(fruit)?this:null;
} //用来取水果的集合
public ArrayList<Fruit> getFruit()
{
if(fruitList.isEmpty())
{
return null;
}
return fruitList;
}
}/**
*测试类
* @author Administrator
*
*/
public class FruitTest
{
public static void main(String[] args)
{
//水果箱子
FruitBox fruitBox = new FruitBox();
//苹果
Fruit a = new Apple("23", "红色");
//苹果
Fruit d = new Apple("29", "青色");
//橘子
Fruit b = new Orange("24","黄色");
//梨
Fruit c = new Pear("28","黄色"); //把水果都放入箱子中
fruitBox.addFruit(a).addFruit(b).addFruit(c).addFruit(d);
//把箱子中的水果拿出来
ArrayList<Fruit> fruitList = fruitBox.getFruit();
for(Fruit fruit :fruitList)
{
System.out.println(fruit);
}
}
}
lz 这是我写的 你参照下吧
package com.djk.celue;import java.util.ArrayList;/**
* 水果接口
* @author Administrator
*
*/
interface Fruit
{
String toString();
} /**
* 苹果
* @author Administrator
*
*/
class Apple implements Fruit
{
private StringBuffer sb = new StringBuffer();
/**
* 重量
*/
private String weight;
/**
* 颜色
*/
private String color; public Apple(String weight,String color)
{
this.weight = weight;
this.color = color;
}
public String getWeight() {
return weight;
} public void setWeight(String weight) {
this.weight = weight;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return sb.append("这个苹果的颜色是:").append(this.color).append(",").append("重量是:").append(this.weight).toString();
}
}
/**
* 橘子
* @author Administrator
*
*/
class Orange implements Fruit
{
private StringBuffer sb = new StringBuffer();
/**
* 重量
*/
private String weight;
/**
* 颜色
*/
private String color; public Orange(String weight,String color)
{
this.weight = weight;
this.color = color;
}
public String getWeight() {
return weight;
} public void setWeight(String weight) {
this.weight = weight;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return sb.append("这个橘子的颜色是:").append(this.color).append(",").append("重量是:").append(this.weight).toString();
}
}
/**
* 梨
* @author Administrator
*
*/
class Pear implements Fruit
{
private StringBuffer sb = new StringBuffer();
/**
* 重量
*/
private String weight;
/**
* 颜色
*/
private String color; public Pear(String weight,String color)
{
this.weight = weight;
this.color = color;
}
public String getWeight() {
return weight;
} public void setWeight(String weight) {
this.weight = weight;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return sb.append("这个梨的颜色是:").append(this.color).append(",").append("重量是:").append(this.weight).toString();
}
}
/**
* 水果箱子
* @author Administrator
*
*/
class FruitBox
{
//集合用来装水果的
private ArrayList<Fruit> fruitList = new ArrayList<Fruit>();
//用来放水果
public FruitBox addFruit(Fruit fruit)
{
return fruitList.add(fruit)?this:null;
} //用来取水果的集合
public ArrayList<Fruit> getFruit()
{
if(fruitList.isEmpty())
{
return null;
}
return fruitList;
}
}/**
*测试类
* @author Administrator
*
*/
public class FruitTest
{
public static void main(String[] args)
{
//水果箱子
FruitBox fruitBox = new FruitBox();
//苹果
Fruit a = new Apple("23", "红色");
//苹果
Fruit d = new Apple("29", "青色");
//橘子
Fruit b = new Orange("24","黄色");
//梨
Fruit c = new Pear("28","黄色"); //把水果都放入箱子中
fruitBox.addFruit(a).addFruit(b).addFruit(c).addFruit(d);
//把箱子中的水果拿出来
ArrayList<Fruit> fruitList = fruitBox.getFruit();
for(Fruit fruit :fruitList)
{
System.out.println(fruit);
}
}
}
package com.djk.celue;import java.util.ArrayList;/**
* 水果接口
* @author Administrator
*
*/
interface Fruit
{
String toString();
} /**
* 苹果
* @author Administrator
*
*/
class Apple implements Fruit
{
private StringBuffer sb = new StringBuffer();
/**
* 重量
*/
private String weight;
/**
* 颜色
*/
private String color; public Apple(String weight,String color)
{
this.weight = weight;
this.color = color;
}
public String getWeight() {
return weight;
} public void setWeight(String weight) {
this.weight = weight;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return sb.append("这个苹果的颜色是:").append(this.color).append(",").append("重量是:").append(this.weight).toString();
}
}
/**
* 橘子
* @author Administrator
*
*/
class Orange implements Fruit
{
private StringBuffer sb = new StringBuffer();
/**
* 重量
*/
private String weight;
/**
* 颜色
*/
private String color; public Orange(String weight,String color)
{
this.weight = weight;
this.color = color;
}
public String getWeight() {
return weight;
} public void setWeight(String weight) {
this.weight = weight;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return sb.append("这个橘子的颜色是:").append(this.color).append(",").append("重量是:").append(this.weight).toString();
}
}
/**
* 梨
* @author Administrator
*
*/
class Pear implements Fruit
{
private StringBuffer sb = new StringBuffer();
/**
* 重量
*/
private String weight;
/**
* 颜色
*/
private String color; public Pear(String weight,String color)
{
this.weight = weight;
this.color = color;
}
public String getWeight() {
return weight;
} public void setWeight(String weight) {
this.weight = weight;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return sb.append("这个梨的颜色是:").append(this.color).append(",").append("重量是:").append(this.weight).toString();
}
}
/**
* 水果箱子
* @author Administrator
*
*/
class FruitBox
{
//集合用来装水果的
private ArrayList<Fruit> fruitList = new ArrayList<Fruit>();
//用来放水果
public FruitBox addFruit(Fruit fruit)
{
return fruitList.add(fruit)?this:null;
} //用来取水果的集合
public ArrayList<Fruit> getFruit()
{
if(fruitList.isEmpty())
{
return null;
}
return fruitList;
}
}/**
*测试类
* @author Administrator
*
*/
public class FruitTest
{
public static void main(String[] args)
{
//水果箱子
FruitBox fruitBox = new FruitBox();
//苹果
Fruit a = new Apple("23", "红色");
//苹果
Fruit d = new Apple("29", "青色");
//橘子
Fruit b = new Orange("24","黄色");
//梨
Fruit c = new Pear("28","黄色"); //把水果都放入箱子中
fruitBox.addFruit(a).addFruit(b).addFruit(c).addFruit(d);
//把箱子中的水果拿出来
ArrayList<Fruit> fruitList = fruitBox.getFruit();
for(Fruit fruit :fruitList)
{
System.out.println(fruit);
}
}
}呵呵这样好看点
class Box{
private Fruit fruit;
private List<Fruit> fruitList = new ArrayList<Fruit>();
Box(Fruit fruit){
this.fruit = fruit;
}
public void addFruit(Fruit fruit){}
public void getFruit(Fruit fruit){}
public String showFruit(Fruit fruit){return fruit.getColor() + fruit.getWeight();}
}
class Fruit{
private String color;
private double weight; public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
} public double getWeight() {
return weight;
} public void setWeight(double weight) {
this.weight = weight;
}
}
package com.djk.celue;import java.lang.reflect.Field;
import java.util.ArrayList;/**
* 水果接口
* @author Administrator
*
*/
abstract class Fruit
{
private StringBuffer sb = new StringBuffer();
public String toString()
{
//水果的重量
String weigth = null;
//水果的颜色
String color = null;
//水果的全称包括包名字
String name =null;
//水果的英语名字
String fruitname = null;
try {
name = this.getClass().getName();
//如果name包括包名的话
if(name.lastIndexOf(".")!=-1)
{
fruitname = name.substring(name.lastIndexOf(".")+1,name.length());
}
else
{
fruitname = name;
}
//利用反射拿到fruit的私有属性
Field field1 = this.getClass().getDeclaredField("weight");
field1.setAccessible(true);
Field field2 = this.getClass().getDeclaredField("color");
field2.setAccessible(true);
//得到重量
weigth = (String)field1.get(this);
//得到颜色
color = (String)field2.get(this);
}
catch (Exception e)
{
System.out.println("error");
}
return sb.append("这个").append(fruitname).append("的颜色是").append(color).append("重量是:").append(weigth).toString();
}
} /**
* 苹果
* @author Administrator
*
*/
class Apple extends Fruit
{
/**
* 重量
*/
private String weight;
/**
* 颜色
*/
private String color; public Apple(String weight,String color)
{
this.weight = weight;
this.color = color;
}
public String getWeight() {
return weight;
} public void setWeight(String weight) {
this.weight = weight;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
}
/**
* 橘子
* @author Administrator
*
*/
class Orange extends Fruit
{
/**
* 重量
*/
private String weight;
/**
* 颜色
*/
private String color; public Orange(String weight,String color)
{
this.weight = weight;
this.color = color;
}
public String getWeight() {
return weight;
} public void setWeight(String weight) {
this.weight = weight;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
}
/**
* 梨
* @author Administrator
*
*/
class Pear extends Fruit
{
/**
* 重量
*/
private String weight;
/**
* 颜色
*/
private String color; public Pear(String weight,String color)
{
this.weight = weight;
this.color = color;
}
public String getWeight() {
return weight;
} public void setWeight(String weight) {
this.weight = weight;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
}
/**
* 水果箱子
* @author Administrator
*
*/
class FruitBox
{
//集合用来装水果的
private ArrayList<Fruit> fruitList = new ArrayList<Fruit>();
//用来放水果
public FruitBox addFruit(Fruit fruit)
{
return fruitList.add(fruit)?this:null;
} //用来取水果的集合
public ArrayList<Fruit> getFruit()
{
if(fruitList.isEmpty())
{
return null;
}
return fruitList;
}
}/**
*测试类
* @author Administrator
*
*/
public class FruitTest
{
public static void main(String[] args)
{
//水果箱子
FruitBox fruitBox = new FruitBox();
//苹果
Fruit a = new Apple("23", "红色");
//苹果
Fruit d = new Apple("29", "青色");
//橘子
Fruit b = new Orange("24","黄色");
//梨
Fruit c = new Pear("28","黄色"); //把水果都放入箱子中
fruitBox.addFruit(a).addFruit(d).addFruit(b).addFruit(c);
//把箱子中的水果拿出来
ArrayList<Fruit> fruitList = fruitBox.getFruit();
for(Fruit fruit :fruitList)
{
System.out.println(fruit);
}
}
}
package com.zf.test;import java.util.ArrayList;public class Test7 { public static void main(String[] args) throws Exception{
final FruitBasket basket = new FruitBasket();
final Fruit apple = new Apple("红色", 300);
final Fruit pear = new Pear("黄色", 200); for(int i = 0 ; i < 10 ; i++){ //模拟 10 人取 , 10人放
new Thread(new Runnable() { //添加
@Override
public void run() {
int x = 0 ;
while(true)
try {
basket.addFruit(++x % 2 == 0 ? apple : pear);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() { //取出
@Override
public void run() {
while(true)
try {
basket.getFruit();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}}//果篮
class FruitBasket extends ArrayList<Fruit>{ private static final long serialVersionUID = 1L;
private int max = 10 ; public FruitBasket(){}; public FruitBasket( int max ){
this.max = max ;
}
public synchronized void addFruit(Fruit fruit) throws Exception{
while(size() >= max){
System.out.println("篮子已经满了,请稍等!");
wait();
}
add(fruit);
notifyAll();
System.out.println("放入水果成功,当前水果总数:" + size());
} public synchronized Fruit getFruit() throws Exception{
while(size() <= 0){
System.out.println("篮子已经空了,请稍等!");
wait();
}
Fruit fruit = remove(0);
System.out.println("取出水果成功\t:" + fruit + "\t当前水果总数:" + size());
notifyAll();
return fruit ;
}}//抽象父类
abstract class Fruit{ protected String color ; protected double weight ; abstract void setColor(String color) ; abstract String getColor() ; abstract double getWeight(); abstract void setWeight(double weight) ;
@Override
public String toString() {
return "颜色:" + this.color + " , 重量:" + this.weight;
}
}class Apple extends Fruit{ public Apple(){}
public Apple(String color , double weight){
this.color = color ;
this.weight = weight ;
} @Override
public void setColor(String color) {
this.color = color ;
} @Override
public String getColor() {
return this.color;
} @Override
public double getWeight() {
return this.weight;
} @Override
public void setWeight(double weight) {
this.weight = weight;
} @Override
public String toString() {
return "苹果\t" + super.toString();
}
}class Pear extends Fruit{ public Pear(){}
public Pear(String color , double weight){
this.color = color ;
this.weight = weight ;
}
@Override
public void setColor(String color) {
this.color = color ;
} @Override
public String getColor() {
return this.color;
} @Override
public double getWeight() {
return this.weight;
} @Override
public void setWeight(double weight) {
this.weight = weight;
} @Override
public String toString() {
return "梨子\t" + super.toString();
}}
//抽象父类
abstract class Fruit{ private String name ; private String color ; private double weight ; public Fruit(String name){
this.name = name ;
} public String getName() {
return name;
}
public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
} public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
} @Override
public String toString() {
return "类型:" + this.name + ",颜色:" + this.color + " , 重量:" + this.weight;
}}class Apple extends Fruit{
public Apple(){
super("苹果");
}}class Pear extends Fruit{
public Pear(){
super("梨子");
}
}
public static void main(String args[]){
//水果箱子
FruitBox fruitBox = new FruitBox();
Fruit a = new Apple(23,"红色");
Fruit d = new Apple(29,"青色");
Fruit b = new Orange(24,"黄色");
Fruit c = new Pear(28,"黄色");
//把水果都放入箱子中
fruitBox.addFruit(a).addFruit(d).addFruit(b).addFruit(c);
//把箱子中的水果拿出来
ArrayList<Fruit> fruitList = fruitBox.getFruit();
for(Fruit fruit:fruitList){
System.out.println(fruit);
}
}
}
class Fruit{
private Integer weight;
private String color;
public Fruit(){
}
public Fruit(Integer weight,String color){
this.color = color;
this.weight = weight;
}
public String toString(){
StringBuffer sb = new StringBuffer();
sb.append("这个").append(this.getClass().getName()).append("的颜色是:").append(color).append(" 重量是:").append(weight);
return sb.toString();
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}class Apple extends Fruit{
public Apple(Integer weight,String color){
super(weight,color);
}
}class Orange extends Fruit{
public Orange(Integer weight,String color){
super(weight,color);
}
}class Pear extends Fruit{
public Pear(Integer weight,String color){
super(weight,color);
}
}/*水果箱子*/
class FruitBox{
//集合用来装水果的
private ArrayList<Fruit> fruitList = new ArrayList<Fruit>();
//用来放水果
public FruitBox addFruit(Fruit fruit){
fruitList.add(fruit);
return this;
}
//用来取水果的集合
public ArrayList<Fruit> getFruit(){ return fruitList;
}
}