扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
/**
创新互联公司专业为企业提供青铜峡网站建设、青铜峡做网站、青铜峡网站设计、青铜峡网站制作等企业网站建设、网页设计与制作、青铜峡企业网站模板建站服务,十年青铜峡做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
* 1.定义一个水果类Fruit,符合如下要求:
(a) 类Fruit的成员变量;
weight表示水果的质量,数据类型为float
color表示水果的颜色,数据类型为string.
(b)类Fruit的成员方法:
getWeight()获得水果的质量
getColor()获得水果的颜色
2、按照第1题中的水果类Fruit的定义,创建该类的对象f1和f2,其中f1的weight值为0.86, color值为yellow;
f2的weight值为0.73, color值为red; 存储并输出f1和f2的值,计算f1和f2的平均值。
* @author Administrator
*
*/
public class Fruit {
private float weight;
private String color;
//构造函数:用于存储对象的2个值
public Fruit(float weight, String color){
this.weight = weight;
this.color = color;
}
public float getWeight() {
return this.weight;
}
public String getColor() {
return this.color;
}
//求重量的平均值
public static float avg(float w1, float w2){
return w1*w2/2;
}
public static void main(String[] args){
Fruit f1 = new Fruit((float) 0.86, "yellow");
Fruit f2 = new Fruit((float) 0.73, "red");
System.out.println("f1:" + "\n" + " weitht:" + f1.getWeight() + "\n" + " color:" + f1.getColor());
System.out.println("f2:" + "\n" + " weitht:" + f2.getWeight() + "\n" + " color:" + f2.getColor());
System.out.println("平均值:" + Fruit.avg(f1.getWeight(), f2.getWeight()));
}
}
class Fruit{
private String fruitName;
private float price;
public Fruit(String fruitName, float price) {
super();
this.fruitName = fruitName;
this.price = price;
}
public String getFruitName() {
return fruitName;
}
public void setFruitName(String fruitName) {
this.fruitName = fruitName;
}
public float getPrice() {
return price;
}
public void setPrice(float privce) {
this.price = privce;
}
@Override
public String toString() {
return "水果名称:" + getFruitName() + " " + "价格:" + getPrice();
}
}
class Apple extends Fruit{
public Apple(String fruitName, float price) {
super(fruitName, price);
}
}
class Banana extends Fruit{
public Banana(String fruitName, float price) {
super(fruitName, price);
}
}
public class FruitShop {
ListFruit list;
public FruitShop(){
list = new ArrayListFruit();
list.add(new Apple("苹果",3));
list.add(new Banana("香蕉",2));
}
public void show(){
for (Fruit f : list) {
System.out.println(f);
}
}
public static void main(String[] args) {
new FruitShop().show();
}
}
public class Fruit {
private String name;
private double weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
//构造方法
public Fruit(String name,double weight){
this.name = name;
this.weight = weight;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Fruit f = new Fruit("苹果",0.5);
System.out.println(f.getName());//输出名称
System.out.println(f.getWeight());//输出名称
}
}
如果对你有所帮助,请采纳,谢谢!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流