/**
* 【练习题】1.继承:
定义一个名为Vehicles(交通工具)的基类,该类中应包含String类型的成员属性brand(商标)和color(颜色),
还应包含成员方法run(行驶,在控制台显示“我已经开动了”)和showInfo(显示信息,在控制台显示商标和颜色),并编写构造方法初始化其成员属性。
编写Car(小汽车)类继承于Vehicles类,增加int型成员属性seats(座位),还应增加成员方法showCar(在控制台显示小汽车的信息),
并编写构造方法。 编写Truck(卡车)类继承于Vehicles类,增加float型成员属性load(载重),
还应增加成员方法showTruck(在控制台显示卡车的信息),并编写构造方法。 在main方法中测试以上各类。
*/
package com.Oracle.oop3;
public class Car01 extends Vehicles {
int seats;
public int getSeats() {
return seats;
}
public void setSeats(int seats) {
this.seats = seats;
}
public void showCar() {
showInfo();
System.out.println("seats:"+seats);
}
public Car01(String brand,String color,int seats) {
super();
this.brand=brand;
this.color=color;
this.seats = seats;
}
}
package com.Oracle.oop3;
public class Truck extends Vehicles {
float load;
public float getLoad() {
return load;
}
public void setLoad(float load) {
this.load = load;
}
public void showTruck() {
showInfo();
System.out.println("load:"+load);
}
public Truck(String brand,String color,float load) {
super();
this.brand=brand;
this.color=color;
this.load = load;
}
}
package com.Oracle.oop3;
public class Inherit01 {
public static void main(String[] args) {
Car01 c=new Car01("Audi","blue",6);
c.showCar();
c.run();
c.showInfo();
Truck t=new Truck("奔腾","brown",15.9f);
t.showTruck();
t.run();
}
}
/**
* 【练习题】2.继承:
编写一个类Calculate1,实现加、减两种运算,然后,编写另一个派生类Calculate2,实现乘、除两种运算。
*/
package com.Oracle.oop3;
public class Calculate1 {
public int plus(int a,int b) {
return a+b;
}
public int minus(int a,int b) {
return a-b;
}
}
package com.Oracle.oop3;
public class Calculate2 extends Calculate1{
public int multiply(int a,int b) {
return a*b;
}
public double divide(double a,double b) {
return a/b;
}
}
package com.Oracle.oop3;
public class Inherit02 {
public static void main(String[] args) {
Calculate2 c=new Calculate2();
System.out.println(c.plus(9, 7));
System.out.println(c.minus(17, 21));
System.out.println(c.multiply(7, 17));
System.out.println(c.divide(86, 3));
}
}
/**
* 【练习题】3.包、继承、构造函数
编写一个控制台应用程序,完成下列功能,并回答提出的问题。
1) 创建一个类A,在构造函数中输出“A”,再创建一个类B,在构造函数中输出“B”。
2) 从A继承一个名为C的新类,并在C内创建一个成员变量B。不要为C创建构造函数。
3) 在Main方法中创建类C的一个对象,写出运行程序后输出的结果。
4) 如果在C中也创建一个构造函数输出“C”,整个程序运行的结果又是什么?
*/
package com.Oracle.oop3;
public class A {
public A(){
System.out.println("A");
}
}
package com.Oracle.oop3;
public class B {
public B() {
System.out.println("B");
}
}
package com.Oracle.oop3;
public class C extends A {
B b;
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
public C() {
System.out.println("C");
}
}
package com.Oracle.oop3;
public class Inherit03 {
public static void main(String[] args) {
C c=new C();
c.getB();
}
}
/**
* 【练习题】4.继承:
建立三个类:居民、成人、官员。居民包含身份证号、姓名、出生日期,而成人继承自居民,多包含学历、职业两项数据;
官员则继承自成人,多包含党派、职务两项数据。要求每个类的字段都以属性的方式对外提供数据输入输出的功能。
*/
package com.Oracle.oop3;
public class Resident {
long id;
String name;
String birth;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
}
package com.Oracle.oop3;
public class Adult extends Resident {
String diplomas;
String occupation;
public String getDiplomas() {
return diplomas;
}
public void setDiplomas(String diplomas) {
this.diplomas = diplomas;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
}
package com.Oracle.oop3;
public class Officer extends Adult{
String groups;
String duty;
public String getGroups() {
return groups;
}
public void setGroups(String groups) {
this.groups = groups;
}
public String getDuty() {
return duty;
}
public void setDuty(String duty) {
this.duty = duty;
}
}
package com.Oracle.oop3;
public class Inherit04 {
public static void main(String[] args) {
Officer o=new Officer();
o.setId(1000);
o.setName("习大大");
o.setBirth("1960年6月6日");
o.setDiplomas("博士后");
o.setOccupation("主席");
o.setGroups("共产党");
o.setDuty("管理中国");
System.out.println("id: "+o.getId());
System.out.println("name: "+o.getName());
System.out.println("birth: "+o.getBirth());
System.out.println("diplomas: "+o.getDiplomas());
System.out.println("occupation: "+o.getOccupation());
System.out.println("groups: "+o.getGroups());
System.out.println("duty: "+o.getDuty());
}
}
/**
* 【练习题】5.覆盖、继承
建立一个汽车Auto类,包括轮胎个数,汽车颜色,车身重量、速度等成员变量。并通过不同的构造方法创建实例。
至少要求: 汽车能够加速,减速,停车。 再定义一个小汽车类Car,继承Auto,并添加空调、CD等成员变量,覆盖加速,减速的方法
*/
package com.Oracle.oop3;
public class Auto {
int tyre=4;
String color;
int weight=100;
int speed=60;
public int getTyre() {
return tyre;
}
public void setTyre(int tyre) {
this.tyre = tyre;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public Auto(int tyre, String color, int weight, int speed) {
super();
this.tyre = tyre;
this.color = color;
this.weight = weight;
this.speed = speed;
}
public Auto(int tyre, String color, int weight) {
super();
this.tyre = tyre;
this.color = color;
this.weight = weight;
}
public Auto(String color, int weight, int speed) {
super();
this.color = color;
this.weight = weight;
this.speed = speed;
}
public Auto() {
}
public void speedUp(int speed) {
this.speed+=speed;
System.out.println("您的汽车要加速:"+speed+"现在的速度为:"+this.speed);
}
public void speedDown(int speed) {
this.speed-=speed;
System.out.println("您的汽车要减速:"+speed+"现在的速度为:"+this.speed);
}
public void speedStop() {
this.speed=0;
System.out.println("您要停车,速度已降为0");
}
}
package com.Oracle.oop3;
public class Car05 extends Auto{
boolean air;
String cd;
public boolean isAir() {
return air;
}
public void setAir(boolean air) {
this.air = air;
}
public String getCd() {
return cd;
}
public void setCd(String cd) {
this.cd = cd;
}
public void speedUp(int speed) {
this.speed+=speed;
System.out.println("您的小汽车已经加速了:"+speed+"现在的速度为:"+this.speed);
}
public void speedDown(int speed) {
this.speed-=speed;
System.out.println("您的汽车要减速:"+speed+"现在的速度为:"+this.speed);
}
public Car05() {
}
}
package com.Oracle.oop3;
public class Inherit05 {
public static void main(String[] args) {
@SuppressWarnings("unused")
Auto a=new Auto("blue",100,65);
Car05 c=new Car05();
c.speedUp(15);
c.speedDown(23);
c.speedStop();
}
}
/**
* 写一个用户程序测试Account类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%的Account对象。
使用withdraw方法提款30000元,并打印余额。
再使用withdraw方法提款2500元,使用deposit方法存款3000元,然后打印余额和月利率。
* 要求:写一个用户程序测试CheckAccount类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%,可透支限额为5000元的CheckAccount对象。
使用withdraw方法提款5000元,并打印账户余额和可透支额。
再使用withdraw方法提款18000元,并打印账户余额和可透支额。
再使用withdraw方法提款3000元,并打印账户余额和可透支额。
*/
/**
* 2、创建Account类的一个子类CheckAccount代表可透支的账户,该账户中定义一个属性overdraft代表可透支限额。
* 在CheckAccount类中重写withdraw方法,其算法如下:如果(取款金额<账户余额),可直接取款
如果(取款金额>账户余额),计算需要透支的额度。判断可透支额overdraft是否足够支付本次透支需要,如果可以,将账户余额修改为0,冲减可透支金额
如果不可以,提示用户超过可透支额的限额
*/
package com.Oracle.oop3;
import com.Oracle.oop2.Account;
public class CheckAccount extends Account{
double overdraft=5000; //可透支金额
public CheckAccount(int id, double balance, double annualInterestRate, double overdraft) {
super(id, balance, annualInterestRate);
this.overdraft=overdraft;
// TODO Auto-generated constructor stub
}
public void display() {
System.out.println("id:"+super.getId()+", balance:"+super.getBalance()+", annualInterestRate:"+super.getAnnualInterestRate()+", overdraft:"+this.overdraft);
}
public void displayBalanceAndOverdraft() {
System.out.println("balance:"+super.getBalance()+", overdraft:"+this.overdraft);
}
public void withdraw (double amount) {
if(super.getBalance()>=amount) {
super.setBalance((super.getBalance()-amount));
}else if(super.getBalance()+this.overdraft>=amount){
super.setBalance(0);
this.overdraft-=this.overdraft-(amount-super.getBalance());
}else {
System.out.println("超过可透支额的限额");
}
}
}
package com.Oracle.oop3;
public class Inherit06 {
public static void main(String[] args) {
// Account a=new Account(1122,20000,0.045);
// a.display();
// a.withdraw(30000);
// a.displayBalance();
// a.withdraw(2500);
// a.deposit(3000);
// a.displayBalanceAndAnnualInterestRate();
CheckAccount c=new CheckAccount(1122,20000,0.045,5000);
c.display();
c.withdraw(18000);
c.displayBalanceAndOverdraft();
c.withdraw(3000);
c.displayBalanceAndOverdraft();
}
}
注:Account类在:OOP基础习题二