【Objective-C】01-一个简单OC类的浅析

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------


刚开始学习oc,感觉跟C#的类有很大的不一样。

1、oc类的导入用的是#import关键字。

2、oc的基类是NSObject。类对象的创建要显示继承自NSObject。

3、oc的关键字都是以@开头,字符串也是以@开头。如@public表示成员变量的访问级别是公共的。

4、oc类的成员和方法在@interface和@end中声明,方法在@implementation和@end中实现。

5、类的对象方法以 - 开头,类方法以 + 开头。类型需要用括号括住。如- (void)run;

6、要执行某些行为或者方法时,需要用中括号括住。如

    [Car new] 指创建了一个新的Car对象

    [p run]指执行对象p的run方法

7、对象成员的访问用->符号表示,如p->wheels表示访问p对象的wheels成员。



//导入框架类
#import <Foundation/Foundation.h>
//声明类Car,并继承自NSObject,使类具有创造对象的能力
@interface Car:NSObject
{
    //@public声明它可以被外部的指针间接访问对象内部的成员变量
    @public
    int wheels;//轮胎
    int speed;//速度
}
//-减号表示当前方法是对象方法
//(void):oc中任何数据类型都要用括号括起来,void表示不返回值
//run是方法名。方法在@interface和@end之间声明
- (void)run;
@end
//实现类@interface中声明的方法
@implementation Car
//run方法的具体实现
- (void)run
{
    //打印并换行
    NSLog(@"车子快跑");
}
@end
int main()
{
    //oc中,用中括号[行为执行者 行为名称]表示执行某些行为。[Car new]指创建了一个新的Car对象
    //Car *p声明了一个Car类型的指针变量,并且指向等号右边创建的新对象
    Car *p=[Car new];
    //修改wheels成员变量。oc的类跟c中的结构相似,可以用指针->成员的方式修改成员
    p->wheels=4;
    p->speed=100;
    //执行run方法
    [p run];
    //打印信息,p->wheels表示获取对象的wheels成员
    NSLog(@"车子有%d个轮子,时速%d公里/小时",p->wheels,p->speed);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是用Objective-C纯代码实现的签到日期界面。实现的界面包括一个导航栏、一个显示当前日期的标签和一个签到按钮。 ViewController.h文件: ```objective-c #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end ``` ViewController.m文件: ```objective-c #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UILabel *dateLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 设置导航栏标题 self.navigationItem.title = @"签到"; // 设置背景颜色 self.view.backgroundColor = [UIColor whiteColor]; // 创建并添加日期标签 self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)]; self.dateLabel.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2 - 50); self.dateLabel.textAlignment = NSTextAlignmentCenter; self.dateLabel.textColor = [UIColor blackColor]; [self.view addSubview:self.dateLabel]; // 更新日期标签的文本 [self updateDateLabel]; // 创建并添加签到按钮 UIButton *checkInButton = [UIButton buttonWithType:UIButtonTypeSystem]; checkInButton.frame = CGRectMake(0, 0, 200, 50); checkInButton.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2 + 50); [checkInButton setTitle:@"签到" forState:UIControlStateNormal]; [checkInButton addTarget:self action:@selector(checkInButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:checkInButton]; } // 更新日期标签的文本 - (void)updateDateLabel { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy年MM月dd日"; self.dateLabel.text = [formatter stringFromDate:[NSDate date]]; } // 签到按钮点击事件 - (void)checkInButtonClicked:(UIButton *)sender { // TODO: 处理签到逻辑 // 更新日期标签的文本 [self updateDateLabel]; } @end ``` 在AppDelegate.m文件中,将ViewController设置为根视图控制器: ```objective-c #import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; ViewController *viewController = [[ViewController alloc] init]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; self.window.rootViewController = navigationController; [self.window makeKeyAndVisible]; return YES; } ``` 这样就完成了一个简单的签到日期界面的实现。运行程序后,可以看到一个带有日期标签和签到按钮的界面。点击签到按钮后,日期标签上的日期会更新为当前日期。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值