定义一个类 Passbook ( 银行卡 / 银行存折) , 描述 银行卡 的特征
1. 字段:
卡号
余额
户主姓名
密码
2. 方法:
Balance( ) : 返回银行卡中的余额
bool saving ( double m ) :给银行卡中存钱
bool fetch( double m ) :从银行卡中取钱
bool shift( Passbook pb , (double m ) :转账
3. 定义构造函数给 字段赋值
4. 定义一个独立的类测试 Passbook 的相关方法
java 用eclipse做的面向对象的初级题,完全不知道怎么做,全靠网友了
答案:3 悬赏:70 手机版
解决时间 2021-03-21 01:09
- 提问者网友:绫月
- 2021-03-20 11:49
最佳答案
- 五星知识达人网友:胯下狙击手
- 2021-03-20 12:49
package bobo;
public class TestPassbook {
public static void main(String[] args) {
Passbook pb1 = new Passbook(111,111,2000,"jack");
Passbook pb2 = new Passbook(222,222,3000,"lucy");
pb1.Balance();
pb1.saving(3000);
pb1.fetch(10000);
pb1.fetch(2000);
pb1.shift(pb2, 20000);
pb1.shift(pb2, 1000);
}
}
class Passbook {
private int cardID;
private int password;
private double balance;
private String name;
public int getCardID() {
return cardID;
}
public void setCardID(int cardID) {
this.cardID = cardID;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Passbook(int cardID, int password, double balance, String name) {
this.cardID = cardID;
this.password = password;
this.balance = balance;
this.name = name;
}
//返回银行卡中的余额
public double Balance(){
System.out.println("余额:"+this.balance);
return this.balance;
}
//给银行卡中存钱
public boolean saving(double m){
this.balance += m;
System.out.println("存钱成功,余额:"+this.balance);
return true;
}
//从银行卡中取钱
public boolean fetch( double m ){
if(this.balance < m){
System.out.println("取钱失败,余额不足,余额只有:"+this.balance);
return false;
}else{
this.balance -= m;
System.out.println("取钱成功,余额:"+this.balance);
return true;
}
}
//转账
public boolean shift( Passbook pb , double m ){
if(this.balance < m){
System.out.println("转账失败,余额不足,余额只有:"+this.balance);
return false;
}else{
this.balance -= m;
System.out.println("转账成功,余额:"+this.balance);
pb.setBalance(pb.getBalance() + m);
return true;
}
}
}
余额:2000.0
存钱成功,余额:5000.0
取钱失败,余额不足,余额只有:5000.0
取钱成功,余额:3000.0
转账失败,余额不足,余额只有:3000.0
转账成功,余额:2000.0
public class TestPassbook {
public static void main(String[] args) {
Passbook pb1 = new Passbook(111,111,2000,"jack");
Passbook pb2 = new Passbook(222,222,3000,"lucy");
pb1.Balance();
pb1.saving(3000);
pb1.fetch(10000);
pb1.fetch(2000);
pb1.shift(pb2, 20000);
pb1.shift(pb2, 1000);
}
}
class Passbook {
private int cardID;
private int password;
private double balance;
private String name;
public int getCardID() {
return cardID;
}
public void setCardID(int cardID) {
this.cardID = cardID;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Passbook(int cardID, int password, double balance, String name) {
this.cardID = cardID;
this.password = password;
this.balance = balance;
this.name = name;
}
//返回银行卡中的余额
public double Balance(){
System.out.println("余额:"+this.balance);
return this.balance;
}
//给银行卡中存钱
public boolean saving(double m){
this.balance += m;
System.out.println("存钱成功,余额:"+this.balance);
return true;
}
//从银行卡中取钱
public boolean fetch( double m ){
if(this.balance < m){
System.out.println("取钱失败,余额不足,余额只有:"+this.balance);
return false;
}else{
this.balance -= m;
System.out.println("取钱成功,余额:"+this.balance);
return true;
}
}
//转账
public boolean shift( Passbook pb , double m ){
if(this.balance < m){
System.out.println("转账失败,余额不足,余额只有:"+this.balance);
return false;
}else{
this.balance -= m;
System.out.println("转账成功,余额:"+this.balance);
pb.setBalance(pb.getBalance() + m);
return true;
}
}
}
余额:2000.0
存钱成功,余额:5000.0
取钱失败,余额不足,余额只有:5000.0
取钱成功,余额:3000.0
转账失败,余额不足,余额只有:3000.0
转账成功,余额:2000.0
全部回答
- 1楼网友:底特律间谍
- 2021-03-20 14:30
楼上用的是接口来实现,你也可以用继承实现。对着下面看下关于类的继承和多态相关知识点就好理解了。
1. 先建立动物类animal(抽象类)。 然后弄个“唱歌”sing的方法(没有实现的抽象方法)
2. 然后派生出相应的子类,如:狗类,猫类,牛类,羊类四个类。
3. 在派生类中实现“唱歌”的方法,如system.out.println("喵喵喵")。
4. 最后建个测试类,弄个main方法,利用多态性初始化10个不用的动物。如:animal[10] a;
a[0] = new dog();
a[1] = new cat();
……
a[9] = new dog();
5. for循环唱歌,如:
for (int i = 0; i < 10; i++) {
a[i].sing();
}
- 2楼网友:春色三分
- 2021-03-20 13:58
public class Passbook{
private int id;
private double balance;
private String username;
private String password;
public Passbook(int id, double balance, String username, String password){
this.id = id;
this.balance = balance;
this.username = username;
this.password = password;
}
public double Balance(){
return balance;
}
public boolean saving(double balance){
this.balance = (this.balance + balance);
return true;
}
public boolean fetch(double balance){
this.balance = this.balance - balance;
return true;
}
public boolean shift(Passbook pd , double balance){
pd.balance = pd.balance + balance;
return true;
}
//还有一些异常啊小细节啊,自己试着修改一下吧。
//另外代码我手敲,没有运行,自己测试一下是否正确!
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯