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日

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作