m0_disjjebwoo 2021-10-28 13:55 采纳率: 100%
浏览 146
已结题

Java类的创建和继承以及方法怎么做

img

                                                                                                       自行车速度 (bprice) 自行车颜色(bcolour),自行车价格 1)创建自行车(Bike)类,属性有:自行车类别(biketype)、 bspeed)等,创建两个构造方法,分别
 为带参数的和无参的、创建各属性的getxXX和setXXX方法,再创建输出信息方法(voidshow0),用于输出自行车对象的各属性信息
   (2)创建山地车类(Mountainbike),直接维承Bike类,新增属性胎压(Tyrepressure),创建带参的构造方法,增加成员方法addspeed用于提高自行车速度,subspeed用于降低自行车速度·重写
  文类show方法,用于出山地车信息
3)创建测试类Test1,分别创建父类和子类对象进行测试,并输出各对象信息
  • 写回答

3条回答 默认 最新

  • CSDN专家-sinJack 2021-10-28 13:59
    关注
    public class Bike {
        private String biketype;
        private String bcolor;
        private int bprice;
        private int bspeed;
        public Bike(){}
        public Bike(String biketype, String bcolor, int bprice, int bspeed) {
            this.biketype = biketype;
            this.bcolor = bcolor;
            this.bprice = bprice;
            this.bspeed = bspeed;
        }
    
        public String getBiketype() {
            return biketype;
        }
    
        public void setBiketype(String biketype) {
            this.biketype = biketype;
        }
    
        public String getBcolor() {
            return bcolor;
        }
    
        public void setBcolor(String bcolor) {
            this.bcolor = bcolor;
        }
    
        public int getBprice() {
            return bprice;
        }
    
        public void setBprice(int bprice) {
            this.bprice = bprice;
        }
    
        public int getBspeed() {
            return bspeed;
        }
    
        public void setBspeed(int bspeed) {
            this.bspeed = bspeed;
        }
    
        
        public void show() {
            System.out.println("biketype='" + biketype + '\'' +
                    ", bcolor='" + bcolor + '\'' +
                    ", bprice=" + bprice +
                    ", bspeed=" + bspeed );
        }
    }
    
    public class Mountainbike extends Bike{
        private int typePressure;
    
        public Mountainbike(int typePressure) {
            this.typePressure = typePressure;
        }
    
        public Mountainbike(String biketype, String bcolor, int bprice, int bspeed, int typePressure) {
            super(biketype, bcolor, bprice, bspeed);
            this.typePressure = typePressure;
        }
        
        public void addSpeed(int speed){
            this.setBspeed(this.getBspeed()+speed);
        }
        public void subSpeed(int speed){
            this.setBspeed(this.getBspeed()-speed);
        }
    
        @Override
        public void show() {
            super.show();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月5日
  • 已采纳回答 10月28日
  • 创建了问题 10月28日

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格