经典指数          
原因
3058
浏览数
0
收藏数
 

定义一个水果类 Fruit ,要求如下: l 私有属性 weight ,代表水果的质量,数据类型为 float l 私有属性 color ,代表水果的颜色,数据类型为 String l 构造方法 public Fruit() ,将质量和颜色初始化为默认值 l 构造方法 public Fruit(float w,String c) ,将质量和颜色初始化为w和c l 公有方法 getWeight ( ) ,用于获取水果的质量 l 公有方法 setWeight ( ) ,用于设置水果的质量 l 公有方法 getColor ( ) ,用于获取水果的颜色 l 公有方法 setColor ( ) ,用于设置水果的颜色 l 公有方法 disp ( ) ,用于显示水果的质量和颜色

     举报   纠错  
 
切换
1 个答案
class Fruit { private float weight; private String color; public Fruit() { } public Fruit(float w,String c) { setWeight(w) ; setColor(c); } public float getWeight() { return weight; } public void setWeight(float w) { if(w<0) weight=0; else weight=w; } public String getColor() { return color; } public void setColor(String c) { color=c; } public void disp() { System.out.println("weight="+ this.getWeight()+"  color="+this. getColor()); } }
 
切换
撰写答案
扫描后移动端查看本题