一、选择
1.A
2.D
3.C
4.B
5.B
6.C
7.C
8.B
9.ABD
10.D
11.D
12.C
二、编程
1. 编程练习:某公司要开发“XX车行管理系统”,请使用面向对象的思想,设计自定义类描述自行
车、电动车和三轮车。
程序参考运行效果图如下:
任务分析;
第一步:分析自行车、电动车和三轮车的共性:
- 都是非机动车,具有非机动车的基本特征
- 都有运行的方法
第二步:根据共性,定义非机动车
属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
方法: - 编写无参构造方法、双参构造方法和四参构造方法,其中,在双参构造方法中,完成对品牌和
颜色的赋值;在四参构造方法中,完成对所有属性的赋值 - 编写运行的方法,描述内容为:这是一辆颜色的,牌的非机动车,有个轮子,有个座椅
的非机动车。其中**的数据由属性提供
第三步:定义自行车、电动车和三轮车分别继承自行车类,要求:
自行车类: - 在构造方法中调用父类多参构造,完成属性赋值
- 重写运行方法,描述内容为:这是一辆**颜色的,牌的自行车。其中的数据由属性提供
电动车: - 增加“电池品牌”属性
- 重写运行方法,描述内容为:这是一辆使用牌电池的电动车。其中的数据由属性提供
三轮车: - 在无参构造中实现对轮子属性值进行修改
- 重写运行方法,描述内容为:三轮车是一款有个轮子的非机动车。其中的数据由属性提供
package com.dodoke.lianxi2.demo;
/**
* 父类-非机动车类
* @author Administrator
*
*/
public class Novehicle {
private String brand;
private String color;
private int wheel;
private int chair;
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 getChair() {
return chair;
}
public void setChair(int chair) {
this.chair = chair;
}
public Novehicle() {
super();
}
public Novehicle(String brand, String color) {
super();
this.setBrand(brand);
this.setColor(color);
}
public Novehicle(String brand, String color, int wheel, int chair) {
super();
this.setBrand(brand);
this.setColor(color);
this.setWheel(wheel);
this.setChair(chair);
}
public String infoTest() {
String str = "";
str = "这是一辆" + this.getColor() + "颜色的,";
str += this.getBrand() + "的非机动车," + "有" + this.getWheel() + "个轮子,";
str += "有" + this.getChair() + "个座椅";
return str;
}
}
package com.dodoke.lianxi2.demo;
/**
* 自行车类
* @author Administrator
*
*/
public class Bicycle extends Novehicle {
public Bicycle() {
super();
}
public Bicycle(String brand, String color) {
super(brand,color);
}
public String infoTest() {
String str = "";
str = "这是一辆" + this.getColor() + "颜色的,";
str += this.getBrand() + "的自行车。";
return str;
}
}
package com.dodoke.lianxi2.demo;
/**
* 电动车类
* @author Administrator
*
*/
public class Evehicle extends Novehicle {
private String batteryBrand;
public String getBatteryBrand() {
return batteryBrand;
}
public void setBatteryBrand(String batteryBrand) {
this.batteryBrand = batteryBrand;
}
public Evehicle() {
super();
}
public Evehicle(String batteryBrand) {
super();
this.setBatteryBrand(batteryBrand);
}
public String infoTest() {
String str = "";
str = "这是一辆使用" + this.getBatteryBrand() + "牌电池的电动车。";
return str;
}
}
package com.dodoke.lianxi2.demo;
/**
* 三轮车类
* @author Administrator
*
*/
public class Tricycle extends Novehicle {
public Tricycle() {
this.setWheel(3);
}
public String infoTest() {
String str = "";
str = "三轮车是一款有" + this.getWheel() + "个轮子的非机动车";
return str;
}
}
package com.dodoke.lianxi2.demo;
/**
* 测试类
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
Novehicle v = new Novehicle("天宇牌","红",4,2);
System.out.println("父类信息测试:" + v.infoTest());
Bicycle b = new Bicycle("捷安特牌","黄");
System.out.println("自行车类信息测试:" + b.infoTest());
Evehicle e = new Evehicle("飞鸽牌");
System.out.println("电动车类信息测试:" + e.infoTest());
Tricycle t = new Tricycle();
System.out.println("三轮车类信息测试:" + t.infoTest());
}
}
2. 请使用面向对象的思想,设计自定义类Person继承Object类,重写toString方法实现对象信息输
出。
思路分析
创建一个 Person 类继承自 Object,其中类的结构要求为:
属性:name(姓名)、age(年龄)、sex(性别)
方法:
创建带参(name、age、sex为参数)构造方法
重写 toString 方法,输出信息格式为:姓名:** 年龄:** 性别:**(其中,**为对象对应
属性值)
创建测试类,在测试方法中,实例化 Person对 象,并传入三个属性值。然后,分别通过直接
打印Person对象以及利用重写的 toString 方法,打印输出2行对象信息。
package com.dodoke.lianxi2.demo;
public class Person {
private String name;
private int age;
private String sex;
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;
}
public Person() {
super();
}
public Person(String name, int age, String sex) {
super();
this.setName(name);
this.setAge(age);
this.setSex(sex);
}
public String toString() {
String str = "";
str = "姓名:" + this.getName() + " ";
str += "年龄:" + this.getAge() + " ";
str += "性别:" + this.getSex();
return str;
}
}
package com.dodoke.lianxi2.demo;
public class Test2 {
public static void main(String[] args) {
Person p = new Person("李明",18,"男");
System.out.println(p);
System.out.println(p.toString());
}
}