关于.h和.m文件如何在目标c中交互的简明描述? [英] Concise description of how .h and .m files interact in objective c?
问题描述
我刚刚开始学习目标C,但我真的很困惑.h和.m文件如何相互影响.这个简单的程序有3个文件:
I have just started learning objective C and am really confused how the .h and .m files interact with each other. This simple program has 3 files:
Fraction.h
Fraction.h
#import <Foundation/NSObject.h>
@interface Fraction : NSObject {
int numerator;
int denominator;
}
- (void) print;
- (void) setNumerator: (int) n;
- (void) setDenominator: (int) d;
- (int) numerator;
- (int) denominator;
@end
Fraction.m
Fraction.m
#import "Fraction.h"
#import <stdio.h>
@implementation Fraction
-(void) print { printf( "%i/%i", numerator, denominator ); }
-(void) setNumerator: (int) n { numerator = n; }
-(void) setDenominator: (int) d { denominator = d; }
-(int) denominator { return denominator; }
-(int) numerator { return numerator; }
@end
Main.m
#import <stdio.h>
#import "Fraction.h"
int main(int argc, char *argv[]) {
Fraction *frac = [[Fraction alloc] init];
[frac setNumerator: 1];
[frac setDenominator: 3];
printf( "The fraction is: " );
[frac print];
printf( "\n" );
[frac release];
return 0;
}
据我了解,该程序最初开始运行main.m文件.我了解基本的C概念,但是整个类"和实例"的内容确实令人困惑.在Fraction.h文件中,@ interface将分子和分母定义为整数,但是下面对(void)还要做什么呢?在下面重新定义的目的是什么?对于Fraction.m的(void)和(int)部分正在发生什么以及如何将所有这些如何合并到main.m文件中,我也感到很困惑.我想我想说的是,这看起来像是一个相当容易的程序,用于学习不同部分如何相互配合-有人可以用非技术术语解释吗?
From what I understand, the program initially starts running the main.m file. I understand the basic C concepts but this whole "class" and "instance" stuff is really confusing. In the Fraction.h file the @interface is defining numerator and denominator as an integer, but what else is it doing below with the (void)? and what is the purpose of re-defining below? I am also quite confused as to what is happening with the (void) and (int) portions of the Fraction.m and how all of this is brought together in the main.m file. I guess what I am trying to say is that this seems like a fairly easy program to learn how the different portions work with each other - could anyone explain in non-tech jargon?
推荐答案
来自其他环境的人们似乎总是相信,在C和Objective- C编程.
People who come from other environments always seem to belive that something complicated is happening with the .c, .m, and .h files used in C and Objective-C programming.
实际上,它非常非常简单.
Actually, its very, VERY simple.
出于构建项目的目的,集成开发环境(如XCode)忽略所有.h文件.他们要做的是获取每个.c和.m文件并进行编译.如果程序员(也就是您)使用了任何#include或#import指令,则编译器会将包含/导入的.h文件的整个文本插入该指令所在的位置.
For the purpose of buiding a project Integrated Development Environments - like XCode - ignore all the .h files. What they do do is to take each .c and .m file and compile it. If the programmer (thats you) has used any #include, or #import directives, the compiler inserts the entire text of the included/imported .h file where the directive was.
因此,如果您有一个.h文件-insert.h-表示:
So, if you had a .h file - insert.h - that said:
in
.c文件中显示:
Alice
#include "insert.h"
Wonderland
编译器将在处理#include& #import指令,请参见:
The compiler would, after processing the #include & #import directives, see this:
Alice
in
Wonderland
我们使用这种非常简单的文件合并行为来制作复杂的程序:)
It is this very VERY simple file merging behavior that we use to make complicated programs :)
.h是一个非常简单的约定,程序员可以通过该约定相互告知文件适合使用#include或#import合并(可能多次).
.h is very simply a convention by which programmers can tell each other that the file is suitable to be merged in - potentially multiple times - using #include or #import.
.c和.m文件不是那样合并的.每个.c和.m文件都单独编译-生成.o文件.每个.o文件都是编译函数的集合.然后将.o文件合并(或链接")以生成最终程序. 链接步骤可确保每个功能仅存在一次,并且所有被调用的功能实际上都存在于某个地方.
The .c and .m files are not merged like that. Each .c and .m file is compiled seperately - to produce .o files. Each .o file is a collection of compiled functions. The .o files are then merged - or "linked" - to produce the final program. The linking step ensures that each function exists only once, and that all functions that are called do in fact exist somewhere.
C&目标C定义了一个必须存在于某处的特殊功能-main()
.同样,该语言非常轻松-不在乎main()
函数位于哪个.c或.m文件中.只是它存在于某个文件中的某个位置.
C & Objctive-C define one special function that must exist somewhere - main()
. Again, the language is very relaxed - it doesn't care which .c or .m file the main()
function is in. Merely that it exists in some file somewhere.
这篇关于关于.h和.m文件如何在目标c中交互的简明描述?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!