泛型类、泛型方法、泛型接口
This commit is contained in:
parent
e4d146365b
commit
26fcbfbd91
@ -1,4 +1,33 @@
|
||||
package chapter6;
|
||||
|
||||
public class Example22 {
|
||||
// T 标识一个不确定的类型
|
||||
class Score<T> {
|
||||
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<Integer> s1 = new Score<Integer>("小明", "数学", 100);
|
||||
Score<String> s2 = new Score<String>("小明", "英语", "A");
|
||||
Score<String> s3 = new Score<String>("小明", "军事理论", "合格");
|
||||
|
||||
// 打印三门课程的成绩
|
||||
System.out.println(s1);
|
||||
System.out.println(s2);
|
||||
System.out.println(s3);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,29 @@
|
||||
package chapter6;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
|
||||
public class Example23 {
|
||||
public static void main(String[] args) {
|
||||
// 存放字符串的集合
|
||||
ArrayList<String> names = new ArrayList<String>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,36 @@
|
||||
package chapter6;
|
||||
|
||||
|
||||
|
||||
public class Example24 {
|
||||
// 泛型方法:验证订单信息
|
||||
public static <T> 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);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,59 @@
|
||||
package chapter6;
|
||||
|
||||
public class Example25 {
|
||||
// 定义支付接口(泛型接口)
|
||||
interface Payment<T> {
|
||||
boolean pay(T amount); // 支付方法
|
||||
T getBalance(); // 获取余额
|
||||
}
|
||||
|
||||
// 方式一:明确类型的接口实现 - 支付宝支付
|
||||
class AliPay implements Payment<Double> {
|
||||
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<T> implements Payment<T> {
|
||||
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<Integer> campusCard = new CampusCard<>(500);
|
||||
campusCard.pay(100);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user