首页 行业资讯 宠物日常 宠物养护 宠物健康 宠物故事

java两道题目——类的继承和多态机制

发布网友 发布时间:2022-04-26 20:29

我来回答

1个回答

热心网友 时间:2023-10-29 18:05

//vehicle类, public class Vehicle { private float speed; private String kind; private String color; public float getSpeed() { return speed; } public void setSpeed(float speed) { this.speed = speed; } public String getKind() { return kind; } public void setKind(String kind) { this.kind = kind; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } } //汽车类; public class Car extends Vehicle { private int passenger; public int getPassenger() { return passenger; } public void setPassenger(int passenger) { this.passenger = passenger; } @Override public String toString() {//现实所有的属性 return "speed="+this.getSpeed()+" kind="+this.getKind()+" color="+this.getColor()+" passenger="+this.getPassenger(); } public static void main(String[] args){//测试 Car car=new Car(); car.setSpeed(60.5f); car.setKind("汽车"); car.setColor("blue"); car.setPassenger(5); System.out.println(car.toString()); } } 下面个题马上发上来,等下!

热心网友 时间:2023-10-29 18:05

//vehicle类, public class Vehicle { private float speed; private String kind; private String color; public float getSpeed() { return speed; } public void setSpeed(float speed) { this.speed = speed; } public String getKind() { return kind; } public void setKind(String kind) { this.kind = kind; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } } //汽车类; public class Car extends Vehicle { private int passenger; public int getPassenger() { return passenger; } public void setPassenger(int passenger) { this.passenger = passenger; } @Override public String toString() {//现实所有的属性 return "speed="+this.getSpeed()+" kind="+this.getKind()+" color="+this.getColor()+" passenger="+this.getPassenger(); } public static void main(String[] args){//测试 Car car=new Car(); car.setSpeed(60.5f); car.setKind("汽车"); car.setColor("blue"); car.setPassenger(5); System.out.println(car.toString()); } } 下面个题马上发上来,等下!

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com