面向对象练习题求解答

求解

img

挺简单的问题,除非今后不走研发这条路,要不还是要自己尝试着写。


package gcms;

public class Fruits {
    private String shape;
    private String taste;
    
    /**
     * 无参数构造函数
     */
    public Fruits() {};
    
    /**
     * 带参数构造函数
     * @param shape
     * @param taste
     */
    public Fruits(String shape, String taste) {
        this.shape = shape;
        this.taste = taste;
    }
    
    public void face() {
        System.out.println("水果可供人们使用,形状:"+shape+",口感:"+taste);
    }

    public String getShape() {
        return shape;
    }
    public void setShape(String shape) {
        this.shape = shape;
    }
    public String getTaste() {
        return taste;
    }
    public void setTaste(String taste) {
        this.taste = taste;
    }
    
    /**
     * 香蕉类
     * @author lms
     *
     */
    public class Banana extends Fruits{
        private String color;
        private String variety;
        private int weight;

        public Banana(){}
        public Banana(String shape, String taste, String color, String variety, int weight) {
            super(shape, taste);
            this.color = color;
            this.variety = variety;
            this.weight = weight;
        }
        
        @Override
        public void face() {
            System.out.println("香蕉可口常吃!非常好吃!形状:"+getShape()+",口感:"+getTaste()+",颜色:"+color+",品种:"+variety+",重量:"+weight);
        }

        public String getColor() {
            return color;
        }
        public void setColor(String color) {
            this.color = color;
        }
        public String getVariety() {
            return variety;
        }
        public void setVariety(String variety) {
            this.variety = variety;
        }
        public int getWeight() {
            return weight;
        }
        public void setWeight(int weight) {
            this.weight = weight;
        }
    }
    
    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        Fruits f = new Fruits();
        f.setShape("椭圆形");
        f.setTaste("可口");
        f.face();
        
        Banana b = new Fruits().new Banana("月湾儿型","香甜","金黄","仙人蕉",35);
        b.face();
    }
}

这么简单自己写呀,会写一个其他都不是问题了,他们代码都是类似的