【练习题】1.继承:
定义一个名为Vehicles(交通工具)的基类(父类),该类中应包含String类型的成员属性brand(商标)和color(颜色),还应包含成员方法run(行驶,在控制台显示“我已经开动了”)和showInfo(显示信息,在控制台显示商标和颜色),并编写构造方法初始化其成员属性。
编写Car(小汽车)类继承于Vehicles类,增加int型成员属性seats(座位),还应增加成员方法showCar(在控制台显示小汽车的信息),并编写构造方法。 编写Truck(卡车)类继承于Vehicles类,增加float型成员属性load(载重),还应增加成员方法showTruck (在控制台显示卡车的信息),并编写构造方法。 在main方法中测试以上各类。
代码实现:
1、Vehicles类:
package home.work;
public class T1_Vehicles {
String brand;// 商标
String color;
public T1_Vehicles() {
}
public T1_Vehicles(String brand, String color) {
this.brand = brand;
this.color = color;
}
public void run() {
System.out.println("我已经开动了!");
}
public void showInfo() {
System.out.println("商标是:" + brand + ";颜色是:" + color);
}
}
2、Car类:
package home.work;
public class T1_Car extends T1_Vehicles {
int seats;// 座位
public T1_Car() {
}
public T1_Car(int seats, String brand, String color) {
super.brand = brand;
super.color = color;
this.seats = seats;
}
public void showCar() {
System.out.println("商标是:" + brand + ";颜色是:" + color + ";座位是" + seats + "个");
}
}
3、Truch类:
package home.work;
public class T1_Truck extends T1_Vehicles {
float load;// 载重
public T1_Truck(float load, String brand, String color) {
super.brand = brand;
super.color = color;
this.load = load;
}
public void showTruck() {
System.out.println("商标是:" + brand + ";颜色是:" + color + ";载重是:" + load + "吨");
}
}
4、Text测试类:
package home.work;
public class T1_Text {
public static void main(String[] args) {
T1_Vehicles v = new T1_Vehicles("奥迪", "白色");
v.showInfo();
T1_Car c = new T1_Car(5, "奔驰", "黑色");
c.showCar();
T1_Truck t = new T1_Truck(20.5f, "弗兰德", "蓝色");
t.showTruck();
}
}
运行结果展示:
【练习题】2.继承:
编写一个类Calculate1,实现加、减两种运算,然后,编写另一个派生类Calculate2,实现乘、除两种运算。
代码实现:
1、Calculate1类:
package home.work;
public class T2_Calculate1 {
public double add(double a, double b) {
System.out.println(a + "+" + b + "=" + (a + b));
return a + b;
}
public double app(double a, double b) {
System.out.println(a + "-" + b + "=" + (a - b));
return a - b;
}
}
2、Calculate2类:
package home.work;
public class T2_Calculate2 extends T2_Calculate1 {
public double cheng(double a, double b) {
System.out.println(a + "*" + b + "=" + (a * b));
return a * b;
}
public double chu(double a, double b) {
System.out.println(a + "/" + b + "=" + (a / b));
return a / b;
}
}
3、Text测试类:
package home.work;
public class T2_Text {
public static void main(String[] args) {
T2_Calculate2 c = new T2_Calculate2();
c.add(10.1, 9.9);
c.app(10.1, 5.1);
c.cheng(5, 5);
c.chu(10, 2);
}
}
运行结果展示:
【练习题】3.继承:
建立三个类:居民、成人adult、官员Official。居民包含身份证号、姓名、出生日期,而成人继承自居民,多包含学历、职业两项数据;官员则继承自成人,多包含党派、职务两项数据。要求每个类的字段都以属性的方式对外提供数据输入输出的功能。Setter getter 方法
代码实现:
1、居民JuMin类:
package home.work;
public class T4_JuMin {
private String id;
private String name;
private String date;
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setDate(String date) {
this.date = date;
}
public String getDate() {
return date;
}
}
2、成人Adult类:
package home.work;
public class T4_Adult extends T4_JuMin {
private String xunLi;
private String zhiYe;
public void setXunLi(String xunLi) {
this.xunLi = xunLi;
}
public String getXunLi() {
return xunLi;
}
public void setZhiYe(String zhiYe) {
this.zhiYe = zhiYe;
}
public String getZhiYe() {
return zhiYe;
}
}
3、官员Official类:
package home.work;
public class T4_Official extends T4_Adult {
private String dangPai;
private String zhiWu;
public void setDangPai(String dangPai) {
this.dangPai = dangPai;
}
public String getDangPai() {
return dangPai;
}
public void setZhiWu(String zhiWu) {
this.zhiWu = zhiWu;
}
public String getZhiWu() {
return zhiWu;
}
}
4、测试类Text类:
package home.work;
public class T4_Text {
public static void main(String[] args) {
T4_JuMin j = new T4_JuMin();
j.setName("张三");
j.setId("230125198812210124");
j.setDate("1988年12月21日");
System.out.println(j.getName() + "\t" + j.getId() + "\t" + j.getDate());
T4_Adult a = new T4_Adult();
a.setXunLi("本科");
a.setZhiYe("教师");
a.setName("张三");
a.setId("230125198812210124");
a.setDate("1988年12月21日");
System.out.println(
a.getName() + "\t" + a.getId() + "\t" + a.getDate() + "\t" + a.getXunLi() + "\t" + a.getZhiYe());
T4_Official o = new T4_Official();
o.setDangPai("共产党");
o.setZhiWu("交Java");
o.setXunLi("本科");
o.setZhiYe("教师");
o.setName("张三");
o.setId("230125198812210124");
o.setDate("1988年12月21日");
System.out.println(o.getName() + "\t" + o.getId() + "\t" + o.getDate() + "\t" + o.getXunLi() + "\t"
+ o.getZhiYe() + "\t" + o.getDangPai() + "\t" + o.getZhiWu());
}
}
运行结果展示:
【练习题】4.覆盖(重写 @Override)、继承
建立一个汽车Auto类,包括轮胎(wheel)个数,汽车颜色,车身重量、速度等成员变量。并通过不同的构造方法创建实例。至少要求: 汽车能够加速,减速,停车。 再定义一个小汽车类Car,继承Auto,并添加空调、CD等成员变量,覆盖(重写)加速,减速的方法。
代码实现:
1、汽车Auto类:
package home.work;
public class T5_Auto {
int wheel;
String color;
double weight;// 单位 :吨
double speed;// 单位 :千米/小时
public T5_Auto() {
}
public T5_Auto(int wheel, String color, double weight, double speed) {
this.color = color;
this.speed = speed;
this.weight = weight;
this.wheel = wheel;
}
public void showInfo() {
System.out.println("该汽车有" + wheel + "个轱辘," + "颜色为" + color + ",重量为" + weight + "吨,当前速度为" + speed + "千米/小时。");
}
public void speedAdd(double speedAdd) {
if (speed + speedAdd <= 120) {
speed += speedAdd;
System.out.println("加速成功!加速" + speedAdd + "千米/小时" + ",当前速度为:" + speed + "千米/小时");
} else {
System.out.println("超过最大加速度,加速失败!");
}
}
public void speedApp(double speedAdd) {
if (speed - speedAdd >= 0) {
speed -= speedAdd;
System.out.println("减速成功!减速" + speedAdd + "千米/小时" + ",当前速度为:" + speed + "千米/小时");
} else {
System.out.println("超过最大减速度,减速失败!");
}
}
public void park() {
speed = 0;
System.out.println("当前速度为:0千米/小时,停车成功!");
}
}
2、小汽车Car类:
package home.work;
public class T5_Car extends T5_Auto {
String kongTiao;
String CD;
public T5_Car() {
}
public T5_Car(String kongTiao, String CD, int wheel, String color, double weight, double speed) {
super.color = color;
super.speed = speed;
super.weight = weight;
super.wheel = wheel;
this.CD = CD;
this.kongTiao = kongTiao;
}
public void showInfo() {
System.out.println("该汽车有" + wheel + "个轱辘," + "颜色为" + color + ",重量为" + weight + "吨,当前速度为" + speed + "千米/小时,"
+ "空调处于" + kongTiao + "状态," + "CD处于" + CD + "状态。");
}
@Override
public void speedAdd(double speedAdd) {
if (speed + speedAdd <= 200) {
speed += speedAdd;
System.out.println("加速成功!加速" + speedAdd + "千米/小时" + ",当前速度为:" + speed + "千米/小时");
} else {
System.out.println("超过最大加速度,加速失败!");
}
}
@Override
public void speedApp(double speedAdd) {
if (speed - speedAdd >= 0) {
speed -= speedAdd;
System.out.println("减速成功!减速" + speedAdd + "千米/小时" + ",当前速度为:" + speed + "千米/小时");
} else {
System.out.println("超过最大减速度,减速失败!");
}
}
}
3、测试类Text类:
package home.work;
public class T5_Text {
public static void main(String[] args) {
T5_Auto a = new T5_Auto(10, "蓝色", 10.5, 50.5);
a.showInfo();
a.speedAdd(30.5);
a.speedApp(30.5);
a.speedAdd(100);
a.speedApp(100);
a.park();
System.out.println("=====================================");
T5_Car c = new T5_Car("工作", "关闭", 4, "红色", 2.5, 120);
c.showInfo();
c.speedAdd(50.5);
c.speedApp(50.5);
c.speedAdd(80.1);
c.speedApp(120.1);
c.park();
}
}
运行结果展示:
【练习题】5.类的继,super
1)、写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方法(getter和setter方法),返回月利率的方法getMonthlyInterest(),取款方法withdraw(),存款方法deposit()。
Account
private int id
private double balance
private double annualInterestRate
public Account (int id, double balance, double annualInterestRate )
public int getId()
public double getBalance()
public double getAnnualInterestRate()
public void setId( int id)
public void setBalance(double balance)
public void setAnnualInterestRate(double annualInterestRate)
public double getMonthlyInterest()
public void withdraw (double amount)
public void deposit (double amount)
写一个用户程序测试Account类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%的Account对象。使用withdraw方法提款30000元,并打印余额。
再使用withdraw方法提款2500元,使用deposit方法存款3000元,然后打印余额和月利率。
提示:在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。
运行结果如图所示:
2)、创建Account类的一个子类CheckAccount代表可透支的账户,该账户中定义一个属性overdraft代表可透支限额。在CheckAccount类中重写withdraw方法,其算法如下:
如果(取款金额<账户余额),
可直接取款
如果(取款金额>账户余额),
计算需要透支的额度
判断可透支额overdraft是否足够支付本次透支需要,如果可以
将账户余额修改为0,冲减可透支金额
如果不可以
提示用户超过可透支额的限额
要求:写一个用户程序测试CheckAccount类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%,可透支限额为5000元的CheckAccount对象。
使用withdraw方法提款5000元,并打印账户余额和可透支额。
再使用withdraw方法提款18000元,并打印账户余额和可透支额。
再使用withdraw方法提款3000元,并打印账户余额和可透支额。
提示:
(1) 子类CheckAccount的构造方法需要将从父类继承的3个属性和子类自己的属性全部初始化。
(2) 父类Account的属性balance被设置为private,但在子类CheckAccount的withdraw方法中需要修改它的值,因此好好理解 super.setBalance(super.getBalance() - amount);这句话的涵义,使用好getter,setter方法在子类覆盖方法中调用父类成员变量。
1、Account类:
package home.work;
public class T6_Account {
private int id;
private double balance;
private double annualInterestRate;
public T6_Account() {
}
public T6_Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public void setId(int id) {
this.id = id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public double getMonthlyInterest() {
return annualInterestRate / 12;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("取款成功,取款金额 " + amount + " 元");
System.out.println("余额为:" + balance + "元");
} else {
System.out.println("余额不足,取款" + amount + "元失败!");
System.out.println("余额为:" + balance + "元");
}
}
public void deposit(double amount) {
balance += amount;
System.out.println("存款成功,存款金额 " + amount + " 元");
System.out.println("余额为:" + balance + "元");
}
}
2、测试类Text1类:
package home.work;
public class T6_Text1 {
public static void main(String[] args) {
T6_Account a = new T6_Account();
a.setId(1122);
a.setBalance(20000);
a.setAnnualInterestRate(0.045);
a.withdraw(30000);
System.out.println("==================");
a.withdraw(2500);
System.out.println("==================");
a.deposit(3000);
System.out.println("==================");
double monthlyInterest = a.getMonthlyInterest();
System.out.println("月利率为:"+monthlyInterest);
}
}
3、CheckAccount类:
package home.work;
public class T6_CheckAccount extends T6_Account {
private double overdraft;// 可透支额度
public T6_CheckAccount() {
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
public double getOverdraft() {
return overdraft;
}
public void withdraw(double amount) {
if (amount <= super.getBalance()) {
super.setBalance(super.getBalance() - amount);
System.out.println("取款成功,取款金额 " + amount + " 元");
System.out.println("余额为:" + super.getBalance() + "元," + "可透支余额为:" + overdraft + "元。");
} else {
if (amount - overdraft <= super.getBalance()) {
overdraft -= (amount - super.getBalance());
System.out.println("取款成功,取款金额 " + amount + " 元");
System.out.println("账户已透支,透支金额为:" + (amount - super.getBalance()) + "元," + "账户可透支余额为:" + overdraft + "元。");
super.setBalance(0);
} else {
if (super.getBalance() > 0) {
System.out.println("余额不足,取款" + amount + "元失败!");
System.out.println("余额为:" + super.getBalance() + "元," + "可透支余额为:" + overdraft + "元," + "共计:"
+ (overdraft + super.getBalance()) + "元。");
} else {
System.out.println("余额为:0,可透支余额不足,取款" + amount + "元失败!");
System.out.println("可透支余额为" + overdraft + "元。");
}
}
}
}
}
4、测试类Text2类:
package home.work;
public class T6_Text2 {
public static void main(String[] args) {
T6_CheckAccount c = new T6_CheckAccount();
c.setId(1122);
c.setBalance(20000);
c.setAnnualInterestRate(0.045);
c.setOverdraft(5000);
c.withdraw(5000);
System.out.println("======================");
c.withdraw(18000);
System.out.println("======================");
c.withdraw(3000);
}
}
运行结果如下图所示:
Text1运行结果展示:
Text2运行结果展示:
来源:CSDN
作者:飘洋过海的小毛驴
链接:https://blog.csdn.net/qq_42487358/article/details/104214719