类和对象,类定义了对象的特征和行为。属性,方法。
Posted 浅情孤唐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了类和对象,类定义了对象的特征和行为。属性,方法。相关的知识,希望对你有一定的参考价值。
1、编写一个ATM机类,有属性:所属银行、显示余额、,有方法:取款、存款、查询余额
/*
* 取款机实体类
* ATM
*/
public class ATM {
String affiliatedBank;// 声明所属银行
String keyboard; // 键盘
Double displayBalance; // 显示余额
String horn; // 喇叭
// 无参构造函数
public ATM() {
}
/*
* 封装属性,getXXX()取值,setXXXX()赋值
*/
public String getAffiliatedBank() {
return affiliatedBank;
}
public void setAffiliatedBank(String affiliatedBank) {
this.affiliatedBank = affiliatedBank;
}
public String getKeyboard() {
return keyboard;
}
public void setKeyboard(String keyboard) {
this.keyboard = keyboard;
}
public Double getDisplayBalance() {
return displayBalance;
}
public void setDisplayBalance(Double displayBalance) {
this.displayBalance = displayBalance;
}
public String getHorn() {
return horn;
}
public void setHorn(String horn) {
this.horn = horn;
}
// ATM取款方法
public void withdrawals(Double sum) {
Double withdrawalBalance; // 账户余额
System.out.println("欢迎使用" + affiliatedBank + "ATM01");
withdrawalBalance = displayBalance - sum;
System.out.println("ATM账户余额" + withdrawalBalance + "ATM011");
}
// ATM存款方法
public void deposits(Double sum1) {
Double withdrawalBalance; // 账户余额
System.out.println("欢迎使用" + affiliatedBank + "ATM02");
withdrawalBalance = displayBalance + sum1;
System.out.println("ATM账户余额" + withdrawalBalance + "ATM012");
}
// 查询存款后ATN余额方法
public void inquiries3(Double sum1) {
Double c = displayBalance + sum1; // ATM机余额
displayBalance = c;
System.out.println("欢迎使用" + affiliatedBank + "ATM03");
System.out.println("ATM账户余额" + displayBalance + sum1 + "ATM013");
}
// 取款后查询ATM余额方法
public void inquiries(Double sum1) {
Double c = displayBalance - sum1;
displayBalance = c;
System.out.println("欢迎使用" + affiliatedBank + "ATM03");
System.out.println("ATM账户余额" + displayBalance + "ATM013");
}
// 查询ATM余额方法
public void inquiries() {
System.out.println("欢迎使用" + affiliatedBank + "ATM03");
System.out.println("ATM账户余额" + displayBalance + "ATM013");
}
}
/*
* 储户实体类
*
*/
public class Depositor {
String accountNumber;// 账号
String affiliatedBank;// 所属银行
Double depositAmount;// 账户余额
ATM atm; // ATM 对象
Bank bank;// Bank 对象
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getAffiliatedBank() {
return affiliatedBank;
}
public void setAffiliatedBank(String affiliatedBank) {
this.affiliatedBank = affiliatedBank;
}
public Double getDepositAmount() {
return depositAmount;
}
public void setDepositAmount(Double depositAmount) {
this.depositAmount = depositAmount;
}
// 储户取款方法
public void withdrawals2(Double sum) {
Double withdrawalBalance; //账户余额
System.out.println("欢迎使用"+affiliatedBank+"ATM0");
withdrawalBalance=depositAmount-sum;
System.out.println("储户账户余额"+withdrawalBalance+"ATM01");
}
// 储户取存款方法
public void deposits2(Double sum1) {
Double withdrawalBalance; //账户余额
System.out.println("欢迎使用"+affiliatedBank+"ATM02");
withdrawalBalance=depositAmount+sum1;
System.out.println("储户账户余额"+withdrawalBalance+"ATM12");
}
// 取款查询余额方法
public void inquiries22(Double sum) {
double b=depositAmount-sum;
depositAmount=b;
System.out.println("欢迎使用"+affiliatedBank+"ATM031");
System.out.println("储户账户余额"+depositAmount+"ATM0131");
}
// 储户存款增加查询余额方法
public void inquiries2(Double sum) {
//atm 余额增加
//atm.inquiries3(sum);
double b=depositAmount+sum;
depositAmount=b;
System.out.println("欢迎使用"+affiliatedBank+"ATM031");
System.out.println("储户账户余额"+depositAmount+"ATM0131");
}
// 查询余额方法
public void inquiries2() {
System.out.println("欢迎使用"+affiliatedBank+"ATM03");
System.out.println("储户账户余额"+depositAmount+"ATM013");
}
}
/*
* 银行实体类
*
*/
public class Bank {
String bankName;// 银行名称
String address;// 地址
Double depositAmount;// 存款金额
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Double getDepositAmount() {
return depositAmount;
}
public void setDepositAmount(Double depositAmount) {
this.depositAmount = depositAmount;
}
// 银行取款方法
public void withdrawals1(Double sum) {
Double withdrawalBalance; // 账户余额
System.out.println("欢迎使用" + bankName + "柜台0");
withdrawalBalance = depositAmount - sum;
System.out.println("银行账户余额" + withdrawalBalance + "柜台00");
}
// 银行存款方法
public void deposits1(Double sum1) {
Double withdrawalBalance; // 账户余额
System.out.println("欢迎使用" + bankName + "柜台1");
withdrawalBalance = depositAmount + sum1;
System.out.println("银行账户余额" + withdrawalBalance + "柜台11");
}
// 查询银行余额方法
public void inquiries11(Double sum) {
Double a=depositAmount+sum;
depositAmount=a;
System.out.println("欢迎使用" + bankName + "柜台2");
System.out.println("银行账户余额" + depositAmount + "柜台22");
}
// 查询银行余额方法
public void inquiries1(Double sum) {
Double a=depositAmount-sum;
depositAmount=a;
System.out.println("欢迎使用" + bankName + "柜台2");
System.out.println("银行账户余额" + depositAmount + "柜台22");
}
// 查询银行余额方法不带参
public void inquiries1() {
System.out.println("欢迎使用" + bankName + "柜台21");
System.out.println("银行账户余额" + depositAmount + "柜台221");
}
}
以上是关于类和对象,类定义了对象的特征和行为。属性,方法。的主要内容,如果未能解决你的问题,请参考以下文章