List的分支之Vector的特有功能

本文深入探讨了Java中Vector类的特有功能,包括addElement和elementAt等方法的使用,以及通过Enumeration进行遍历的过程。展示了Vector如何提供线程安全的集合操作。
package cn.itcast_01;

import java.util.Enumeration;
import java.util.Vector;

/*
 * Vector的特有功能
 * 
 * 1.添加功能
 *   public void addElement(Object obj);   ---add()
 * 2.获取功能
 *   public Object elementAt(int index)    ---get()
 *   public Enumeration elements();        ---Interator interator()
 *           boolean hasMoreElements()     ---hasNext()
 *           Object nextElement()          ---next()
 * JDK升级的原因
 *       A:安全
 *       B:效率
 *       C:简化书写
 *       
 */
public class Vector的特有功能 {

	public static void main(String[] args) {
	      //创建集合对象
		Vector v= new Vector();
		//添加功能
		v.addElement("hello");
        v.addElement("word");
        v.addElement("java");
        //遍历
        for(int x =0;x<v.size();x++) {
        	String s = (String) v.elementAt(x);
        	System.out.println(s);
        }
        
        Enumeration en = v.elements();//返回的是子类对象,迭代器很想
        while(en.hasMoreElements()) {
        	String s= (String)en.nextElement();
        	System.out.println(s);
        }
	}
}
# ✅ 解析后的子类如何被使用 —— 实战指南 你现在已经完成了如下工作: 1. 使用 `ExcelFieldTypeClassifier` 判断输入字符串属于哪种类型(Fixed / List / Range / Condition) 2. 使用工厂类 `ExcelFieldTypeFactory` 创建对应的子类对象(如 `FixedExcelField`, `ListExcelField` 等) 3. 所有子类都继承自统一接口 `ExcelFieldType` --- ## 🎯 本节目标 > 如何让这些解析后的子类真正“**发挥作用**”? > 如何让上层逻辑根据字段类型做出不同行为? --- ## 🧩 步骤一:定义子类的公共行为(接口) 你在抽象基类中已经定义了如下方法,用于扩展功能: ```cpp class ExcelFieldType { public: virtual ~ExcelFieldType() {} virtual const std::string& getExpression() const = 0; virtual std::string getTypeName() const = 0; // 子类实现这个函数,完成自己的具体业务逻辑 virtual void parse() = 0; }; ``` 但光有 `parse()` 是不够的。我们可以为每个字段类型设计更具体的接口和行为。 --- ## 🔨 步骤二:给每个子类增加实际的功能逻辑 ### 示例1:`FixedExcelField` 只返回固定值 ```cpp class FixedExcelField : public ExcelFieldType { public: FixedExcelField(const std::string& expr) : m_expression(expr) {} const std::string& getExpression() const { return m_expression; } std::string getTypeName() const { return "Fixed"; } void parse() { // 固定值无需解析,可以记录日志或直接返回 std::cout << "[Fixed] 表达式: " << m_expression << std::endl; } private: std::string m_expression; }; ``` --- ### 示例2:`ListExcelField` 拆分 `~` 字符串为列表 ```cpp #include <vector> class ListExcelField : public ExcelFieldType { public: ListExcelField(const std::string& expr) : m_expression(expr) { splitExpression(); } const std::string& getExpression() const { return m_expression; } std::string getTypeName() const { return "List"; } void parse() { std::cout << "[List] 共 " << m_list.size() << " 项:" << std::endl; for (const auto& item : m_list) { std::cout << " - " << item << std::endl; } } private: void splitExpression() { size_t start = 0, pos; while ((pos = m_expression.find('~', start)) != std::string::npos) { m_list.push_back(m_expression.substr(start, pos - start)); start = pos + 1; } if (start < m_expression.size()) { m_list.push_back(m_expression.substr(start)); } } std::string m_expression; std::vector<std::string> m_list; }; ``` --- ### 示例3:`RangeExcelField` 分割 `^` 表示的范围 ```cpp class RangeExcelField : public ExcelFieldType { public: RangeExcelField(const std::string& expr) : m_expression(expr) { parseRange(); } const std::string& getExpression() const { return m_expression; } std::string getTypeName() const { return "Range"; } void parse() { std::cout << "[Range] 范围: " << m_low << " ~ " << m_high << std::endl; } private: void parseRange() { size_t pos = m_expression.find('^'); if (pos != std::string::npos) { std::istringstream issLow(m_expression.substr(0, pos)); std::istringstream issHigh(m_expression.substr(pos + 1)); issLow >> m_low; issHigh >> m_high; } else { m_low = m_high = 0.0; } } std::string m_expression; double m_low; double m_high; }; ``` --- ### 示例4:`ConditionExcelField` 提取条件语句 ```cpp class ConditionExcelField : public ExcelFieldType { public: ConditionExcelField(const std::string& expr) : m_expression(expr) { extractCondition(); } const std::string& getExpression() const { return m_expression; } std::string getTypeName() const { return "Condition"; } void parse() { std::cout << "[Condition] 条件表达式: " << m_condition << std::endl; } private: void extractCondition() { size_t start = m_expression.find('^'); if (start != std::string::npos) { m_condition = m_expression.substr(start + 1); } else { m_condition = "无条件"; } } std::string m_expression; std::string m_condition; }; ``` --- ## 📦 步骤三:在工厂类中创建并使用它们 ```cpp ExcelFieldType* field = ExcelFieldTypeFactory::createField(classifier); if (field) { std::cout << "表达式: \"" << field->getExpression() << "\", 类型: " << field->getTypeName() << std::endl; field->parse(); // 调用各个子类的特有逻辑 delete field; // C++98 需手动释放 } ``` --- ## ✅ 示例输出结果 对于如下表达式: ```cpp std::string expressions[] = { "DAYTON", "\"X\"~\"R\"~\"L\"", "0.15^W/2", "13_0^if(IsShowXL == \"true\")(XL-18)else(L-18)" }; ``` 输出可能是: ``` 表达式: "DAYTON", 类型: Fixed [Fixed] 表达式: DAYTON 表达式: ""X"~"R"~"L"", 类型: List [List] 共 3 项: - "X" - "R" - "L" 表达式: "0.15^W/2", 类型: Range [Range] 范围: 0.15 ~ 0 表达式: "13_0^if(IsShowXL == "true")(XL-18)else(L-18)", 类型: Condition [Condition] 条件表达式: if(IsShowXL == "true")(XL-18)else(L-18) ``` --- ## 🧠 知识点详解 | 知识点 | 描述 | |--------|------| | **多态调用** | 上层逻辑不需要知道具体子类,只需调用统一接口 `parse()` 就能执行各自逻辑。 | | **职责清晰** | 每个子类负责处理自己类型的数据结构和解析方式。 | | **易于拓展** | 后续新增字段类型时只需添加新子类和工厂分支即可。 | --- ## ✅ 总结 你现在掌握了: ✅ 如何让每个子类做“真正有用的事”,不只是分类 ✅ 如何在工厂模式下统一调用子类功能 ✅ 如何把解析后的数据传递到上层逻辑进行处理 --- ## 📌 下一步建议 你可以继续拓展如下功能: - 将解析后的值(如 `std::vector<std::string>`、`double low/high`)封装成结构体或类保存 - 定义 `ParsedValue` 或 `FieldValue` 类用于保存每种字段的实际内容 - 支持从 Excel 文件读取字段并批量构建这些对象 - 在 UG NX 中集成这些字段,用于参数建模、特征驱动等场景 如果你希望我帮你继续完善这些功能,请告诉我 😊输出到控制台上并不能让我使用到它们,我得获取它们解析后的结果才能使用上它们
07-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unknown To Known

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值