选择
// 1、A
// 2、D
// 3、C
// 4、
// 5、B
// 6、C
// 7、C
// 8、B
// 9、ABD
// 10、D
// 11、D
// 12、C
编程
-
编程练习:某公司要开发“XX车行管理系统”,请使用面向对象的思想,设计自定义类描述自行 车、电动车和三轮车。
任务分析; 第一步:分析自行车、电动车和三轮车的共性:- 都是非机动车,具有非机动车的基本特征 2. 都有运行的方法
第二步:根据共性,定义非机动车 属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
方法:
- 编写无参构造方法、双参构造方法和四参构造方法,其中,在双参构造方法中,完成对品牌和 颜色的赋值;在四参构造方法中,完成对所有属性的赋值
- 编写运行的方法,描述内容为:这是一辆颜色的,牌的非机动车,有个轮子,有个座椅 的非机动车。其中**的数据由属性提供 第三步:定义自行车、电动车和三轮车分别继承自行车类,要求:
自行车类:
在构造方法中调用父类多参构造,完成属性赋值 2. 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
电动车:
增加“电池品牌”属性 2. 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
三轮车:
在无参构造中实现对轮子属性值进行修改 2. 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
- 都是非机动车,具有非机动车的基本特征 2. 都有运行的方法
package java0417;
/**
* 非机动车类
* @author Clearlove7
*
*/
public class NonMotorVehicle {
private String brand; // 品牌
private String color; // 颜色
private int wheel = 2; // 车轮(数量默认为2)
private int seat = 1; // 座椅(数量默认为1)
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 NonMotorVehicle(String brand, String color, int wheel, int seat) {
super();
this.setBrand(brand);
this.setColor(color);
this.setWheel(wheel);
this.setSeat(seat);
}
public NonMotorVehicle(String brand, String color) {
super();
this.setBrand(brand);
this.setColor(color);
}
public NonMotorVehicle() {
super();
}
public String print() {
String str = "父类信息测试:这是一辆" + this.getColor() + "的," + this.getBrand() + "的非机动车,有" + this.getWheel() + "个轮子,有" + this.getSeat() + "个座椅";
return str;
}
}
package java0417;
/**
* 自行车类
* @author Clearlove7
*
*/
public class Bicycle extends NonMotorVehicle {
public Bicycle(String brand, String color) {
super(brand, color);
}
@Override
public String print() {
String str = "自行车类信息测试:这是一辆" + this.getColor() + "的," + this.getBrand() + "的自行车";
return str;
}
}
package java0417;
/**
* 电动车类
* @author Clearlove7
*
*/
public class ElectricBicycle extends NonMotorVehicle {
private String batteryBrand; // 电池品牌
public String getBatteryBrand() {
return batteryBrand;
}
public void setBatteryBrand(String batteryBrand) {
this.batteryBrand = batteryBrand;
}
public ElectricBicycle(String batteryBrand) {
super();
this.batteryBrand = batteryBrand;
}
public ElectricBicycle() {
super();
}
@Override
public String print() {
String str = "电动车类信息测试:这是一辆使用" + this.getBatteryBrand() + "电池的电动车";
return str;
}
}
package java0417;
/**
* 三轮车类
* @author Clearlove7
*
*/
public class Tricycle extends NonMotorVehicle {
public Tricycle() {
super();
this.setWheel(3);
}
@Override
public String print() {
String str = "三轮车类信息测试:三轮车是一款有" +this.getWheel() + "个轮子的非机动车";
return str;
}
}
package java0417;
/**
* 测试类
* @author Clearlove7
*
*/
public class TestV {
public static void main(String[] args) {
NonMotorVehicle no = new NonMotorVehicle("红颜色", "天宇牌", 4, 2);
System.out.println(no.print());
//父类信息测试:这是一辆天宇牌的,红颜色的非机动车,有4个轮子,有2个座椅
Bicycle bi = new Bicycle("捷安特", "黄色");
System.out.println(bi.print());
//自行车类信息测试:这是一辆黄色的,捷安特的自行车
ElectricBicycle el = new ElectricBicycle("飞鸽牌");
System.out.println(el.print());
//电动车类信息测试:这是一辆使用飞鸽牌电池的电动车
Tricycle ti = new Tricycle();
System.out.println(ti.print());
//三轮车类信息测试:三轮车是一款有3个轮子的非机动车
}
}
2、 请使用面向对象的思想,设计自定义类Person继承Object类,重写toString方法实现对象信息输 出。
思路分析
创建一个 Person 类继承自 Object,其中类的结构要求为:
属性:name(姓名)、age(年龄)、sex(性别)
方法:
创建带参(name、age、sex为参数)构造方法 重写 toString 方法,输出信息格式为:姓名:** 年龄:** 性别:**(其中,**为对象对应 属性值)
创建测试类,在测试方法中,实例化 Person对 象,并传入三个属性值。然后,分别通过直接 打印Person对象以及利用重写的 toString 方法,打印输出2行对象信息
package java0417;
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(String name, int age, String sex) {
super();
this.setName(name);
this.setAge(age);
this.setSex(sex);
}
public Person() {
super();
}
public String toString() {
String str = "姓名:" + this.getName() + " " + "年龄:" + this.getAge() + " " + "性别:" + this.getSex() ;
return str;
}
package java0417;
public class TestP {
public static void main(String[] args) {
Person pe1 = new Person("黎明", 18, "男");
pe1.toString();
System.out.println(pe1.toString());
//姓名:黎明 年龄:18 性别:男
Person pe2 = new Person("黎明", 18, "男");
pe2.toString();
System.out.println(pe2.toString());
//姓名:黎明 年龄:18 性别:男
}
}
-
请使用面向对象的思想,实现杨梅和仙人蕉的信息描述。
程序参考运行效果图如下:
思路分析:
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和它的重载方法
package lianxi;
/**
* 父类-水果类
* @author Clearlove7
*
*/
public class Fruits {
private String shape; // 形状
private String taste; // 口感
public void eat() {
System.out.println("水果可供人们食用");
}
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 Fruits(String shape, String taste) {
super();
this.shape = shape;
this.taste = taste;
}
public Fruits() {
super();
}
@Override
public boolean equals(Object obj) {
if(obj == null) {
return false;
} else {
Fruits fru = (Fruits)obj;
if(this.getShape().equals(fru.getShape()) && this.getTaste().equals(fru.getTaste())) {
return true;
} else {
return false;
}
}
}
}
package lianxi;
public final class Waxberry extends Fruits {
private String color;
public final void face() {
System.out.println("杨梅:" + this.color + "、" + this.getShape() + ",果味酸甜适中");
}
@Override
public void eat() {
System.out.println("杨梅酸甜适中,非常好吃");
}
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;
}
@Override
public String toString() {
return "杨梅的信息:" + this.getShape() + this.color + this.getTaste() + "];";
}
}
package lianxi;
public class Banana extends Fruits{
private String variety; // 品种
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
public Banana(String shape, String taste, String variety) {
super(shape, taste);
this.variety = variety;
}
public void adventage() {
System.out.println(this.getVariety() + "果形" + this.getShape() + "果肉香甜,可供生食");
}
public void adventage(String color) {
System.out.println(this.getVariety() + "颜色为" + color );
}
}
package lianxi;
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));
System.out.println("————————————————————————————————————————————————————————————");
Waxberry wb = new Waxberry("紫红色", "酸甜适中非常好吃", "圆形");
wb.face();
wb.eat();
System.out.println(wb.toString());
Banana ba = new Banana("短而稍圆", "" , "仙人蕉");
ba.adventage();
ba.adventage("黄色");
}
}