JavaSEday09学习
发布日期:2021-05-18 05:20:12 浏览次数:22 分类:精选文章

本文共 7759 字,大约阅读时间需要 25 分钟。

Java编程学习笔记:面向对象基础与常见问题解决

作业回顾

本次学习主要完成了以下几个作业:

  • Exer1类的设计与实现
    • 目的:实践私有变量的访问控制与方法的重写。
    • 实现
      public class TriAngle {    private double base;    private double height;    public TriAngle(double base, double height) {        this.base = base;        this.height = height;    }    public double getBase() { return base; }    public void setBase(double base) { this.base = base; }    public double getHeight() { return height; }    public void setHeight(double height) { this.height = height; }    public void area() {        System.out.println("三角形的面积为:" + base * height / 2);    }}
    • 用法示例
      public class TriAngleTest {    public static void main(String[] args) {        TriAngle triangle1 = new TriAngle(3, 4);        triangle1.area(); // 打印三角形面积        triangle1.setBase(4.5);        triangle1.setHeight(5.4);        triangle1.area();    }}
    1. Exer2类的设计与实现
      • 目的:模拟矩形数组的创建、排序以及存储与显示。
      • 实现
        public class RectangleUtil {    public Rectangle[] getRectangleArray(int length, int low, int high) {        Rectangle[] rectangles = new Rectangle[length];        for (int i = 0; i < length; i++) {            // 随机生成矩形的长宽            double random1 = Math.random() * (high - low) + low;            double random2 = Math.random() * (high - low) + low;            Rectangle rectangle = new Rectangle();            rectangle.setLength(Math.max(random1, random2));            rectangle.setWidth(Math.min(random1, random2));            rectangles[i] = rectangle;        }        return rectangles;    }    public void print(Rectangle[] rectangles) {        for (int i = 0; i < rectangles.length; i++) {            System.out.println(rectangles[i].showLengthAndWidth() + ", 面积为:" + rectangles[i].area());        }    }    public void sortRectangleArray(Rectangle[] rectangles) {        for (int i = 0; i < rectangles.length - 1; i++) {            for (int j = 0; j < rectangles.length - 1 - i; j++) {                if (rectangles[j].area() > rectangles[j + 1].area()) {                    Rectangle temp = rectangles[j];                    rectangles[j] = rectangles[j + 1];                    rectangles[j + 1] = temp;                }            }        }    }}// 矩形类定义class Rectangle {    private double length;    private double width;    public double getLength() { return length; }    public void setLength(double length) { this.length = length; }    public double getWidth() { return width; }    public void setWidth(double width) { this.width = width; }    public double area() { return length * width; }    public String showLengthAndWidth() { return "长:" + length + ", 宽:" + width; }}
      1. Exer3类的设计与实现
        • 目的:模拟一个简单的银行客户管理系统。
        • 实现
          class Customer {    private String firstName;    private String lastName;    private Account account;    public Customer(String f, String l) {        this.firstName = f;        this.lastName = l;    }    public String getFirstName() { return firstName; }    public String getLastName() { return lastName; }    public void setAccount(Account account) { this.account = account; }    public Account getAccount() { return account; }}class Bank {    private Customer[] customers;    private int numberOfCustomer;    public Bank() {        customers = new Customer[10];    }    public void addCustomer(String f, String l) {        Customer customer = new Customer(f, l);        customers[numberOfCustomer++] = customer;    }    public int getNumOfCustomers() {        return numberOfCustomer;    }    public Customer getCustomer(int index) {        if (index < 0 || index >= numberOfCustomer) {            return null;        }        return customers[index];    }}class Account {    private double balance;    public Account(double initBalance) {        this.balance = initBalance;    }    public double getBalance() { return balance; }    public void deposit(double amt) {        if (amt > 0) {            balance += amt;            System.out.println("存入金额:" + amt + ", 账户余额:" + balance);        }    }    public void withdraw(double amt) {        if (amt <= balance) {            balance -= amt;            System.out.println("取出金额:" + amt + ", 账户余额:" + balance);        } else {            System.out.println("账户余额不足,取款失败!!");        }    }}// 测试类class BankTest {    public static void main(String[] args) {        Bank bank = new Bank();        bank.addCustomer("国华", "桑");        bank.addCustomer("锐达", "曲");        bank.addCustomer("忠超", "汤");        System.out.println(getCurrentCustomersCount()); // getNumOfCustomers()的调用方式        // 示例:获取并修改客户信息        Customer customer = bank.getCustomer(0);        if (customer != null) {            Account account = customer.getAccount();            account.deposit(1000);            System.out.println("银行客户信息:" + customer.getFirstName() + " " + customer.getLastName());        }    }    private static int getCurrentCustomersCount() {        return new Bank().numberOfCustomer; // 通过公共方法获取客户数    }}

        知识点回顾

      2. package 和 import 关键字的使用
        • package 用于定义代码的组织文件夹结构,遵循包命名规范。
        • import 用于导入外部类、接口或静态成员。
        • 注意事项
          • 避免无用的导入。
          • 使用static import时注意不会导致命名冲突。
        1. 面向对象的特征之二:继承性
          • 继承性的好处:减少代码冗余,提高代码复用性,促进功能扩展。
          • 继承性理解:子类可以继承父类的属性和方法,对现有功能进行扩展或修改。
          1. 方法的重写
            • 方法重写的前提:在类继承之后对父类方法进行覆盖。
            • 重写规则
              • 方法名和参数列表与父类完全一致。
              • 权限修饰符不小于父类。
              • 返回值类型与父类一致。
              • 抛出的异常类型不大于父类。
            1. super关键字的使用
              • super属性、方法调用:显式调用父类中的属性或方法。
              • super构造器调用:用于在子类构造器中调用父类构造器。
              1. 知识点实践:圆与圆柱的面积与体积计算
                • 圆类设计

                  class Circle {    private double radius;    public Circle(double radius) {        this.radius = radius;    }    public void setRadius(double radius) {        this.radius = radius;    }    public double getRadius() {        return radius;    }    public double findArea() {        return Math.PI * radius * radius;    }}
                • 圆柱类设计

                  class Cylinder extends Circle {    private double length;    public Cylinder(double length) {        super(length);    }    public double getLength() {        return length;    }    public void setLength(double length) {        this.length = length;    }    public double findVolume() {        return super.findArea() * length;    }    @Override    public double findArea() {        return Math.PI * super.getRadius() * super.getRadius() * 2 + Math.PI * super.getRadius() * length;    }}
                • 测试类

                  class CylinderTest {    public static void main(String[] args) {        Cylinder cylinder = new Cylinder(10);        cylinder.setRadius(5);        System.out.println("圆柱底面半径:" + cylinder.getRadius());        System.out.println("圆柱的体积为:" + cylinder.findVolume());        System.out.println("圆柱的表面积为:" + cylinder.findArea());    }}

                练习题解决

                • 问题:设计一个圆类及其子类圆柱,实现面积与体积的计算。
                • 解决方案
                  // 圆类class Circle {    private double radius;    public Circle(double radius) {        this.radius = radius;    }    public void setRadius(double radius) {        this.radius = radius;    }    public double getRadius() {        return radius;    }    public double calculateArea() {        return Math.PI * radius * radius;    }}// 圆柱类class Cylinder extends Circle {    private double height;    public Cylinder(double radius, double height) {        super(radius);        this.height = height;    }    public double getHeight() {        return height;    }    public void setHeight(double height) {        this.height = height;    }    public double calculateVolume() {        return super.calculateArea() * height;    }    @Override    public double calculateArea() {        return super.calculateArea() * 2 + Math.PI * super.getRadius() * height;    }}class CylinderTest {    public static void main(String[] args) {        Cylinder cylinder = new Cylinder(5, 10);        System.out.println("圆柱底面半径:" + cylinder.getRadius());        System.out.println("圆柱高度:" + cylinder.getHeight());        System.out.println("圆柱的体积为:" + cylinder.calculateVolume());        System.out.println("圆柱的表面积为:" + cylinder.calculateArea());    }}

                总结

                通过本次学习,我掌握了Java中基本的面向对象编程知识,包括继承、重写以及使用包与导入的技巧。通过实际代码的编写与修改,进一步理解了不同概念之间的联系与应用。这为后续的面向对象编程学习奠定了坚实的基础。

    上一篇:JavaSE学习day10
    下一篇:JavaSE学习day08

    发表评论

    最新留言

    表示我来过!
    [***.240.166.169]2025年04月22日 13时30分43秒