JavaSE初学(6)

📅 2026/8/9 14:04:24
JavaSE初学(6)
第六章 面向对象三十、接口interface关键字implements接口是一种纯粹的抽象类其中所有方法都是抽象方法。它定义了一个规范标准本身不提供任何实现具体功能由实现该接口的子类来完成。接口的核心价值在于定义标准实现可插拔性。只要大家都遵循同一个接口标准不同的实现类就可以互相替换。1. 面向接口编程接口有广义和狭义之分狭义接口指 Java 中的interface技术。广义接口指声明为父类类型指向不同的子类对象。这里的父类可以是接口、抽象类或普通父类。接口也是父类的一种形式。// 接口就是一个纯粹的抽象类也是一个标准 public interface IFly { // 接口中的属性默认是 public static final double PI 3.14; // 接口中的方法默认是 public abstract void fly(); }2. 如何选择接口还是抽象类继承抽象类表示严格的 is-a 父子关系例如鸽子就是鸟。抽象类抽取了所有子类的共同特征其中的属性和方法是所有子类共有的。接口表示一个标准通常用于描述 能不能 具有某个功能例如能不能飞。将能力定义为接口实现了该接口的类就具备了相应功能。public void test1() { DaYan daYan new DaYan(); daYan.egg(); daYan.fly(); daYan.print(); // 从 AbstractBird 类的角度看 DaYan 对象只能看到 egg() 方法 AbstractBird bird new DaYan(); bird.egg(); // bird.fly(); // 编译报错 // bird.show(); // 编译报错 // 从 IFly 接口的角度看 DaYan 对象只能看到 fly() 方法 // 接口也是父类的一种形式 IFly fly new DaYan(); fly.fly(); // fly.egg(); // 编译报错 // fly.show(); // 编译报错 fly new GeZi(); fly.fly(); } public void fly(IFly fly) { fly.fly(); }3. 接口的特点接口是纯粹的抽象类其中的方法都是抽象方法默认public abstract。接口中的所有属性都是静态常量默认public static final例如public static final double PI 3.14;。关于 final 关键字的总结final 变量变量不能被修改即常量例如public static final double PI 3.1415;final 类类不能被继承。final 方法方法不能被重写。十一、静态属性与静态方法非静态的属性和方法实例属性和方法必须通过创建对象来访问而静态的属性和方法属于类本身在类加载到内存后就可以直接访问不需要创建对象并且可以被所有对象共享。public static void main(String[] args) { // Math math new Math(); // 错误Math 构造方法私有 // int max math.max(3, 5); // 静态方法调用类名.方法名() int max Math.max(3, 5); System.out.println(max); }注意事项静态方法只能访问静态方法和静态属性因为调用静态方法时对象实例可能尚未创建。静态方法中不能使用this和super关键字因为当前对象实例可能尚未创建。public class Student { // 实例变量成员变量 public int id; private String name; // 静态变量类变量 public static String country 中国; // 实例方法非静态方法 public void show() { System.out.println(Student.show); print(); // 实例方法可以调用静态方法 } // 静态方法 public static void print() { System.out.println(Student.print); // show(); // 错误静态方法不能调用非静态方法 // this.name lisi; // 错误静态方法中不能使用 this country zhongguo; // 正确可以修改静态变量 } public static void test() { test(); // 递归调用静态方法可以调用自身 } // Getter 和 Setter 方法 public int getId() { return id; } public void setId(int id) { this.id id; } public String getName() { return name; } public void setName(String name) { this.name name; } }十二、工具类Math、Arrays工具类中的方法都是静态方法方法之间相互独立。工具类的构造方法通常被设为私有禁止外部创建对象只能通过类名.方法名()的方式访问。// Math 工具类示例 int max1 Math.max(3, 2); System.out.println(max1); double max2 Math.max(3.0, 5.2); System.out.println(max2); // Math math new Math(); // 错误Math() has private access in java.lang.Math // Arrays 数组操作工具类示例 int[] array {34, 45, 7, 89}; System.out.println(array); // 输出数组地址[Id7b1517 System.out.println(Arrays.toString(array)); // 输出数组内容[34, 45, 7, 89] Arrays.sort(array); // 排序 System.out.println(Arrays.toString(array)); // 输出排序后[7, 34, 45, 89] // 先使用 Arrays.sort() 排序再进行二分查找 int index Arrays.binarySearch(array, 7); System.out.println(index); // 输出0// 自定义工具类示例 public class ArraysUtil { // 工具类不希望被实例化应将构造方法设为私有 private ArraysUtil() { } // 工具类方法通过 类名.方法名() 调用 public static int max(int[] array) { if (array null || array.length 0) { return -1; } int max array[0]; for (int i 1; i array.length; i) { if (array[i] max) { max array[i]; } } return max; } public static int min(int[] array) { if (array null || array.length 0) { return -1; } int min array[0]; for (int i 1; i array.length; i) { if (array[i] min) { min array[i]; } } return min; } public static void bubbleSort(int[] array) { for (int i 0; i array.length - 1; i) { for (int j 0; j array.length - 1 - i; j) { if (array[j] array[j 1]) { int temp array[j]; array[j] array[j 1]; array[j 1] temp; } } } } }