选择
1、在Java中,以下程序的输出结果是 A
2、在Java中,以下关于方法重载和方法重写描述正确的是 D
3、哪个选项中的方法插入到(1)处可以正确实现方法重写 C
4、在下面程序的注释1处补充上下列()方法,会导致在编译过程中发生错误 B
5、如下Java源文件,编译并运行Child.java后,以下结果描述正确的是 B
6、分析如下所示的Java代码,则选项中的说法正确的是 C
7、关于super的说法正确的是 C
8、阅读下面JAVA代码片段,正确的选项是 B
9、下列关于super和this的说法正确的是(多选) ABD
10、下列关于Object类的叙述错误的是 D
11、该段代码的运行结果为 D
12、在Java中,关于继承的说法错误的是 D
13、下列关于final的说法错误的是 C
编程
1、编程练习:某公司要开发“XX车行管理系统”,请使用面向对象的思想,设计自定义类描述自行车、电动车和三轮车。
程序参考运行效果图如下:
package project0721.demo1;
/**
* 父类:非机动车
*/
public class Vehicle {
// 成员属性
private String brand;
private String color;
private int wheel = 2;
private int seat = 1;
// get方法
public String getBrand() {
return brand;
}
public String getColor() {
return color;
}
public int getWheel() {
return wheel;
}
public int getSeat() {
return seat;
}
// set方法
public void setBrand(String brand) {
this.brand = brand;
}
public void setColor(String color) {
this.color = color;
}
public void setWheel(int wheel) {
this.wheel = wheel;
}
public void setSeat(int seat) {
this.seat = seat;
}
// 构造器
public Vehicle() {
super();
}
public Vehicle(String brand, String color) {
super();
this.setBrand(brand);
this.setColor(color);
}
public Vehicle(String brand, String color, int wheel, int seat) {
super();
this.setBrand(brand);
this.setColor(color);
this.setWheel(wheel);
this.setSeat(seat);
}
// 成员方法
/**
* 描述非机动车运行的方法
* @return 字符串
*/
public String function() {
String str = "这是一辆" + this.getColor() + "颜色的,";
str += this.getBrand() + "牌的非机动车,有";
str += this.getWheel() + "个轮子,有";
str += this.getSeat() + "个座椅";
return str;
}
}
package project0721.demo1;
/**
* 子类:自行车类
*/
public class Bicycle extends Vehicle {
// 构造器
public Bicycle() {
super();
}
public Bicycle(String brand, String color) {
super(brand, color);
}
// 成员方法
/**
* 重写描述自行车运行的方法
*/
@Override
public String function() {
String str = "这是一辆" + this.getColor() + "颜色的,";
str += this.getBrand() + "牌的自行车";
return str;
}
}
package project0721.demo1;
/**
* 子类:电动车类
*/
public class ElecBicycle extends Vehicle {
// 成员属性
private String battery;
// get方法
public String getBattery() {
return battery;
}
// set方法
public void setBattery(String battery) {
this.battery = battery;
}
// 构造器
public ElecBicycle() {
super();
}
public ElecBicycle(String battery) {
super();
this.setBattery(battery);
}
// 成员方法
/**
* 重写描述电动车运行的方法
*/
@Override
public String function() {
String str = "这是一辆使用" + this.getBattery() + "牌电池的电动车";
return str;
}
}
package project0721.demo1;
/**
* 子类:三轮车类
*/
public class Tricycle extends Vehicle {
// 构造器
public Tricycle() {
super();
this.setWheel(3);
}
// 成员方法
/**
* 重写描述三轮车运行的方法
*/
@Override
public String function() {
String str = "三轮车是一款有" + this.getWheel() + "个轮子的非机动车";
return str;
}
}
package project0721.demo1;
/**
* 测试类
*/
public class Test {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle("天宇","红",4,2);
System.out.println("父类信息测试:" + vehicle.function());
Bicycle bicycle = new Bicycle("捷安特","黄");
System.out.println("自行车类信息测试:" + bicycle.function());
ElecBicycle elecBicycle = new ElecBicycle("飞鸽");
System.out.println("电动车类信息测试:" + elecBicycle.function());
Tricycle tricycle = new Tricycle();
System.out.println("三轮车类信息测试:" + tricycle.function());
}
}
2、请使用面向对象的思想,设计自定义类Person继承Object类,重写toString方法实现对象信息输出。
运行效果如下图所示:
package project0721.demo2;
public class Person {
// 成员属性
private String name;
private int age;
private String sex;
// get方法
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getSex() {
return sex;
}
// set方法
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
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);;
}
/**
* 重写toString方法
*/
@Override
public String toString() {
String str = "姓名:" + this.getName() + " ";
str += "年龄:" + this.getAge() + " ";
str += "性别:" + this.getSex();
return str;
}
}
package project0721.demo2;
public class Test {
public static void main(String[] args) {
Person person = new Person("李明",18,"男");
System.out.println(person);
System.out.println(person.toString());
}
}
3、请使用面向对象的思想,实现杨梅和仙人蕉的信息描述。
程序参考运行效果图如下:
思路分析:
package project0721.demo3;
/**
* 父类:水果类
*/
public class Fruits {
// 成员属性
private String shape;
private String taste;
// get方法
public String getShape() {
return shape;
}
public String getTaste() {
return taste;
}
// set方法
public void setShape(String shape) {
this.shape = shape;
}
public void setTaste(String taste) {
this.taste = taste;
}
// 构造器
public Fruits() {
super();
}
public Fruits(String shape, String taste) {
super();
this.setShape(shape);
this.setTaste(taste);
}
// 成员方法
/**
* 水果描述方法
*/
public void eat() {
System.out.println("水果可供人们食用!");
}
/**
* 重写equals方法
*/
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
} else {
Fruits fruits = (Fruits) obj;
if (this.getShape().equals(fruits.getShape()) && this.getTaste().equals(fruits.getTaste())) {
return true;
} else {
return false;
}
}
}
}
package project0721.demo3;
/**
* 子类:杨梅类
*/
public final class Waxberry extends Fruits {
// 成员属性
private String color;
// get方法
public String getColor() {
return color;
}
// set方法
public void setColor(String color) {
this.color = color;
}
// 构造器
public Waxberry() {
super();
}
public Waxberry(String shape, String taste, String color) {
super(shape, taste);
this.setColor(color);
}
// 成员方法
/**
* 描述杨梅的face方法
*/
public final void face() {
System.out.println("杨梅:" + this.getColor() + "、" + this.getShape() + "," + "果味" + this.getTaste());
}
/**
* 重写杨梅的eat方法
*/
@Override
public void eat() {
System.out.println("杨梅酸甜适中,非常好吃!");
}
/**
* 重写杨梅toString方法
*/
@Override
public String toString() {
String str = "杨梅的信息:果实为" + this.getShape() + "、" + this.getColor() + "," + this.getTaste() + ",非常好吃!";
return str;
}
}
package project0721.demo3;
/**
* 子类:仙人蕉类
*/
public class Banana extends Fruits {
// 成员方法
private String variety;
// get方法
public String getVariety() {
return variety;
}
// set方法
public void setVariety(String variety) {
this.variety = variety;
}
// 构造器
public Banana() {
super();
}
public Banana(String shape, String taste, String variety) {
super(shape, taste);
this.setVariety(variety);
}
// 成员方法
/**
* 描述仙人蕉优势的方法(无参)
*/
public void advantage() {
System.out.println(this.getVariety() + "果形" + this.getShape() + ",果肉" + this.getTaste() + ",可供生食。");
}
/**
* 描述仙人蕉优势的方法(带参)
* @param color 颜色
*/
public void advantage(String color) {
System.out.println(this.getVariety() + "颜色为" + color);
}
}
package project0721.demo3;
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 wax = new Waxberry("圆形", "酸甜适中", "紫红色");
wax.face();
wax.eat();
System.out.println(wax);
System.out.println("————————————————————————————————————————");
Banana ban = new Banana("短而稍圆", "香甜", "仙人蕉");
ban.advantage();
ban.advantage("黄色");
}
}