JAVA基础day07

这篇博客介绍了JAVA基础中的类定义和对象操作。通过`Account`、`Boy`、`Customer`等类展示了如何定义属性和方法。还涵盖了继承的概念,如`Girl`继承自`Boy`,以及`Cylinder`类如何扩展`Circle`类。此外,讲解了如何在子类中调用父类的构造器和方法,并展示了`Customer`类的实例化以及`TestCustomer`类中对账户的操作,如存款、取款等。
摘要由CSDN通过智能技术生成

package com.atguigu.exer;

public class Account {
private int id;//账号
private double balance;//余额
private double annualInterestRate;//年利率

public Account(int id, double balance, double annualInterestRate) {
	this.id = id;
	this.balance = balance;
	this.annualInterestRate = 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 void withdraw (double amount){
	if(balance >= amount){
		balance -= amount;
		System.out.println("成功取出:" + amount);
	}else{
		System.out.println("余额不足,取款失败!");
	}
}
//存钱
public void deposit (double amount){
	balance += amount;
	System.out.println("成功存入:" + amount);
}

}

package com.atguigu.exer;

public class Boy {
private String name;
private int age;
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 void marry(Girl girl){
	System.out.println("我要娶" + girl.getName());
}

public void marry(Boy boy){
	System.out.println("我要娶" + boy.getName());
}

public void shout(){
	if(this.age >= 22){
		System.out.println("我到了结婚年龄了!");
	}else{
		System.out.println("还是先谈谈恋爱吧");
	}
}

}

package com.atguigu.exer;

public class Customer {
private String firstName;
private String lastName;
private Account account;

public Customer(String f, String l) {
	this.firstName = f;
	this.lastName = l;
}

public Account getAccount() {
	return account;
}

public void setAccount(Account account) {
	this.account = account;
}

public String getFirstName() {
	return firstName;
}

public String getLastName() {
	return lastName;
}

}

package com.atguigu.exer;

public class Girl {
private String name;

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public void marry(Boy boy){
	System.out.println("我要嫁给" + boy.getName());
	boy.marry(this);
}

}

package com.atguigu.exer;

public class TestBoyGirl {
public static void main(String[] args) {
Boy boy = new Boy();
boy.setName(“工藤新一”);
boy.setAge(23);

	Girl girl = new Girl();
	girl.setName("小兰");
	
	boy.marry(girl);
	boy.shout();
	System.out.println();
	girl.marry(boy);
	
	
}

}

package com.atguigu.exer;

/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值