2022-7-19 第三小组 甘源册 学习笔记
知识点掌握情况
this关键字(理解) 封装(理解)
学习心情(心路历程)
封装是今天所讲的重点,理解的还算可以,基本明白了封装的概念,但是在思想层面上,不太理解将属性私有化的意义,将属性私有化后可以通过getter和setter取赋值和获取,那不私有化,通过对象名.属性也能获取和赋值,那私有化让我感觉到多此一举,这不是很理解。希望老师能指点一下。
1.this关键字
- this代表的是当前类的属性,this代表的是方法的调用者
- this即然是代表是方法的调用者,它实际上是和对象的作用是一样的
- 既可以调属性也可以调方法
- this调用构造器
- 必须在构造器使里this调用构造器
- 必须是第一个(第一行代码)
- 不能再static方法里调用this
- 一个类里面可以有:属性,方法,构造器
2.封装
- 面向对象的特征之一
2.1代码层面
- 所有的属性私有化(都用private修饰)
- 提供一个公有的set,get方法
- getter方法能够按照客户的期望返回格式化的数据
- setter方法可以限制和检验setter方法传入的参数是否合法
- 隐藏对象的内部构造
- 正确定义一个类
- 所有的属性私有化
- 每个属性都有对应的set和get方法
- 这是规矩,必须遵守
2.2思想层面
- 把一堆重复执行(相似功能)的代码封装成方法
- 把一堆可能重复执行的方法封装成类
- 所有的属性私有化,由getter和setter方法
以后我们使用赋值的方式:
- 不在使用对象.属性的方式
- 使用构造器
- 使用setter方法
习惯
- 必须保证每个类都有一个无参构造器
3.遇到的异常
- 数组下标越界
- 内存溢出(错误)
- 空指针
练习1
-
银行开户存款取款
-
Person类
-
package com.gyc.morning1; import java.util.Date; public class Person { String name; int age; Debit debit; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } public void openAccount(Debit debit){ this.debit=debit; debit.xx(); xx(); } public void xx(){ System.out.println(" 姓名: "+name+" 年龄: "+age); } public void in( int s){ debit.balanec+=s; System.out.println("存入"+s+"元"); debit.xx(); xx(); } public boolean vaild(int pass){ if (pass==debit.password){ return true; } return false; } public void out( int s){ if (debit.balanec>=s) { debit.balanec -= s; System.out.println("取出"+s+"元"); debit.xx(); xx(); }else { System.out.println("余额不足"); } } }
-
-
Debit类
-
package com.gyc.morning1; public class Debit { int cardId; int password; int balanec; public Debit() { } public Debit(int cardId, int password, int balanec) { this.cardId = cardId; this.password = password; this.balanec = balanec; } public void xx( ) { System.out.print(" 银行卡的卡号是:"+this.cardId+" 余额是:"+this.balanec); } }
-
-
Demo测试类
-
package com.gyc.morning1; import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Debit debit = new Debit(); Person person = new Person("伟大", 21); int pass; a: for (; ; ) { System.out.println("请选择功能 1:开户 2:取款 3:存款 "); int anInt3 = scanner.nextInt(); switch (anInt3) { case 1: System.out.println("输入密码"); int anInt1 = scanner.nextInt(); debit = new Debit(622848, anInt1, 200); person.openAccount(debit); continue a; case 2: b: for (; ; ) { if (debit.cardId == 0) { System.out.println("请先去创建用户"); continue a; } System.out.println("输入密码"); pass = scanner.nextInt(); boolean vaild = person.vaild(pass); if (vaild) { System.out.println("输入取款金额:"); int inMoney = scanner.nextInt(); person.out(inMoney); continue a; } System.out.println("输入错误"); continue b; } case 3: c: for (; ; ) { if (debit.cardId == 0) { System.out.println("请先去开户"); continue a; } System.out.println("输入密码"); pass = scanner.nextInt(); boolean vaild = person.vaild(pass); if (vaild) { System.out.println("输入存款金额:"); int outMoney = scanner.nextInt(); person.in(outMoney); continue a; } continue c; } } } } }
-
练习2
-
需求:完成注册和登录
-
代码
-
User类
-
package com.gyc.afternoon.test; public class User { private int username; private int password; public int getUsername() { return username; } public void setUsername(int username) { this.username = username; } public int getPassword() { return password; } public void setPassword(int password) { this.password = password; } }
-
-
测试类
-
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Dem0 dem0 = new Dem0(); User[] users = new User[1]; User[] users1 = new User[1]; User user; int index = 0; a: for (; ; ) { System.out.println("请选择功能 1:注册 2:登录 3:查询"); String flag = scanner.next(); switch (flag) { case "1": users = dem0.kuo(users, users1); System.out.println("请输入账户:"); int username = scanner.nextInt(); boolean b = false; c: for (int i = 0; i < users.length; i++) { if (users[i] == null) { continue c; } if (username == users[i].getUsername()) { b = true; break; } } if (b == false) { System.out.println("请输入密码:"); int password = scanner.nextInt(); user = new User(); user.setPassword(password); user.setUsername(username); users[index] = user; System.out.println("账户:" + users[index].getUsername() + "密码:" + users[index].getPassword()); index++; continue a; } else { System.out.println("用户名重复"); continue a; } case "2": b: for (; ; ) { System.out.println("请输入账户:"); int username1 = scanner.nextInt(); System.out.println("请输入密码:"); int password1 = scanner.nextInt(); for (int i = 0; i < users.length; i++) { if (username1 == users[i].getUsername() && password1 == users[i].getPassword()) { System.out.println("登陆成功"); continue a; } else { continue; } } System.out.println("账户或密码错误,请重新输入"); continue b; } case "3": dem0.bl(users); break a; } } } public User[] kuo(User[] users, User[] users1) { while (users[users.length - 1] != null) { users1 = new User[users.length + 1]; for (int i = 0; i < users.length; i++) { users1[i] = users[i]; } users = users1; } return users; } public void bl(User[] arr) { for (User user : arr) { System.out.println("账户:" + user.getUsername() + "密码:" + user.getPassword()); } }
-

浙公网安备 33010602011771号