I have a game which have 5 class/level.Each class contain almost same variables.Now i want to know in which place should i declare my class variable.I have three option-
1.declaring the class variable in corresponding class .h file. 2.declaring a variable .h file(开发者_高级运维an empty .h file only for declaring variable and then import it to corresponding class.m file). 3.declaring the class variable in class .m file before init method.
Now I want to know which is the correct method.
**each class must import other class .h file because after completing level-1 i have to go level-2.So for scene transition i have to import level-2.h file and so on.
And one fore things if i import a class.h file which has same class variable like my class file ,then is it conflict with my class variable, or what will happen??
To answer your question: "if i import a class.h file which has same class variable like my class file ,then is it conflict with my class variable, or what will happen??"
No there will not be a conflict. When you define instance variables in a class, it is for objects of that class type only. For example, if you have Class1 which has a property UIColor *color
and Class2 has a property UIColor *color
, and you include the Class2.h file in class1.m, then you do something like color = [UIColor blueColor];
in a method inside Class1.m you will be referring to class1's color property. To refer to the color property of class2, you would have to instantiate a class2 object and then set it's property like so:
Class2 *c2 = [[Class2 alloc] init];
c2.color = [UIColor greenColor];
Hope that helps a little.
Also, as others have mentioned, you should really just have one game class and either create new instances of the same class or just change the properties of the existing instance. If the 5 classes are very similar, but just harder, you should just have a difficulty property, and then use new instances of the same class with that property set to a higher value.
And, to clear any confusion, let's say you have Class1 again and it's a view controller. You can, inside a method in Class1, create a new Class1 object and display it and it will be a new instance of Class1, with whatever different properties you set.
If the classes are that similar, it sounds like they should probably have a common superclass.
精彩评论