2022.07.19 第九小组 王茜 学习笔记

今天学习了this关键字以及封装和构造器。

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

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

 4、开发中,this通常用在什么位置!
 通常用在赋值,构造器赋值。

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

面向对象的特征:封装
 1、代码层面
   (1)属性私有化,所有的属性的都要使用private封装
   (2)提供一个共有的set、get方法
    getter方法能够按照客户的期望返回格式化的数据
    setter方法可以限制和检验setter方法传入的参数是否合法

 

 * 有一个person类,有name,age属性
 * 有一个Debit类,有cardID,password,balance属性
 * Person类有一个开户的方法,openAccount,in(余额增加),out(余额减少)
 * Person类有一个开户的方法,openAccount,in(余额增加),out(余额减少,
 *                  取款时要判断余额)
 * Debit类中有一个显示银行卡信息的方法。
 *
 * 赋值的方式:
 * 1.构造器(建议)
 * 2.直接通过.属性的方式赋值
 *
 * 分析:
 *  开户:给Person类的Debit属性赋值,Debit初始化时需要给cardId,password,balance赋值
 *  最终在Demo类中测试相关功能
 *
 *  加功能:键盘输入,存款和取款需要比对密码。
 *  加键盘输入:
 *  哪些地方是需要我们来输入的?
 *  1.密码,余额
 *  2.存多少钱
 *  3.取多少钱
 *
 *  在哪里写键盘输入Scanner写在哪里?
 *  Scanner应该出现在main里
 *

代码:1、public class Demo 

public class Demo
 public class Demo {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Person person=new Person("张三",20);
        System.out.println("请选择功能:1、开户 2、存款  3、取款");
        String flag=sc.next();
        switch (flag){
            case "1":
                System.out.println("请输入账户密码");
                String password=sc.next();
                System.out.println("请输入余额:");
                double balance=sc.nextDouble();
                Debit debit=new Debit("6666666666","123456",100);
                person.openAccount(debit);
                break;
            case "2":
                System.out.println("请输入密码");
                int pass=sc.nextInt();
                boolean b=person.valid(pass);
                if(b){
                    System.out.println("请输入存款金额");
                    double money=sc.nextDouble();
                    person.in(money);
                }
               else {
                    System.out.println("密码有误");
                }
                break;
        }
    }
}

代码:2、 public class Person

public class 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 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 {

        }
    }
    public  void show(){
        System.out.println("姓名:"+name+"年龄"+age);
    }
    public boolean valid(int pass) {
       return true;
    }
}

代码:3、public class Debit

public class Debit
 public class Debit {
    String carID;
    String password;
    double balance;
    public  Debit(){

    }
    public  Debit(String carID,String password ,double balance){
        this.carID=carID;
        this.password=password;
        this.balance=balance;
    }
    public void show(){
        System.out.println("卡号"+carID+"余额"+balance);
    }

}

 

 

今日感受:

        刚开始接受这个知识点的时候感到很懵,感觉有一点会,但大多数都是不会的,当老师留时间做题的时候,我只能写出最开始的那个小点,之后就一点头绪都没有,都是在老师讲过之后才有那种:原来是这样啊。

        虽然老师讲的很好,但是我接受新知识不好,学的不太好!

posted @ 2022-07-19 18:53  无聊的wx  阅读(17)  评论(0编辑  收藏  举报