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

img

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

运行结果如下,如有帮助,请帮忙采纳一下,谢谢。

img

Bkie类:


public class Bike {
    protected String biketype;
    protected String bcolour;
    protected int bprice;
    protected int bspeed;
    public Bike(){}
    public Bike(String type,String color,int price,int speed){
        this.biketype = type;
        this.bcolour = color;
        this.bprice = price;
        this.bspeed = speed;
    }
    public void show(){
        System.out.println(this.toString());
    }
    public String getBiketype() {
        return biketype;
    }
    public void setBiketype(String biketype) {
        this.biketype = biketype;
    }
    public String getBcolour() {
        return bcolour;
    }
    public void setBcolour(String bcolour) {
        this.bcolour = bcolour;
    }
    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;
    }
    @Override
    public String toString() {
        return "Bike [biketype=" + biketype + ", bcolour=" + bcolour + ", bprice=" + bprice + ", bspeed=" + bspeed
                + "]";
    }
    
}


Mountainbike类:

public class Mountainbike extends Bike{
    private int TyrePressure;
    public Mountainbike(String type,String color,int price,int speed,int pres){
        super(type,color,price,speed);
        this.TyrePressure = pres;
    }
    
    public void addSpeed(int s){
        this.bspeed += s;
    }
    public void subSpeed(int s){
        this.bspeed -= s;
    }
    
    public void show(){
        System.out.println(this.toString());
    }

    public int getTyrePressure() {
        return TyrePressure;
    }

    public void setTyrePressure(int tyrePressure) {
        TyrePressure = tyrePressure;
    }

    @Override
    public String toString() {
        return "Mountainbike [TyrePressure=" + TyrePressure + ", biketype=" + biketype + ", bcolour=" + bcolour
                + ", bprice=" + bprice + ", bspeed=" + bspeed + "]";
    }

}

Test1类:

public class Test1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Bike bk = new Bike("普通自行车","黑色",400,15);
        Mountainbike mbk = new Mountainbike("凤凰山地车","红色",600,20,100);
        bk.show();
        mbk.show();
        mbk.addSpeed(3);
        mbk.show();
        mbk.subSpeed(5);
        mbk.show();
    }

}