In objective-C, there is no static class variable, but we can simulate it. However, if there is an inheritance relationship between class objects, this is a little complicated. Let's assume that a coupon class object needs to save the number of coupons currently remaining, and a newcoupon Class Object inherited from the coupon class object, but it also saves the number of available coupons, the following example shows how to ensure that their static class variables are independent of each other.
Coupon. h
# Import<Foundation/Foundation. h>
@ Interface coupon: nsobject {
}
+(Int) Numbercouponsleft;
+(Void) Resetcoupon;
@ End
Coupon. m
# Import " Coupon. h "
# Define Initialize al_coupon_allocation 100
// Set static variables
Static Int Availablecoupons = Initial_coupon_allocation;
// Define Private Static accessors in implementation classes
@ Interface coupon ()
+ ( Int ) Availablecoupons;
+ ( Void ) Setavailablecoupons :( Int ) Inavailablecoupons;
@ End
@ Implementation coupon
+ ( Int ) Availablecoupons
{
Return Availablecoupons;
}
+ ( Void ) Setavailablecoupons :( Int ) Inavailablecoupons
{
Availablecoupons = Inavailablecoupons;
}
// To support inheritance of static classes, focus on init.
- (ID) Init
{
If (Self = [Super init])
{
// [Self class] indicates the object used as the receiver. If it is an inherited class, it refers to the inherited class object.
If ([Self Class ] Availablecoupons] = 0 )
{
// If the valid coupon value is 0, this class object is destroyed.
[Self release];
Return Nil;
}
// Currently valid coupon minus 1
[[Self Class ] Setavailablecoupons: [[self Class ] Availablecoupons] - 1 ];
}
Return Self;
}
+ ( Int ) Numbercouponsleft
{
Return [Self availablecoupons];
}
+ ( Void ) Resetcoupon
{
[Self setavailablecoupons: initial_coupon_allocation];
}
@ End
Newcoupon. h
# Import<Foundation/Foundation. h>
# Import"Coupon. h"
@ Interface newcoupon: coupon {
}
@ End
Newcoupon. m
# Import " Newcoupon. h "
# Define Initial_new_coupon_allocation 200
// Set new static variables in the inheritance class
Static Int Availablecoupons = Initial_new_coupon_allocation;
@ Interface newcoupon ()
+ ( Int ) Availablecoupons;
+ ( Void ) Setavailablecoupons :( Int ) Inavailablecoupons;
@ End
@ Implementation newcoupon
+ ( Int ) Availablecoupons
{
Return Availablecoupons;
}
+ ( Void ) Setavailablecoupons :( Int ) Inavailablecoupons
{
Availablecoupons = Inavailablecoupons;
}
+ ( Void ) Resetcoupon
{
[Self setavailablecoupons: initial_new_coupon_allocation];
}
@ End
Main. m
# Import < Foundation / Foundation. h >
# Import " Coupon. h "
# Import " Newcoupon. h "
Int Main ( Int Argc, Const Char * Argv [])
{
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
// Insert code here...
// Nslog (@ "Hello, world! ");
[[Coupon alloc] init];
Nslog ( @" % D " , [Coupon numbercouponsleft]);
[Coupon resetcoupon];
Nslog ( @" % D " , [Coupon numbercouponsleft]);
[[Newcoupon alloc] init];
Nslog ( @" % D " , [Newcoupon numbercouponsleft]);
[Newcoupon resetcoupon];
Nslog ( @" % D " , [Newcoupon numbercouponsleft]);
[Pool drain];
Return 0 ;
}
Returned results:
[Switching to process 3200 Thread 0x0 ]
2011 - 05 - 03 10 : 15 : 08.505 Demo04 [ 3200 : 903 ] 99
2011 - 05 - 03 10 : 15 : 08.508 Demo04 [ 3200 : 903 ] 100
2011 - 05 - 03 10 : 15 : 08.508 Demo04 [ 3200 : 903 ] 199
2011 - 05 - 03 10 : 15 : 08.509 Demo04 [ 3200 : 903 ] 200