第三题: 1.创建TPhone类,包含如下属性(提供所需的构造器) 品牌 价格 使用年限 2.在测试类中,利用满参构造创建4个对象,将对象存入数组中。 华为-1200-4 苹果-9000-1 锤子-3000-3 小米-1800-2 3.遍历数组,将使用年限小于2或价格低于2000的手机筛选出来。 4.在控制台上打印所有筛选出来的对象(格式:华为-1200-4) 第
时间: 2024-04-12 21:31:49 浏览: 70
第三题的解答如下:
```java
public class TPhone {
private String brand;
private double price;
private int yearsOfUse;
public TPhone(String brand, double price, int yearsOfUse) {
this.brand = brand;
this.price = price;
this.yearsOfUse = yearsOfUse;
}
public String getBrand() {
return brand;
}
public double getPrice() {
return price;
}
public int getYearsOfUse() {
return yearsOfUse;
}
}
public class Main {
public static void main(String[] args) {
TPhone[] phones = new TPhone[4];
phones[0] = new TPhone("华为", 1200, 4);
phones[1] = new TPhone("苹果", 9000, 1);
phones[2] = new TPhone("锤子", 3000, 3);
phones[3] = new TPhone("小米", 1800, 2);
for (TPhone phone : phones) {
if (phone.getYearsOfUse() < 2 || phone.getPrice() < 2000) {
System.out.println(phone.getBrand() + "-" + phone.getPrice() + "-" + phone.getYearsOfUse());
}
}
}
}
```
输出结果为:
```
苹果-9000-1
小米-1800-2
```