#import #import "Car.h" #import "Phone.h" int main(int argc, const char * argv[]) { Car *c1 = [[Car alloc] init]; Car *c2 = [[Car alloc] initWithBrand:@"QQ" price:40000.0]; //給c1設置品牌和價格 [c1 setBrand:@"蘭博基尼"]; [c1 setPrice:3000000.0]; NSLog(@"%@ %.2f",[c1 brand],[c1 price]); //創建手機 Phone *p1 = [[Phone alloc] init]; //設置屬性 /* [p1 setBrand:@"諾基亞"]; [p1 setColor:@"白色"]; [p1 setPrice:2000.0]; [p1 setAddress:@"中國"]; */ //用點語法代替 p1.brand = @"諾基亞"; p1.color = @"白色"; NSLog(@"手機是%@品牌的",p1.brand); //用KVC賦值(以間接的方式對屬性進行賦值) [p1 setValue:@"摩托羅拉" forKey:@"brand"]; [p1 setValue:@"墨綠色" forKey:@"color"]; NSLog(@"手機是%@品牌的",p1.brand); return 0; }