4.榨汁机
不是很简单吗?主要考察你对创建对象的理解,代码如下
//榨汁机类
public class ZhaFruit {
public static void main(String[] args) {
Apple a=new Apple("苹果","绿","酸酸的");
Banana b=new Banana("黄","香蕉","甜甜的");
String fruitZhi = getFruitZhi(a);
System.out.println(fruitZhi);
String fruitZhi1 = getFruitZhi(a, b);
System.out.println(fruitZhi1);
String fruitZhi2 = getFruitZhi(a, 3);
System.out.println(fruitZhi2);
}
public static String getFruitZhi(Apple a){
return "一杯"+a.getColor()+"(颜色)的"+a.getName()+"(水果名)汁"+",味道"+a.getWeiDao();
}
public static String getFruitZhi(Apple a,Banana b){
return "一杯"+b.color+a.getColor()+"(颜色)的"+a.getName()+b.name
+"(水果名)汁"+",味道"+a.getWeiDao()+b.weiDao;
}
public static String getFruitZhi(Apple a,int num){
return num+a.getColor()+"(颜色)的"+a.getName()+"(水果名)汁"+",味道"+a.getWeiDao();
}
}
//苹果类
class Apple{
private String name;
private String color;
private String weiDao;
public Apple(String name, String color, String weiDao) {
this.name = name;
this.color = color;
this.weiDao = weiDao;
}
public String getName() {
return name;
}
public String getColor() {
return color;
}
public String getWeiDao() {
return weiDao;
}
}
//定义一个香蕉类:Banana,内有公有的字段:name,color,weiDao
class Banana{
public String name;
public String color;
public String weiDao;
public Banana(String name, String color, String weiDao) {
this.name = name;
this.color = color;
this.weiDao = weiDao;
}
}
测试结果如图