选择题
1.A
2.D
3.C
4.
5.B
6.C
7.C
8.B
9.ABC
10.D
11.D
12.C
13.C
编程题
- 编程练习:某公司要开发“XX车行管理系统”,请使用面向对象的思想,设计自定义类描述自行 车、电动车和三轮车。
任务分析; 第一步:分析自行车、电动车和三轮车的共性: - 都是非机动车,具有非机动车的基本特征 2. 都有运行的方法
第二步:根据共性,定义非机动车 属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个) 方法: 1. 编写无参构造方法、双参构造方法和四参构造方法,其中,在双参构造方法中,完成对品牌和 颜色的赋值;在四参构造方法中,完成对所有属性的赋值 2. 编写运行的方法,描述内容为:这是一辆颜色的,牌的非机动车,有个轮子,有个座椅 的非机动车。其中**的数据由属性提供 第三步:定义自行车、电动车和三轮车分别继承自行车类,要求:
自行车类: - 在构造方法中调用父类多参构造,完成属性赋值 2. 重写运行方法,描述内容为:这是一辆**颜色的,牌的自行车。其中的数据由属性提供
电动车: - 增加“电池品牌”属性 2. 重写运行方法,描述内容为:这是一辆使用牌电池的电动车。其中的数据由属性提供
三轮车: - 在无参构造中实现对轮子属性值进行修改 2. 重写运行方法,描述内容为:三轮车是一款有个轮子的非机动车。其中的数据由属性提供
//车类
public class Vehicle {
String brand;
String color;
int wheel = 2;
int seat = 1;
public Vehicle() {}
public Vehicle(String brand, String color) {
this.brand = brand;
this.color = color;
}
public Vehicle(String brand, String color, int wheel, int seat) {
this.brand = brand;
this.color = color;
this.wheel = wheel;
this.seat = seat;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getWheel() {
return wheel;
}
public void setWheel(int wheel) {
this.wheel = wheel;
}
public int getSeat() {
return seat;
}
public void setSeat(int seat) {
this.seat = seat;
}
public void info() {
System.out.println("父类信息测试:这是一辆" + this.getColor() + "的," + this.getBrand() + "的非机动车," + "有" + this.getWheel()
+ "个轮子," + this.getSeat() + "个座椅");
}
}
//自行车类
public class Bicycle extends Vehicle {
public Bicycle() {
}
public Bicycle(String brand, String color) {
super(brand, color);
}
public Bicycle(String brand, String color, int wheel, int seat) {
super(brand, color, wheel, seat);
}
@Override
public void info() {
System.out.println("自行车类信息测试:这是一辆" + this.getColor() + "的," + this.getBrand() + "的自行车");
}
}
//电动车类
public class Electrocar extends Vehicle {
String battery;
public Electrocar() {
}
public Electrocar(String battery) {
this.battery = battery;
}
public String getBattery() {
return battery;
}
public void setBattery(String battery) {
this.battery = battery;
}
@Override
public void info() {
System.out.println("电动车类信息测试:这是一辆使用" + this.getBattery() + "的电动车");
}
}
//三轮车类
public class Ticycle extends Vehicle {
public Ticycle() {
this.setWheel(3);
}
public Ticycle(String brand, String color) {
super(brand, color);
}
public Ticycle(String brand, String color, int wheel, int seat) {
super(brand, color, wheel, seat);
}
@Override
public void info() {
System.out.println("三轮车类信息测试:三轮车是一款有" + wheel + "个轮子的非机动车");
}
}
//测试类
public class Test {
public static void main(String[] args) {
Vehicle v = new Vehicle("天宇牌", "红颜色", 4, 2);
v.info();
Bicycle b = new Bicycle("捷安特牌", "黄颜色");
b.info();
Electrocar e = new Electrocar("飞鸽牌电池");
e.info();
Ticycle t = new Ticycle();
t.info();
}
}
- 请使用面向对象的思想,设计自定义类Person继承Object类,重写toString方法实现对象信息输 出。
思路分析
创建一个 Person 类继承自 Object,其中类的结构要求为: 属性:name(姓名)、age(年龄)、sex(性别)
方法: 创建带参(name、age、sex为参数)构造方法 重写 toString 方法,输出信息格式为:姓名:** 年龄:** 性别:**(其中,**为对象对应 属性值) 创建测试类,在测试方法中,实例化 Person对 象,并传入三个属性值。然后,分别通过直接 打印Person对象以及利用重写的 toString 方法,打印输出2行对象信息
//Person类
public class Person {
//私有属性:name(姓名)、age(年龄)、sex(性别)
private String name;
private int age;
private String sex;
public Person() {}
//带参构造方法(name、age、sex为参数)
public Person(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
//通过封装实现对属性的get/set方法设定
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//重写toString方法,表示形式为:姓名:+**+ 年龄:+**+ 性别:+**
@Override
public String toString() {
return "姓名:" + this.getName() + " 年龄:" + this.getAge() + " 性别:" + this.getSex();
}
}
//测试类
public class Test {
public static void main(String[] args) {
Person p = new Person("李明", 18, "男");
System.out.println(p);
System.out.println(p.toString());
}
}
- 请使用面向对象的思想,实现杨梅和仙人蕉的信息描述。
思路分析: 1、根据杨梅和香蕉的共性,抽取父类水果(Fruits) 私有属性:水果的形状(shape)和口感(taste)
方法:
带参构造函数(参数为shape和taste)
创建无参无返回值得方法eat(描述内容为:水果可供人们食用!)
重写equals方法,比较两个对象是否相等(比较shape,taste)
2、子类Waxberry 私有属性:颜色(color) 方法:
调用父类的构造方法,完成属性赋值 创建不允许重写的face方法,描述为:杨梅:、,果味酸甜适中。 重写父类eat方法,描述为:杨梅酸甜适中,非常好吃! 重写toString方法,输出的表现形式不同(输出shape,color,taste) 要求Waxberry类不允许有子类
3、子类:Banana 私有属性:品种(variety) 方法:
带参构造方法为所有属性赋值 创建无参无返回值的advantage方法,描述为:果形,果肉香甜,可供生食。 重载要求(2)中的advantage方法(带参数color),描述为:颜色为
4、测试,运行效果参照效果图:
实例化2个父类对象,并传入两组相同的参数值 调用父类eat方法 测试重写equals方法,判断两个对象是否相等 实例化子类Wacberry对象,并传入相关参数值 调用子类face方法和重写父类eat方法后的eat方法 测试重写toString方法,输出子类对象的信息 实例化Banana类对象,并传入相关参数值 调用子类的advantage和它的重载方法
//水果类
public class Fruits {
private String shape;
private String taste;
public Fruits() {}
public Fruits(String shape, String taste) {
this.shape = shape;
this.taste = taste;
}
public String getShape() {
return shape;
}
public void setShape(String shape) {
this.shape = shape;
}
public String getTaste() {
return taste;
}
public void setTaste(String taste) {
this.taste = taste;
}
public void eat() {
System.out.println("水果可供人们食用!");
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((shape == null) ? 0 : shape.hashCode());
result = prime * result + ((taste == null) ? 0 : taste.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Fruits other = (Fruits) obj;
if (shape == null) {
if (other.shape != null)
return false;
} else if (!shape.equals(other.shape))
return false;
if (taste == null) {
if (other.taste != null)
return false;
} else if (!taste.equals(other.taste))
return false;
return true;
}
}
//杨梅类
public class Waxberry extends Fruits {
private String color;
public Waxberry() {}
public Waxberry(String shape, String taste, String color) {
super(shape, taste);
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
final public void face() {
System.out.println("杨梅:" + this.getColor() + "、" + this.getShape() + ",果味" + this.getTaste() + "。");
}
@Override
public void eat() {
System.out.println("杨梅" + this.getTaste() + ",非常好吃!");
}
@Override
public String toString() {
return "杨梅的信息:果实为" + this.getShape() + "、" + this.getColor() + "," + this.getTaste() + ",非正好吃!";
}
}
//仙人蕉类
public class Banana extends Fruits {
private String variety;
public Banana() {}
public Banana(String shape, String taste, String variety) {
super(shape, taste);
this.variety = variety;
}
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
public void advantage() {
System.out.println("仙人蕉果形" + this.getShape() + "," + this.getTaste() + ",可供生食。");
}
public void advantage(String color) {
System.out.println("仙人蕉颜色为" + color);
}
}
//测试类
public class Test {
public static void main(String[] args) {
Fruits fru1 = new Fruits("圆形", "酸甜");
Fruits fru2 = new Fruits("圆形", "酸甜");
fru1.eat();
System.out.println("fru1和fru2的引用比较:" + fru1.equals(fru2));
Waxberry w = new Waxberry("圆形", "酸甜适中", "紫红色");
w.face();
w.eat();
System.out.println(w.toString());
Banana b = new Banana("短而稍圆", "果肉香甜", " ");
b.advantage();
b.advantage("黄色");
}
}