整理最近学习的Java
- 类和对象的定义
- 访问限定符:
-
public(公有的) default(默认包访问权限) protected(保护) private(私有)
- 对象大小 对象头(8字节(JVM位数有关)) + 实例变量 + 内存填充 (8的倍数)
- 方法调用设计到的内存结构
- 类的应用(MyArrayList,ATM机)
-
MyArrayList
public class MyArrayList { private int[] element;//定义数组 private static int size;//有效数组大小 private final int INITSIZE = 10;//初始化数组长度 public MyArrayList() {//构造函数初始化成员变量 element=new int[INITSIZE]; } public void addHead(int value) { ensureCapacity(); //将元素后移一位 for(int i=size-1;i>=0;i--) { element[i+1]=element[i]; } element[0]=value; size++; } //对数组扩容 private void ensureCapacity() { // TODO Auto-generated method stub if(size==element.length) { element=Arrays.copyOf(element,element.length+(element.length>>1)); } } //将插入的值赋给末尾元素,长度+1 public void addTail(int value) { ensureCapacity(); element[size++]=value; //size++; } private boolean isEmpty() { return size==0; } //1 2 2 3 public void removeValue(int value) { if(isEmpty()) { return; } for(int i=0;i<size;) { if(element[i]==value) { for(int j=i+1;j<size-i;j++) { element[j-1]=element[j]; } }else { i++; } } } public void removeHead() { if(isEmpty()) { return; } for(int i=1;i<size;i++) { element[i-1] = element[i]; } size--; } /** * 去掉尾元素,先判断是否为空,不则直接长度-1 */ public void removeTail() { if(isEmpty()) { return; } size--; } /** * * @param srcValue ֵ 原有值 * @param aimValue 目标值 */ public void change(int srcValue,int aimValue) { if(isEmpty()) { return; } for(int i=0;i<size;i++) { if(element[i] == srcValue) { element[i] = aimValue; } } } public boolean contains(int value) { for(int i=0;i<size;i++) { if(element[i] == value) { return true; } } return false; } public void show() { for(int i=0;i<size;i++) { System.out.println(element[i]+" "); } }
-
ATM
/** * ATM类 * 基于当前登录成功的这张卡进行操作 * 存款,取款,转账,查余额,退卡 * @author Ray * */ public class ATM { private Bank bank; private static Scanner scanner; public ATM() { bank = new Bank(); scanner=new Scanner(System.in); } private BankCard login(int id,int passwd){ return bank.contains(id,passwd); } //注册功能,在银行增加卡是否成功 private boolean register(int id,int passwd) { BankCard card=new BankCard(id,passwd); return bank.add(card); } public void start(){ while(true) { System.out.println("1. 登陆 2.注册 3.退出"); int choice = scanner.nextInt(); if(choice == 3){ break; } System.out.println("请输入账号密码:"); int id = scanner.nextInt(); int passwd = scanner.nextInt(); BankCard successCard=null; switch (choice){ case 1: successCard=login(id,passwd); if(successCard != null) { System.out.println("登陆成功"); loginSuccess(successCard); }else { System.out.println("登陆失败"); } break; case 2: if(register(id,passwd)) { System.out.println("注册成功"); }else { System.out.println("注册失败"); } break; } } } private void loginSuccess(BankCard successCard) { while(true) { System.out.println("1. 存款 2.取款 3.转账 4.余额 5.退卡"); int choice = scanner.nextInt(); if(choice == 5){ break; } switch(choice) { case 1: System.out.println("请输入存款金额"); int money = scanner.nextInt(); successCard.saveMoney(money); System.out.println("存款成功"); break; case 2: System.out.println("请输入取款金额"); money = scanner.nextInt(); if(successCard.withdrawMoney(money)) { System.out.println("取款成功"); }else { System.out.println("取款失败"); } break; case 3: System.out.println("请输入转账用户名和密码"); int id=scanner.nextInt(); int passwd = scanner.nextInt(); System.out.println("请输入转账金额"); money = scanner.nextInt(); BankCard userCard = bank.contains(id, passwd); if(userCard != null) { if(successCard.withdrawMoney(money)) {//当前卡取款成功 userCard.saveMoney(money); System.out.println("转账成功"); }else { System.out.println("余额不足"); } }else { System.out.println("无该用户"); } break; case 4: System.out.println("余额为:"+successCard.getMoney()); break; } } } } /** *银行类 **/ public class Bank { private BankCard[] cards;//10 private int size;//有效用户个数 private static final int INITSIZE = 10; public Bank() { cards=new BankCard[INITSIZE]; //this(INITSIZE) } public boolean add(BankCard card) { //如果之前注册过,增加失败 if(contains(card.getId(),card.getPasswd()) != null) { return false; } if(size == cards.length) {//数组已满,扩容 cards = Arrays.copyOf(cards, cards.length); } cards[size] = card; size++; return true; } public BankCard contains(int id, int passwd) { // TODO Auto-generated method stub BankCard card = null; for(int i=0;i<size;i++) { if(id==cards[i].getId()&&passwd==cards[i].getPasswd()) { card = cards[i]; break; } } return card; } } /** * 银行卡类 * @author Ray * */ public class BankCard { private int id; private int passwd; private int money; public BankCard(int id,int passwd) { this.id=id; this.passwd=passwd; } public int getId() { return id; } public int getPasswd() { return passwd; } public int getMoney() { return money; } public void saveMoney(int money) { // TODO Auto-generated method stub this.money+=money; } public boolean withdrawMoney(int money) { boolean flag = false; // TODO Auto-generated method stub if(this.money >= money) { this.money -= money; flag = true; } return flag; } }