1.bank类
package com.yfs.javase; //类封装 public class BankCard { //属性 int balance;//默认0 实例变量 分配给每个对象一个 //String name; // 方法 存钱 public void saveMoney(int money) { if(money < 0) { System.out.println("非法操作..."); return; } System.out.println("向账户存入" + money +"元..."); balance += money; } //取钱 调用时传入参数 public void drawMoney(int money) { //验证余额 if(balance < money) { System.out.println("账户余额不足..."); return; //终止方法调用 } System.out.println("从账户取出" + money + "元..."); balance -= money; } }
2.测试
package com.yfs.javase; public class CardTest { public static void main(String[] args) { // BankCard bc1 = null;//声明引用 // bc1 = new BankCard();//创建对象 BankCard bc1 = new BankCard(); System.out.println("银行卡余额:" + bc1.balance); bc1.drawMoney(200);//调用对象方法 bc1.saveMoney(5000); bc1.drawMoney(500000); bc1.drawMoney(500); bc1.saveMoney(-500); System.out.println("银行卡余额:" + bc1.balance); } }
3.测试1
package com.yfs.javase; public class CardTest2 { public static void main(String[] args) { BankCard bc1 = new BankCard(); System.out.println("银行卡余额:" + bc1.balance); bc1.saveMoney(5000); bc1.drawMoney(500); System.out.println("银行卡余额:" + bc1.balance); BankCard bc2 = new BankCard(); System.out.println("银行卡余额:" + bc2.balance); bc2.saveMoney(3000); System.out.println("bc2银行卡余额:" + bc2.balance); System.out.println("bc1银行卡余额:" + bc1.balance); } }
4.测试2
package com.yfs.javase; public class CardTest3 { public static void main(String[] args) { BankCard bc1 = new BankCard(); bc1.saveMoney(5000); bc1.drawMoney(500); System.out.println("银行卡余额:" + bc1.balance); BankCard bc2 = new BankCard(); bc2.saveMoney(3000); System.out.println("bc2银行卡余额:" + bc2.balance); bc2.drawMoney(5000); bc2.balance = 10000;//修改余额 不安全操作 bc2.drawMoney(5000); System.out.println("bc2银行卡余额:" + bc2.balance); } }
1.1bank类
package com.yfs.javase.update; //类封装 public class BankCard { //属性 private 属性私有 只能在当前类中访问 private int balance;// 实例变量 分配给每个对象一个 private int password; // 方法 存钱 public void saveMoney(int money) { if(money < 0) { System.out.println("非法操作..."); return; } System.out.println("向账户存入" + money +"元..."); balance += money;//在方法中访问 } //取钱 调用时传入参数 public void drawMoney(int money) { //验证余额 if(balance < money) { System.out.println("账户余额不足..."); return; //终止方法调用 } System.out.println("从账户取出" + money + "元..."); balance -= money; } //通过访问balance属性方法 不能修改 public void getBalance () { System.out.println("账户余额:" + balance); } }
2.1测试
package com.yfs.javase.update; public class CardTest3 { public static void main(String[] args) { BankCard bc1 = new BankCard(); bc1.saveMoney(5000); bc1.drawMoney(50000); // bc1.balance = 100000000;//编译出错 // System.out.println("银行卡余额:" + bc1.balance);//私有属性不能访问 bc1.getBalance(); // bc1.password; } }
3.1方法重载条件
package com.yfs.javase.update; import java.util.Random; public class Demo2 { // public int add(int a, int b) { // return a + b; // } //变量名称不能成为重载条件 // public int add(int c , int d) { // return c + d; // } public double add(int a , double b) { return a + b; } //返回值类型不能成为重载条件 public long add( int a, int b) { return a + b; } public static void main(String[] args) { Demo2 d1 = new Demo2(); long val = d1.add(1,6); } }
4.1方法重载示例
package com.yfs.javase.update; public class OverRideDemo { //方法的重载 方法使用相同的名字 public int add (int a, int b) { return a + b; } public int add (int a , int b, int c) { return a + b + c; } public double add( double a, double b) { return a + b; } public static void main(String[] args) { //创建对象 OverRideDemo od = new OverRideDemo(); int val = od.add(3,5); System.out.println("add(3,5) = " + val); val = od.add(3,5,8);//根据调用时传入参数个数 类型匹配对应方法 System.out.println("add1(3,5,8) = " + val); double result = od.add(3.14, 2.71); System.out.println("add(3.14, 2.71) = " + result); } }
时间: 2024-10-06 17:56:53