day11_面向对象05

本文展示了两个Java编程实例:银行客户与账户管理软件,包括Customer、Account和Bank类的实现,以及银行类的增删查改操作。同时,探讨了super关键字在调用父类属性、方法和构造器中的应用。此外,提供了圆柱体类Cylinder扩展圆形类Circle的示例,展示了继承和面积体积计算。
摘要由CSDN通过智能技术生成

day11复习

1. 练习:Bank-Customer[]-Account

  • Account类
    在这里插入图片描述
package com.atguigu.exer1;

/**
 * @author shkstart
 * @create 10:22
 */
public class Account { //账户类
    private double balance;

    public Account(double init_balance){
        this.balance = init_balance;
    }

    public double getBalance() {
        return balance;
    }

    public void deposit(double amt){
        if(amt > 0){
            balance += amt;
            System.out.println("成功存入:" + amt);
        }
    }
    public void withdraw(double amt){
        if(balance >= amt){
            balance -= amt;
            System.out.println("成功取出:" + amt);
        }else{
            System.out.println("余额不足,取款失败");
        }
    }
}

  • Customer类
    在这里插入图片描述
package com.atguigu.exer1;

/**
 * @author shkstart
 * @create 10:25
 */
public class Customer { //客户类
    private String firstName;
    private String lastName;
    private Account account;

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }
}

  • Bank类
    在这里插入图片描述
package com.atguigu.exer1;

/**
 * @author shkstart
 * @create 10:26
 */
public class Bank {//银行类
    private Customer[] customers = new Customer[10]; //用于保存银行中的多个客户
    private int numberOfCusotomers;//用于记录银行中实际的客户个数

    public Bank(){
//        customers = new Customer[10];
    }
    // addCustomer 方法必须依照参数(姓,名)构造一个新的 Customer对象,
    // 然后把它放到 customer 数组中。还必须把 numberOfCustomers 属性的值加 1。
    public void addCustomer(String f,String l){
        if(numberOfCusotomers < customers.length){
            Customer cust = new Customer(f,l);

//        customers[numberOfCusotomers] = cust;
//        numberOfCusotomers++;
            //合并
            customers[numberOfCusotomers++] = cust;
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }
    //	getNumOfCustomers 方法返回 numberofCustomers 属性值
    public int getNumOfCustomers(){

        return numberOfCusotomers;
    }

    //getCustomer方法返回与给出的index参数相关的客户
    public Customer getCustomer(int index){
        if(index < 0 || index >= numberOfCusotomers){
            return null;
        }

        return customers[index];

    }

}

  • 代码测试
package com.atguigu.exer1;

/**
 * @author shkstart
 * @create 10:33
 */
public class BankTest {
    public static void main(String[] args) {
        Bank bank = new Bank();

        bank.addCustomer("康","孙");
        bank.addCustomer("智","吕");

        System.out.println("银行客户个数为:" + bank.getNumOfCustomers());

        Customer customer = bank.getCustomer(1);
        if(customer == null){
            System.out.println("未找到指定的客户");
        }else{
            customer.setAccount(new Account(2000));
            System.out.println("给客户添加账户成功");
        }

        bank.getCustomer(1).getAccount().withdraw(500);
    }
}
  • 内存简图
    在这里插入图片描述

2. 项目二:客户信息管理软件

  • Customer类
package com.atguigu.p2;

/**
 * Customer为实体类,用来封装客户信息
 *
 * @author shkstart
 * @create 11:28
 */
public class Customer {
    private String name;//客户姓名
    private char gender;  //性别
    private int age; //年龄
    private String phone;//电话号码
    private String email; //电子邮箱

    public Customer() {

    }

    public Customer(String name, char gender, int age, String phone, String email) {
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.phone = phone;
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDetails(){
        return name + "\t" + gender + "\t\t" + age + "\t\t\t" + phone + "\t\t" + email;
    }
}

  • 实现CustomerList(这是项目二的重心)
package com.atguigu.p2;

/**
 * CustomerList为Customer对象的管理模块,内部使用数组管理一组Customer对象
 *
 * @author shkstart
 * @create 11:31
 */
public class CustomerList {
    private Customer[] customers; //用来保存客户对象的数组
    private int total = 0; //记录已保存客户对象的数量

    /**
     * 用途:构造器,用来初始化customers数组
     * @param totalCustomer  指定customers数组的最大空间
     */
    public CustomerList(int totalCustomer){
        customers = new Customer[totalCustomer];
    }

    /**
     * 用途:将参数customer添加组中最后一个客户对象记录之后
     * @param customer  指定要添加的客户对象
     * @return  添加成功返回true;false表示数组已满,无法添加
     */
    public boolean addCustomer(Customer customer){
        if(total < customers.length){
            customers[total++] = customer;
            return true;
        }

        return false;

    }

    /**
     * 用途:用参数customer替换数组中由index指定的对象
     * @param index  指定所替换对象在数组中的位置,从0开始
     * @param cust  指定替换的新客户对象
     * @return  替换成功返回true;false表示索引无效,无法替换
     */
    public boolean replaceCustomer(int index, Customer cust){
        if(index < 0 || index >= total){
            return false;
        }

        customers[index] = cust;
        return true;
    }

    /**
     * 用途:从数组中删除参数index指定索引位置的客户对象记录
     * @param index  指定所删除对象在数组中的索引位置,从0开始
     * @return  删除成功返回true;false表示索引无效,无法删除
     */
    public boolean deleteCustomer(int index){
        if(index < 0 || index >= total){
            return false;
        }

        //在index位置上找到了元素
        for(int i = index;i < total - 1;i++){
            customers[i] = customers[i + 1];
        }

//        customers[total - 1] = null;
//        total--;
        //合并
        customers[--total] = null;
        return true;
    }

    /**
     * 用途:返回数组中记录的所有客户对象
     * @return 数组中包含了当前所有客户对象,该数组长度与对象个数相同
     */
    public Customer[] getAllCustomers(){
        Customer[] custs = new Customer[total];
        for(int i = 0;i < custs.length;i++){
            custs[i] = customers[i];
        }
        return custs;
    }

    /**
     *用途:返回参数index指定索引位置的客户对象记录
     * @param index 指定所要获取的客户在数组中的索引位置,从0开始
     * @return 封装了客户信息的Customer对象
     */
    public Customer getCustomer(int index){
        if(index < 0 || index >= total){
            return null;
        }
        return customers[index];
    }

    /**
     * 获取客户对象的个数
     * @return
     */
    public int getTotal(){
        return total;
    }

}

  • CustomerView

    • 整体框架
    package com.atguigu.p2;
    
    /**
     * CustomerView为主模块,负责菜单的显示和处理用户操作
     * @author shkstart
     * @create 11:54
     */
    public class CustomerView {
        private CustomerList customerList = new CustomerList(10);
    
        //进入主界面
        public void enterMainMenu(){
            boolean isFlag = true;
            while(isFlag){
    
                System.out.println("-----------------客户信息管理软件-----------------\n");
                System.out.println("                   1 添 加 客 户");
                System.out.println("                   2 修 改 客 户");
                System.out.println("                   3 删 除 客 户");
                System.out.println("                   4 客 户 列 表");
                System.out.println("                   5 退       出\n");
                System.out.print("                   请选择(1-5):");
    
                char menu = CMUtility.readMenuSelection();
                switch(menu){
                    case '1':
                        addNewCustomer();
                        break;
                    case '2':
                        modifyCustomer();
                        break;
                    case '3':
                        deleteCustomer();
                        break;
                    case '4':
                        listAllCustomers();
                        break;
                    case '5':
                        System.out.println("退出");
                        break;
                }
            }
    
        }
        //添加客户
        private void addNewCustomer(){
            System.out.println("添加客户");
        }
        //修改客户
        private void modifyCustomer(){
            System.out.println("修改客户");
        }
        //删除客户
        private void deleteCustomer(){
            System.out.println("删除客户");
        }
        //显示客户列表
        private void listAllCustomers(){
            System.out.println("显示客户列表");
        }
        //主方法
        public static void main(String[] args){
            CustomerView view = new CustomerView();
            view.enterMainMenu();
        }
    
    }
    
    
    • 接下来实现针对于数组的添加、删除、修改、查询操作
    package com.atguigu.p2;
    
    /**
     * CustomerView为主模块,负责菜单的显示和处理用户操作
     * @author shkstart
     * @create 11:54
     */
    public class CustomerView {
        private CustomerList customerList = new CustomerList(10);
    
        public CustomerView(){
            customerList.addCustomer(new Customer("张三",'男',23,"13012341234","zhangs@gmail.com"));
            customerList.addCustomer(new Customer("李四",'男',43,"13012341234","zhangs@gmail.com"));
        }
    
        //进入主界面
        public void enterMainMenu(){
            boolean isFlag = true;
            while(isFlag){
    
                System.out.println("-----------------客户信息管理软件-----------------\n");
                System.out.println("                   1 添 加 客 户");
                System.out.println("                   2 修 改 客 户");
                System.out.println("                   3 删 除 客 户");
                System.out.println("                   4 客 户 列 表");
                System.out.println("                   5 退       出\n");
                System.out.print("                   请选择(1-5):");
    
                char menu = CMUtility.readMenuSelection();
                switch(menu){
                    case '1':
                        addNewCustomer();
                        break;
                    case '2':
                        modifyCustomer();
                        break;
                    case '3':
                        deleteCustomer();
                        break;
                    case '4':
                        listAllCustomers();
                        break;
                    case '5':
    //                    System.out.println("退出");
                        System.out.print("确认是否退出(Y/N):");
                        char isExit = CMUtility.readConfirmSelection();
                        if(isExit == 'Y'){
                            isFlag = false;
                        }
                        break;
                }
            }
    
        }
        //添加客户
        private void addNewCustomer(){
    //        System.out.println("添加客户");
            System.out.println("---------------------添加客户---------------------");
            System.out.print("姓名:");
            String name = CMUtility.readString(8);
            System.out.print("性别:");
            char gender = CMUtility.readChar();
            System.out.print("年龄:");
            int age = CMUtility.readInt();
            System.out.print("电话:");
            String phone = CMUtility.readString(13);
            System.out.print("邮箱:");
            String email = CMUtility.readString(25);
    
            Customer customer = new Customer(name, gender, age, phone, email);
            boolean addSuccessed = customerList.addCustomer(customer);
            if(addSuccessed){
                System.out.println("---------------------添加完成---------------------");
            }else{
                System.out.println("很遗憾,客户列表已满,添加失败!");
            }
    
    
    
        }
        //修改客户
        private void modifyCustomer(){
    //        System.out.println("修改客户");
            System.out.println("---------------------修改客户---------------------");
    
            Customer customer;
            int number;
            for(;;){
                System.out.print("请选择待修改客户编号(-1退出):");
                number = CMUtility.readInt();
    
                if(number == -1){
                    return; //结束方法
                }
    
                customer = customerList.getCustomer(number-1);
                if(customer == null){
                    System.out.println("无法找到指定客户!");
                }else{
                    break;
                }
    
            }
    
            //代码能执行到此,其实就意味着找到了指定编号的客户。下面进行修改即可
            System.out.print("姓名(" + customer.getName() + "):");
            String name = CMUtility.readString(8, customer.getName());
            System.out.print("性别(" + customer.getGender() + "):");
            char gender = CMUtility.readChar(customer.getGender());
            System.out.print("年龄("+ customer.getAge() +"):");
            int age = CMUtility.readInt(customer.getAge());
            System.out.print("电话("+ customer.getPhone() +"):");
            String phone = CMUtility.readString(13, customer.getPhone());
            System.out.print("邮箱("+ customer.getEmail() +"):");
            String email = CMUtility.readString(25, customer.getEmail());
    
            Customer cust = new Customer(name,gender,age,phone,email);
            boolean replaceSuccessed = customerList.replaceCustomer(number-1, cust);
            if(replaceSuccessed){
                System.out.println("---------------------修改完成---------------------");
            }else{
                System.out.println("---------------------修改失败---------------------");
            }
        }
        //删除客户
        private void deleteCustomer(){
    //        System.out.println("删除客户");
            System.out.println("---------------------删除客户---------------------");
            Customer customer;
            int number;
            for(;;){
                System.out.print("请选择待删除客户编号(-1退出):");
                number = CMUtility.readInt();
    
                if(number == -1){
                    return; //结束方法
                }
    
                customer = customerList.getCustomer(number-1);
                if(customer == null){
                    System.out.println("无法找到指定客户!");
                }else{
                    break;
                }
    
            }
            //代码能执行到此,其实就意味着找到了指定编号的客户。下面进行删除即可
            System.out.print("确认是否删除(Y/N):");
            char isDelete = CMUtility.readConfirmSelection();
            if(isDelete == 'Y'){
                boolean deleteSuccessed = customerList.deleteCustomer(number - 1);
                if(deleteSuccessed){
                    System.out.println("---------------------删除完成---------------------");
                }else{
                    System.out.println("---------------------删除失败---------------------");
                }
            }
    
        }
        //显示客户列表
        private void listAllCustomers(){
            System.out.println("---------------------------客户列表---------------------------");
            System.out.println("编号\t姓名\t性别\t年龄\t\t电话\t\t\t邮箱");
    
            //思路1:
    //        Customer[] customers = customerList.getAllCustomers();
    //        if(customers.length == 0){
    //            System.out.println("客户列表中没有任何客户信息!");
    //        }else{
    //
    //        }
            //思路2
            int total = customerList.getTotal();
            if(total == 0){
                System.out.println("客户列表中没有任何客户信息!");
            }else{
                Customer[] customers = customerList.getAllCustomers();
                for(int i = 0;i <customers.length;i++){
                    Customer cust = customers[i];
                    System.out.println(i + 1 + "\t\t" + cust.getDetails());
                }
            }
    
            System.out.println("-------------------------客户列表完成-------------------------");
    
        }
        //主方法
        public static void main(String[] args){
            CustomerView view = new CustomerView();
            view.enterMainMenu();
        }
    
    }
    
    

3. super关键字的使用

* super关键字的使用
*
可以用来调用:属性、方法、构造器

3.1 super调用属性、方法

2. super调用属性、方法
*
*  继承以后,我们在子类的方法中,可以通过"super.属性"或"super.方法"的方式,调用父类中的指定的属性或方法。
*
*  2.1 一般情况下,"super.属性"调用父类属性时,我们都选择省略"super."。但是当子类与父类中声明了同名的属性时,为了区分两个属性,我们可以使用"super.属性"表示调用的是父类的属性,而使用"this.属性"表示调用子类的属性。
*       > 注意:开发中,我们尽量避开子父类中定义同名的属性
*  2.2 当子类重写了父类的方法以后,我们在子类方法中,如果希望调用父类中被重写的方法,则可以使用"super.方法"的方式。
*
package com.atguigu.java;

/**
 * @author shkstart
 * @create 16:21
 */
public class Person {
    String name;
    int age;
    int id = 1001;

    public Person(String name) {
        this.name = name;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person() {
    }

    public void eat(){
        System.out.println("人吃饭");
    }

    public void show(){
        System.out.println("name : " + name + ", age : " + age);
    }

    public void sleep(){
        System.out.println("人睡觉");
    }
}


/**
 * @author shkstart
 * @create 16:21
 */
public class Student extends Person {

    String major;
    int id = 1002;
//    int id = 1003;

    public Student(){

    }

    public Student(String major){
        this.major = major;
    }

    public void study(){
        System.out.println("学生学习");
    }

    @Override
    public void eat() {
        System.out.println("学生应该多吃有营养的食物");
    }

    public void info(){
        System.out.println("name : " + this.name);
        System.out.println("age : " + this.age);
        System.out.println("major : " + this.major);
        System.out.println("id : " + this.id);//1002
        System.out.println("id : " + super.id); //1001
    }

    public void display(){
        this.sleep();

        this.eat();//调用子类重写的方法
        super.eat();//调用父类中被重写的方法
    }
}
  • 课后练习
package com.atguigu.exer2;

/**
 * @author shkstart
 * @create 15:32
 */
public class Circle {
    private double radius ;//半径

    public Circle(){
        System.out.println("Circle()....");
        radius = 1;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }
    //计算圆的面积
    public double findArea(){
        return Math.PI * radius * radius;
    }
}

package com.atguigu.exer2;

/**
 * @author shkstart
 * @create 15:34
 */
public class Cylinder extends Circle{//圆柱类
    private double length;//高度

    public Cylinder(){
//        super();
        length = 1;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double findVolume(){//返回圆柱的体积
//        return Math.PI * getRadius() * getRadius() * length;
        return super.findArea() * length;
    }

    //求圆柱的表面积
    public double findArea(){
//        return Math.PI * getRadius() * getRadius() * 2 + 2 * Math.PI * getRadius() * length;
        return super.findArea() * 2 + 2 * Math.PI * getRadius() * length;
    }

}

  • 课后练习
package com.atguigu.exer3;

class Account{ //账户
    private double balance = 100;//余额
    public void withdraw(double amt){ //取钱  父类被重写的方法
        if(amt <= balance){
            balance -= amt;
            System.out.println("成功取出:" + amt);
        }else{
            System.out.println("取款失败");
        }
    }

    public double getBalance() {
        return balance;
    }


}

class CheckingAccount extends Account {//信用卡账户
    private double protectedInit = 5000;//透支额度

    public void withdraw(double amt){ //子类重写的方法
        if(amt <= getBalance()){
            super.withdraw(amt);
            System.out.println("成功取出:" + amt);
        }else if(amt <= getBalance() + protectedInit){
            protectedInit -= (amt - getBalance());
            super.withdraw(getBalance());
            System.out.println("使用了信用资金。成功取出:" + amt);
        }else{
            System.out.println("取款失败");
        }
    }

    public double getProtectedInit() {
        return protectedInit;
    }

}

3.2 super调用构造器

super调用构造器
*    > 在子类的构造器中,可以使用"super(参数列表)"的方式显式的调用直接父类中指定的构造器。
*    > "super(参数列表)"结构,必须声明在构造器的首行
*    > 在任何一个构造器中,"super(参数列表)"结构 和 "this(参数列表)"结构只能二选一
*    > 如果在子类的构造器中,没有显式的使用"super(参数列表)"结构和 "this(参数列表)"结构的话,
*      则默认调用"super()"
*    > 如果一个类有n个构造器,则最多有 n - 1个构造器中使用了"this(参数列表)"结构,
*      至少有一个使用了"super(参数列表)"

> 结论1:一个构造器的首行,不是使用了"this(参数列表)",就是使用了"super(参数列表)" (除java.lang.Object外)
> 结论2:通过构造器创建类的对象,我们说一定会直接或间接的调用到当前类的直接父类的构造器,乃至于父类的父类的构造器,。。。。直到调用到java.lang.Object类的构造器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值