2022-07-19 第二小组 张鑫 学习笔记

实训十一天 面向对象2

1.学习重点

1.构造器赋值
2.this关键字
3.存款系统实例
4.封装
5.登录注册实例

2.导图


3.学习心得

今天一天再次加强了面向对象的学习,主要的学习内容就是this关键字以及封装,做了两个案例,我还是感觉逻辑差一些意思,总是不知道怎么合理的将类和方法练习套用到主方法中,还是需要多加练习!

4.学习内容

构造器赋值

什么时候用构造器赋值:
看创建对象是为了干什么,
如果创建对象只是为了调用类的方法,建议使用无参构造器
如果创建对象时需要用到对象的某个属性,可以使用构造器


this关键字

this代表的是当前类的对象,this代表当前方法的调用者
this既然是代表方法的调用者,它实际上和对象的作用是一样的
既可以调属性,也可以调方法

一个类中可以有什么?
属性,方法,构造器
this不能用在static 方法中

开发中,this通常用在什么位置
通常用来赋值,尤其是构造器赋值

使用this调用构造器的要求
1.必须在构造器中使用this调用构造器
2.必须是第一句话(第一行代码)

代码

public class Ch02 {

    String str;
    int i;

    public Ch02(){

    }
    public Ch02(String str){
        this.str =str;
    }
    public Ch02(String str,int i){
//        this.str =str;
        //调用构造器
        //不需要写任何名字

        this(str);
        this.i = i;
    }
    public void show(String str){
        /*
            使用对象调用属性时,调用的是成员变量
         */
//        Ch02 c=new Ch02();
        this.str =str;
        this.info();
    }
    public void info(){
        System.out.println(str);

    }

存款系统实例

案例要求
有一个Person类,有name,age属性
有一个Debit类,有cardId,password,balance属性
Person类有一个开户的方法,openAccount,in(余额增加),out(余额减少,取款时要判断余额)
Debit类中有一个显示银行卡信息的方法
赋值的方式:
1.构造器
2.直接通过属性的方式赋值
分析:
开户时:给Person类的Debit属性赋值,Debit初始化时需要给cardId,password,balance赋值
最终在Demo类中测试相关功能
加功能
键盘输入:存款和取款需要比对密码
加键盘输入:
1.开户时:密码,姓名,年龄,余额
2.存取钱:存取多少钱

demo类

public class Demo {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入您的姓名:");
        String name =sc.next();
        System.out.println("请输入您的年龄:");
        int age=sc.nextInt();
        System.out.println("请输入您的密码:");
        int password=sc.nextInt();
        System.out.println("请存入您的初始余额");
        double balance =sc.nextDouble();
        System.out.println("开卡成功!");
        Person person = new Person(name,age);
        Debit debit =new Debit("1001",password,balance);
        person.openAccount(debit);
        System.out.println("请选择业务:1.存款 2.取款");
        String choose =sc.next();
        switch (choose){

        }
        person.in(120);
        person.out(230);
    }

person类

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.show();
        show();
    }
    public void show(){
        System.out.println("姓名:"+name+" 年龄"+age);
    }
    public boolean valid(int pass){
        if(pass == debit.password){
            return true;
        }
        return false;
    }
    public void in(double money){
        //存款:修改余额并重新赋值
        debit.balance +=money;
        System.out.println("存款成功,余额为:"+debit.balance);
    }
    public void out(double money){
        //取款:修改余额并重新赋值
        if(money<= debit.balance){
            debit.balance -=money;
            System.out.println("取款成功,余额为:"+debit.balance);
        }else {
            System.out.println("余额不足!余额为:"+debit.balance);
        }

    }
}

debit类

public class Debit {
    String cardId;
    int password;
    double balance;

    public Debit() {
    }

    public Debit(String cardId, int password, double balance) {
        this.cardId = cardId;
        this.password = password;
        this.balance = balance;
    }

    public void show(){
        System.out.println("卡号:"+cardId+" 余额:"+balance);
    }
}

封装

1.代码层面

(1)属性私有化,所有的属性都要使用private封装            (2)提供一个公有的set,get方法
getter方法能够按照客户的期望返回格式化的数据
setter方法可以限制和检验setter方法传入的参数是否合法
隐藏对象的内部结构
正确定义一个类:
    (1)所有的属性私有化
    (2)每个属性都有对应的get,set方法

2.思想层面


空指针异常

什么情况下会出现空指针异常
引用数据类型的默认值(初始值是null,空引用)
3个异常
1.数组下标越界
2.内存溢出(错误)
3.空指针

posted @ 2022-07-19 21:42  菜鸡成长记  阅读(41)  评论(0)    收藏  举报