diff --git a/src/chapter6/Example22.java b/src/chapter6/Example22.java index e47a284..bb842e9 100644 --- a/src/chapter6/Example22.java +++ b/src/chapter6/Example22.java @@ -1,4 +1,33 @@ package chapter6; -public class Example22 { +// T 标识一个不确定的类型 +class Score { + private String studentName; // 学生姓名 + private String subject; // 课程名称 + private T value; // 成绩值(可以是任意类型) + + // 构造方法 + public Score(String studentName, String subject, T value) { + this.studentName = studentName; + this.subject = subject; + this.value = value; + } + + public String toString() { + return studentName + "的" + subject + "成绩是" + value; + } +} + +public class Example22 { + public static void main(String[] args) { + // 创建三个成绩对象,分别存储小明的数学、英语、军事理论成绩 + Score s1 = new Score("小明", "数学", 100); + Score s2 = new Score("小明", "英语", "A"); + Score s3 = new Score("小明", "军事理论", "合格"); + + // 打印三门课程的成绩 + System.out.println(s1); + System.out.println(s2); + System.out.println(s3); + } } diff --git a/src/chapter6/Example23.java b/src/chapter6/Example23.java index 3ab2320..a3e5f2f 100644 --- a/src/chapter6/Example23.java +++ b/src/chapter6/Example23.java @@ -1,4 +1,29 @@ package chapter6; + +import java.util.ArrayList; +import java.util.Iterator; + + public class Example23 { + public static void main(String[] args) { + // 存放字符串的集合 + ArrayList names = new ArrayList(); + names.add("张三"); // √ 正确 + + Iterator it1 = names.iterator(); + while (it1.hasNext()) { + String name = (String) it1.next(); + System.out.println(name); + } + + ArrayList students = new ArrayList(); + names.add("张三"); + + Iterator it2 = students.iterator(); + while (it2.hasNext()) { + String student = (String) it2.next(); + System.out.println(student); + } + } } diff --git a/src/chapter6/Example24.java b/src/chapter6/Example24.java index b9ef937..6407073 100644 --- a/src/chapter6/Example24.java +++ b/src/chapter6/Example24.java @@ -1,4 +1,36 @@ package chapter6; + + public class Example24 { + // 泛型方法:验证订单信息 + public static boolean verify(T info) { + System.out.println("正在验证: " + info); + + if (info instanceof String) { + // 验证订单号 + String orderNo = (String) info; + return orderNo.length() == 10; // 假设订单号必须是10位 + } + else if (info instanceof Double || info instanceof Integer) { + // 验证订单金额 + double amount = Double.parseDouble(info.toString()); + return amount > 0; // 金额必须大于0 + } + return false; + } + + public static void main(String[] args) { + // 验证订单号 + boolean isValidOrderNo = verify("A123456789"); + System.out.println("订单号是否合法: " + isValidOrderNo); + + // 验证订单金额 + boolean isValidAmount = verify(99.9); + System.out.println("订单金额是否合法: " + isValidAmount); + + // 验证负数金额 + boolean isValidNegative = verify(-10.0); + System.out.println("负数金额是否合法: " + isValidNegative); + } } diff --git a/src/chapter6/Example25.java b/src/chapter6/Example25.java index 93a6035..d05c0d1 100644 --- a/src/chapter6/Example25.java +++ b/src/chapter6/Example25.java @@ -1,4 +1,59 @@ package chapter6; -public class Example25 { +// 定义支付接口(泛型接口) +interface Payment { + boolean pay(T amount); // 支付方法 + T getBalance(); // 获取余额 +} + +// 方式一:明确类型的接口实现 - 支付宝支付 +class AliPay implements Payment { + private Double balance = 1000.0; + + @Override + public boolean pay(Double amount) { + if (balance >= amount) { + balance -= amount; + System.out.println("支付成功:" + amount + "元"); + return true; + } + return false; + } + + @Override + public Double getBalance() { + return balance; + } +} + +// 方式二:泛型类型的接口实现 - 校园卡支付 +class CampusCard implements Payment { + private T balance; + + public CampusCard(T balance) { + this.balance = balance; + } + + @Override + public boolean pay(T amount) { + System.out.println("支付积分:" + amount); + return true; + } + + @Override + public T getBalance() { + return balance; + } +} + +public class Example25 { + public static void main(String[] args) { + // 测试支付宝支付 + AliPay aliPay = new AliPay(); + aliPay.pay(100.0); + + // 测试校园卡支付 + CampusCard campusCard = new CampusCard<>(500); + campusCard.pay(100); + } }