java小径车碗组_Java建立一个汽车(Car)类,包括品牌(String car_brand)、速度(double car_speed)两个成员变量具体看图...

展开全部

这个代码还是比较简单,下面的代码可以参考class Car{

private String car_brand;

private double car_speed;

public Car(){ }

public Car(String brand, double speed){

this.car_brand = brand;

this.car_speed = speed;

System.out.println(this.car_brand + "汽车正e69da5e6ba903231313335323631343130323136353331333366303762以时速"

+ this.car_speed + "公里的速度行驶");

}

public void speedUp(String brand){

System.out.println(brand + "汽车正在加速行驶");

}

public void speedUp(String brand, double speed){

System.out.println(brand + "汽车正以时速"

+ speed + "公里的速度加速行驶");

}

public void speedDown(String brand){

System.out.println(brand + "汽车正在减速行驶");

}

public void speedDown(String brand, double speed){

System.out.println(brand + "汽车正以时速"

+ speed + "公里的速度减速行驶");

}

}

class Test{

public static void main(String[] args){

Car car = new Car("宝马", 100.5);

car.speedUp("法拉利");

car.speedUp("法拉利", 280.5);

car.speedDown("法拉利");

car.speedDown("法拉利", 120.9);

}

}

题目1的代码实现如下: ``` public class Tip { private int id; private String content; public Tip() {} public Tip(int id, String content) { this.id = id; this.content = content; } public void setId(int id) { this.id = id; } public int getId() { return id; } public void setContent(String content) { this.content = content; } public String getContent() { return content; } public void getInfo() { System.out.println("话题内容:" + content); } } public class Topic extends Tip { private String category; public Topic() {} public Topic(int id, String content, String category) { super(id, content); this.category = category; } public void setCategory(String category) { this.category = category; } public String getCategory() { return category; } @Override public void getInfo() { System.out.println("话题内容:" + getContent() + ",所属板块:" + category); } } public class Main { public static void main(String[] args) { Tip tip = new Tip(1, "如何学好Java"); tip.getInfo(); Topic topic = new Topic(2, "Java基础知识", "编程语言"); topic.getInfo(); } } ``` 题目2的代码实现如下: ``` public class Drink { private String name; private double price; public Drink() {} public Drink(String name, double price) { this.name = name; this.price = price; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setPrice(double price) { this.price = price; } public double getPrice() { return price; } } public class MilkTea extends Drink { private String composition; public MilkTea() {} public MilkTea(String name, double price, String composition) { super(name, price); this.composition = composition; } public void setComposition(String composition) { this.composition = composition; } public String getComposition() { return composition; } public void getInfo() { System.out.println("饮品名字:" + getName() + ",饮品价格:" + getPrice() + ",成分:" + composition); } } public class Cola extends Drink { private String brand; public Cola() {} public Cola(String name, double price, String brand) { super(name, price); this.brand = brand; } public void setBrand(String brand) { this.brand = brand; } public String getBrand() { return brand; } public void getInfo() { System.out.println("饮品名字:" + getName() + ",饮品价格:" + getPrice() + ",品牌:" + brand); } } public class Sales { public void sell(Drink drink, int count) { double total = drink.getPrice() * count; System.out.println("售卖的饮品是:" + drink.getName() + ",需要付款:" + total + "元"); } } public class Main { public static void main(String[] args) { MilkTea milkTea = new MilkTea("珍珠奶茶", 15.0, "珍珠、牛奶、茶叶"); milkTea.getInfo(); Cola cola = new Cola("可乐", 3.0, "可口可乐"); cola.getInfo(); Sales sales = new Sales(); sales.sell(milkTea, 2); sales.sell(cola, 3); } } ``` 题目3的代码实现如下: ``` public abstract class Vehicle { public abstract void run(); public abstract void speed(); } public class Car extends Vehicle { @Override public void run() { System.out.println("在跑"); } @Override public void speed() { System.out.println("速度很快"); } } public class Motorbike extends Vehicle { @Override public void run() { System.out.println("摩托在跑"); } @Override public void speed() { System.out.println("摩托速度很快"); } } public class Main { public static void main(String[] args) { Vehicle car = new Car(); car.run(); car.speed(); Vehicle motorbike = new Motorbike(); motorbike.run(); motorbike.speed(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值