目的:使用类来封装对象的属性和功能,子类完成设计。 题目1:创建一个类话题Tip类,该类有Id、话题内容两个属性,设计有参数和无参数构造方法,设计getInfo()方法打印话题内容。创建帖子类Topic继承帖子类,增加一个属性所属板块,设计构造方法注意使用super关键字,重写getInfo()方法打印内容为话题内容和所属板块信息。编写主类进行测试。 题目2:设计Drink类,有一个饮品名字和饮品价格两个属性,设计相应的get和set方法,设计一个奶茶类继承饮品类,增加成员变量成分Composition,设计getInfo()方法得到奶茶的相关信息;设计一个可乐类继承饮品类,增加成员变量品牌必brand,设计getInfo()方法得到可乐的相关信息;设计售卖类,售卖的内容是Drink类对象饮品,设计售卖方法,根据价格和数量计算需要付款多少钱,设计显示信息方法打印售卖的饮品是什么需要多少钱在屏幕上。设计主类进行上述类的测试。 题目3:创建一个Vehicle类并将它声明为抽象类。在Vehicle类中声明两个抽象方法run和speed。创建两个类Car 和 Motorbike 从Vehicle类继承,并在这两个类中实现抽象类方法。重写的方法自己定义方法的操作。设计主类,在该类中创建Car和 Motorbike的实例,进行上述重写方法的测试。
时间: 2024-02-24 10:53:02 浏览: 71
题目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();
}
}
```
阅读全文