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日

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP