GMbyz 2021-10-22 15:23 采纳率: 71.4%
浏览 36
已结题

面向对象练习题求解答

求解

img

  • 写回答

2条回答 默认 最新

  • 天行归来 2021-10-22 16:54
    关注

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

    
    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();
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月23日
  • 已采纳回答 10月23日
  • 修改了问题 10月22日
  • 创建了问题 10月22日