栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

30天学会JAVA—练习题(2021韩顺平)——Day9

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

30天学会JAVA—练习题(2021韩顺平)——Day9


Test Demo Rose Jack John Jack

class BankAccount{
	private int balance;

	public BankAccount(int balance) {
		super();
		this.balance = balance;
	}
	
	
	public int getBalance() {
		return balance;
	}

	public void setBalance(int balance) {
		this.balance = balance;
	}

	public void deposit(double amount){
		balance += amount;
	}
	
	
	public void withdraw(double amount){
		balance -= amount;
	}
}

class CheckingAccount extends BankAccount{

	public CheckingAccount(int balance) {
		super(balance);
	}
	
	@Override
	public void deposit(double amount) {
		super.deposit(amount-1);
	}
	
	@Override
	public void withdraw(double amount) {
		super.withdraw(amount+1);
	}
	
}

class SavingsAccount extends BankAccount{
	private double rate = 0.01;
	private int count = 3;
	

	public double getRate() {
		return rate;
	}


	public void setRate(double rate) {
		this.rate = rate;
	}


	public int getCount() {
		return count;
	}


	public void setCount(int count) {
		this.count = count;
	}
	public SavingsAccount(int balance) {
		super(balance);
	}
	
	@Override
	public void deposit(double amount) {
		// 判断是否还有免手续费的机会
		if(count > 0){
			super.deposit(amount);
		}else{
			super.deposit(amount - 1);
		}
		count--;
	}
	
	@Override
	public void withdraw(double amount) {
		// 判断是否还有免手续费的机会
		if(count > 0){
			super.withdraw(amount);
		}else{
			super.withdraw(amount + 1);
		}
		count--;
	}
	
	public void earnMonthlyInterest(){//每个月初,统计上个月的利息,同时将count = 3
		count = 3;
		super.deposit(getBalance() * rate);
	}		
}

9. 代码
class Point{
	private double x;
	private double y;
	
	public Point(double x, double y) {
		super();
		this.x = x;
		this.y = y;
	}

	public double getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public double getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
}

class LabeledPoint extends Point{
	private String label;

	public LabeledPoint(double x, double y, String label) {
		super(x, y);
		this.label = label;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}	
}
10.代码
class Doctor{
	private String name;
	private int age;
	private String job;
	private char gender;
	private int sal;
	
	public Doctor(String name, int age, String job, char gender, int sal) {
		super();
		this.name = name;
		this.age = age;
		this.job = job;
		this.gender = gender;
		this.sal = sal;
	}

	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 getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public char getGender() {
		return gender;
	}

	public void setGender(char gender) {
		this.gender = gender;
	}

	public int getSal() {
		return sal;
	}

	public void setSal(int sal) {
		this.sal = sal;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Doctor){//判断是否为Doctor类
			if((((Doctor) obj).name.equals(name) &&
					((Doctor) obj).age == age &&
					((Doctor) obj).job.equals(job)&&
					((Doctor) obj).gender == gender&&
					((Doctor) obj).sal == sal)){
				return true;
			}		
		}
		return false;
	}	
}
public class Test {	
	public static void main(String[] args) {
		Doctor doctor1 = new Doctor("jack", 34, "doctor", '男', 2000);
		Doctor doctor2 = new Doctor("jack", 34, "doctor", '男', 2000);
		System.out.println(doctor1.equals(doctor2));
	}		
}

//向上转型[父类的引用指向子类的对象] 左编译,右运行
Person person = new Student();


//向下转型[强制转型](先向上转型之后,才可以向下转型)
Student student = (Student) person;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425610.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号