#import 的位置究竟改放在h文件还是m文件中

本文探讨了在编程中将#import指令正确放置于.h头文件的重要性,并详细解释了当A引用B且B调用A时,如何避免出现“无法找到接口声明”的交叉引用错误。

首先一个原则 #import 应该放在.h头文件中

如果有些特殊的情况 例如 A中引用到了B ,B中调用到了A,这种情况下就需要放在 .m文件里面了。否则会出现
Bug:“can not find interface declaration” 交叉引用错误。

### C#(Windows Forms) 在 C# 的 Windows Forms 项目中,若要将 `AddTimestampedLog` 方法放在其他文件中声明,可按如下步骤操作: #### 1. 创建新类文件 创建一个新的类文件,例如 `LoggingHelper.cs`,并在其中定义 `AddTimestampedLog` 方法。 ```csharp using System; using System.Windows.Forms; namespace WindowsFormsApp { public static class LoggingHelper { public static void AddTimestampedLog(ListBox listBox, string message) { string timestampedMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}"; if (listBox.InvokeRequired) { listBox.Invoke(new Action(() => listBox.Items.Add(timestampedMessage))); } else { listBox.Items.Add(timestampedMessage); } } } } ``` #### 2. 在主窗体中调用该方法 在主窗体的代码文件(如 `Form1.cs`)中调用 `LoggingHelper` 类的 `AddTimestampedLog` 方法。 ```csharp using System; using System.Windows.Forms; namespace WindowsFormsApp { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { LoggingHelper.AddTimestampedLog(listBox1, "这是一条测试消息"); } } } ``` ### Python(Tkinter) 在 Python 的 Tkinter 项目中,若要将 `add_timestamped_log` 函数放在其他文件中声明,可按如下步骤操作: #### 1. 创建新的 Python 文件 创建一个新的 Python 文件,例如 `logging_helper.py`,并在其中定义 `add_timestamped_log` 函数。 ```python import datetime def add_timestamped_log(listbox, message): timestamped_message = f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - {message}" listbox.insert("end", timestamped_message) ``` #### 2. 在主程序中导入并调用该函数 在主程序文件中导入 `add_timestamped_log` 函数并调用。 ```python from tkinter import * from logging_helper import add_timestamped_log win = Tk() frame = Frame(win) listbox = Listbox(frame) listbox.pack() frame.pack() button = Button(win, text="添加日志", command=lambda: add_timestamped_log(listbox, "这是一条测试消息")) button.pack() win.mainloop() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值