引言
在Java学习的第四天,我们将探索Java中的数组和面向对象编程(OOP)的基础知识。数组是存储多个相同类型数据的集合,而面向对象编程则是Java编程的核心。通过今天的学习,你将能够理解如何在Java中操作数组,并初步掌握面向对象编程的概念。
数组概述
数组是Java中最基本的数据结构之一,用于存储固定大小的同一类型元素的集合。
静态初始化
静态初始化是在数组创建时,直接在括号中指定元素值的方法。
1 | int[] numbers = {10, 20, 30, 40, 50}; |
一维数组
动态初始化
动态初始化是根据数组的长度来创建数组。
1 | int[] numbers = new int[5]; // 创建一个长度为5的整型数组 |
求最值案例
下面是一个使用一维数组求最大值和最小值的案例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class ArrayMaxMin { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; int max = numbers[0]; int min = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (numbers[i] > max) { max = numbers[i]; } if (numbers[i] < min) { min = numbers[i]; } } System.out.println("Max: " + max); System.out.println("Min: " + min); } } |
综合案例实战
我们可以创建一个简单的程序,让用户输入一系列数字,并计算它们的平均值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.Scanner; public class ArrayAverage { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the array: "); int size = scanner.nextInt(); int[] numbers = new int[size]; System.out.println("Enter " + size + " numbers:"); for (int i = 0; i < size; i++) { numbers[i] = scanner.nextInt(); } double sum = 0; for (int i = 0; i < size; i++) { sum += numbers[i]; } double average = sum / size; System.out.println("Average: " + average); scanner.close(); } } |
二维数组
概述、定义
二维数组可以看作是数组的数组,用于存储表格状的数据。
1 | int[][] matrix = new int[3][4]; // 创建一个3行4列的二维数组 |
综合案例
下面是一个使用二维数组打印乘法表的案例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class MultiplicationTable { public static void main(String[] args) { int[][] multiplicationTable = new int[10][10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { multiplicationTable[i][j] = (i + 1) * (j + 1); } } for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { System.out.print(multiplicationTable[i][j] + "\t"); } System.out.println(); } } } |
面向对象编程入门
面向对象编程是一种编程范式,它将现实世界中的实体抽象为对象,并通过类和对象来模拟现实世界中的行为和属性。
学生案例
下面是一个简单的学生类案例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public void introduce() { System.out.println("Name: " + name + ", Age: " + age); } public static void main(String[] args) { Student student = new Student("Kimi", 20); student.introduce(); } } |
构造器
构造器是一种特殊的方法,用于在创建对象时初始化对象。
1 2 3 4 5 6 7 8 9 10 |
this关键字
1 2 3 4 5 6 7 8 9 10 11 |
封装
封装是将数据(属性)和行为(方法)捆绑到一个单元(类)中,并隐藏内部细节。
1 2 3 4 5 6 7 8 9 10 11 |
实体类
实体类通常用于描述现实世界中的实体,包含属性和方法。
1 2 3 4 5 6 7 |
static静态变量和方法
应用场景
静态变量和方法属于类,而不是类的实例。
1 2 3 4 5 6 7 | public class MathUtils { public static int counter = 0; // 静态变量 public static void incrementCounter() { // 静态方法 counter++; } } |
工具类
静态方法常用于工具类,这些类包含一组相关的静态方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class MathUtils { public static int add(int a, int b) { return a + b; } public static double divide(double a, double b) { if (b != 0) { return a / b; } else { throw new IllegalArgumentException("Divider cannot be zero."); } } } |
静态方法的注意事项
静态方法不能访问类的实例变量或实例方法,它们只能访问静态变量和其他静态方法。
综合实战:电影信息展示
下面是一个使用面向对象编程展示电影信息的综合案例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class Movie { private String title; private int year; private String director; public Movie(String title, int year, String director) { this.title = title; this.year = year; this.director = director; } public void displayInfo() { System.out.println("Title: " + title + ", Year: " + year + ", Director: " + director); } public static void main(String[] args) { Movie movie1 = new Movie("Inception", 2010, "Christopher Nolan"); Movie movie2 = new Movie("The Matrix", 1999, "Lana Wachowski, Lilly Wachowski"); movie1.displayInfo(); movie2.displayInfo(); } } |