java类的继承作业

实验 类的继承,super

1、写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方法(getter和setter方法),返回月利率的方法getMonthlyInterest(),取款方法withdraw(),存款方法deposit()。
在这里插入图片描述
写一个用户程序测试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方法中需要修改它的值,因此应修改父类的balance属性,定义其为protected。

运行结果如下图所示:在这里插入图片描述
在这里插入图片描述
1.

public class Account {
	private int id;//账号
	protected double balance;//余额
	private double annualInterestRate;//年利率
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	public double getAnnualInterestRate() {
		return annualInterestRate;
	}
	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInterestRate = annualInterestRate;
	}
	public Account(int id, double balance, double annualInterestRate) {
		super();
		this.id = id;
		this.balance = balance;
		this.annualInterestRate = annualInterestRate;
	}
	public Account() {
		super();
	}
	//返回月利率的方法
	public double getMonthlyInterest(){
	  double MonInterest=this.annualInterestRate/1200;
		return MonInterest;
	}
	//取款方法
	public void withdraw (double amount){
		double with=this.balance-amount;
		
		if(with<0){
			System.out.println("余额不足!");
		}
		if(with>=0){
			this.balance=with;
			System.out.println("您的账户余额为:"+balance);
		}
	}
	//存款方法
	public void deposit (double amount){

		double  dep=this.balance+amount;
		this.balance=dep;
		System.out.println("您的账户余额为:"+balance);
	}
	

}

public class TestAccount {
	
	public static void main(String[] args) {
	  Account  ac=new Account(1122, 20000, 4.5);
	  ac.withdraw(30000);
	  ac.withdraw(2500);
	  ac.deposit(3000);
	  System.out.println("您的余额为:"+ac.getBalance());
	  System.out.println("月利率为:"+ac.getMonthlyInterest());
	}

}
public class CheckAccount extends Account {
	private double overdraft;

	public double getOverdraft() {
		return overdraft;
	}

	public void setOverdraft(double overdraft) {
		this.overdraft = overdraft;
	}

	public CheckAccount(int id, double balance, double annualInterestRate,
			double overdraft) {
		super(id, balance, annualInterestRate);
		this.overdraft = overdraft;
	}

	public CheckAccount() {
		super();
	}

	public void withdraw(double amount) {
		  
		if (amount < getBalance()) {
			double am = getBalance() - amount;
            balance=am;
			System.out.println("您的账户余额为:" + balance);
			System.out.println("您的可透支额为:" + getOverdraft());
		}
		if (amount >getBalance()) {

			if (amount < getBalance() + getOverdraft()) {
                
				double Over = getBalance() + getOverdraft() - amount;
				setBalance(0);
				overdraft=Over;
				System.out.println("您的账户余额为:" +getBalance());
				System.out.println("您的可透支额为:" + overdraft);
			} else {
				System.out.println("超过可透支额!");
			}
		}
	}
}

public class TestCheckAccount {

	public static void main(String[] args) {
		CheckAccount ch=new CheckAccount(1122, 20000,4.5, 5000);
        ch.withdraw(5000);
        ch.withdraw(18000);
        ch.withdraw(3000);
	}

}

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在给定的代码中,我们可以看到一个抽象类Vehicle和它的两个子类Car和RaceCar。每个类都有一个名为speed()的方法,返回不同的速度值。在TestCar类的main方法中,创建了一个RaceCar对象、一个Car对象和一个Vehicle对象,并通过调用speed()方法分别获取它们的速度值,并将结果打印输出。输出结果为"150, 60, 60"。 抽象类的意义在于它提供了一种定义接口的方式,它可以包含抽象方法和具体方法。抽象方法是没有实现的方法,需要在具体的子类中实现。抽象类可以作为基类,被其他类继承,并在子类中实现抽象方法。它可以提供一些公共的方法和属性,以及规范子类的行为。通过使用抽象类,我们可以实现代码的重用和扩展性的增强。 编写建设银行接口CCB继承银联接口,并实现该接口,可以在建设银行接口CCB中定义燃气费支付的方法,并在实现该接口的类中实现该方法。接口可以为不同的类提供一种共同的协议或契约,使得这些类可以按照接口中定义的方法进行操作。 正确的说法是接口定义了一组方法的规范,而抽象类可以包含方法的实现。接口可以被多个类实现,而抽象类只能被单继承。接口中的方法默认是公共的,而抽象类中的方法可以有不同的访问修饰符。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Java入门第67课——抽象类和接口作业](https://blog.csdn.net/houjunkang363/article/details/90726776)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值