目录
复习
封装
/*
* 面向对象三大思想:继承、封装、多态
*
* 封装的含义:把功能封装在一个方法里,对属性的操作都是通过方法来实现的,而不是直接操作对象的属性
*
* 冲突的出现:如果为对象赋值,不加任何约束的话,很有可能会和现实情况冲突
*
*使用封装解决冲突:把属性进行私有化,把对属性约束的逻辑体现在公共的方法里
*
* 封装的步骤:1)把属性私有化,用private 2)提供公共的方法来操作私有的属性 3)公共方法里
* 加入一些逻辑
* */
Animal类
package dmo;
public class Animal {
//private 只有类的内部才能访问该属性
private int leg;
//public,大家都能访问
//提供一个设置腿的数量的方法
public void setLeg(int leg){
//set是有参函数
//要求腿的数量不能是奇数
//把涉及到对属性赋值的动作封装在方法里,使得用户不能直接访问
if(leg%2==1){
System.out.println("没那么多腿,大der");
return;
}//this。属性=属性; 结尾
this.leg=leg;
}
// 获得腿的数量的方法
// get是无参函数
public int getLeg() {
//return this.属性; 结尾
return this.leg;
}
}
测试类
package dmo;
public class Demo1 {
public static void main(String[] args) {
Animal a1= new Animal();
// a1被属性leg被private修饰,于是访问不到
// a1.leg = 4;
a1.setLeg(3);
System.out.println(a1.getLeg());
}
}
结果输出
正确输出
错误输出
继承
/* * 1.继承的目的:创建一个类时,可以通过继承,快速的获得该类中已经定义的内容,避免了重复定义(重复造轮子) * * 2.继承的使用:class子类 extends父类{ * } * */
Person类
package oa;
public class Person {
private String name;
private int age;
private String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public void eat(){
System.out.println();
}
public void sleep(){
System.out.println();
}
}
Teacher类
package oa;
public class Teacher extends Person{
private String schoolName;
private String major;
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public void teach(){
System.out.println("别tmbb了,上课!");
}
}
Student类
package oa;
public class Student extends Person{
private Long stuid;
private String address;
public Long getStuid() {
return stuid;
}
public void setStuid(Long stuid) {
this.stuid = stuid;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void study(){
System.out.println("你在狗叫什么,我上还不行吗!");
}
}
Demo类
package oa;
public class Demo {
public static void main(String[] args) {
Teacher teacher=new Teacher();
//Person给的属性、、方法
teacher.setName("潘堂智");
teacher.eat();
//自己的属性、方法
teacher.setMajor("java");
System.out.println( teacher.getName());
teacher.teach();
}
}
结果输出
老师输出
学生输出
银行卡系统
* 有一个银行卡类,有账号和密码,余额,有存款和取款的方法。 * 创建一个借记卡类,继承于银行卡,转账额度。 * 存款和取款的方法,取款的时候, * 如果超过了20000,则提示需要去柜台拿身份证办理!!! * 创建一个信用卡类,继承于银行卡,信用额度。 * 有存款和取款的方法。 * 取款:如果余额大于0,则从余额中扣, * 如果余额小于等于0,则会走信用额度。 * 余额:100,信用额度:50000,要取钱数:1000 * * 最终在Demo类中测试相关功能
Card类
public class Card {
private String cardId;
private String password;
private Double balance;
public Double in(Double money) {
balance += money;
return balance;
}
public Double out(Double money) {
balance -= money;
return balance;
}
public Card() {
}
public Card(String cardId, String password, Double balance) {
this.cardId = cardId;
this.password = password;
this.balance = balance;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}
}
CreditCard类
public class CreditCard extends Card {
private Double credits;
private Double temp = 0.0;
public CreditCard() {
}
public CreditCard(String cardId, String password, Double balance, Double credits) {
super(cardId, password, balance);
this.credits = credits;
}
public Double in(Double money) {
// 信用卡的存款,首先要还额度,其次此时存款
if(temp == 0.0){
return super.in(money);
}
// 先还额度,剩下的才是余额
if(temp >= money) {
credits += money;
temp -= money;
return credits;
}
if(temp < money) {
credits += temp;
setBalance(money - temp);
temp = 0.0;
return getBalance();
}
return null;
}
public Double out(Double money) {
// 取款先走余额,再走信用额度
if(getBalance() >= money) {
return super.out(money);
}else if(getBalance() < money && credits < money){
return Double.valueOf(-1);
}else if(getBalance() < money && credits > money) {
double d = money - getBalance();
credits = credits - d;
temp = d;
return credits;
}
return null;
}
}
DebitCard类
public class DebitCard extends Card {
// 转账额度
private Double transferAmount;
public DebitCard() {
}
public DebitCard(String cardId, String password, Double balance,Double transferAmount) {
super(cardId, password, balance);
this.transferAmount = transferAmount;
}
public Double out(Double money) {
// 借记卡的取款,除了要考虑余额是否足够
// 转账额度的问题。20000
if(getBalance() >= money && transferAmount >= money){
// 说明余额足够,直接调用父类的方法
return super.out(money);
}else if(getBalance() < money){
return Double.valueOf(-1);
}else if(transferAmount < money) {
return Double.valueOf(-2);
}
return Double.valueOf(-3);
}
}
Demo类
public class Demo {
public static void main(String[] args) {
// DebitCard debitCard = new DebitCard("123456","123456",Double.valueOf(100),Double.valueOf(20000));
// Double in = debitCard.in(1000000.0);
// in = debitCard.out(300000.0);
// if (in >= 0) {
// System.out.println("余额为:" + in);
// }else if(in == -1){
// System.out.println("取款失败,余额不足!");
// }else if(in == -2){
// System.out.println("取款失败,今日取款额度不足!");
// }
CreditCard creditCard = new CreditCard("123456","123465",1000.0,50000.0);
// 余额为0,额度还剩49000,欠了1000
creditCard.out(2000.0);
System.out.println(creditCard.in(3000.0));
}
}
结果
总结
老师把银行卡系统细细的讲了一遍,虽然没能自己动手写出代码来,但是老师的代码已经能看懂了,在晚自习时我将之前讲的:封装、继承好好地复习了一遍,自己感觉已经掌握了简单的封装、继承知识,对于综合性较强的系统题目我打算用一周的时间把他吃下,在赶上进度的同时将后面知识预习,争取做到未雨绸缪。