第一部分 1.12 OC基础语法-类的声明和实现,static和变量,typedef

新建一student类

只是用来声明Student这个类有哪些成员变量和方法

#import <Foundation/Foundation.h>

// @interface代表声明一个类
// : 代表继承
@interface Student : NSObject { // 成员变量要定义在下面的大括号中{}
    int age;
    int no;
}

// 在这里声明的所有方法都是公共

// age的get方法
// - 代表动态方法  + 代表静态方法
- (int)age;

// age的set方法
- (void)setAge:(int)newAge;

// no的get方法
- (int)no;

- (void)setAge:(int)newAge andNo:(int)newNo;
@end
#import "Student.h"

@implementation Student

- (int)age {
    NSLog(@"调用了getAge方法");
    return age;
}

- (void)setAge:(int)newAge {
    age = newAge;
    
    NSLog(@"调用了setAge方法");
}

- (int)no {
    return no;
}

- (void)setAge:(int)newAge andNo:(int)newNo {
    age = newAge;
    no = newNo;
}
@end

main.m

#import <Foundation/Foundation.h>

#import "Student.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        // 创建一个Student对象:
        
        // 1.调用一个静态方法alloc来分配内存
        // 暂时把id当做是任何对象
//        Student *stu = [Student alloc];
//        
//        // 2.调用一个动态方法init进行初始化
//        stu = [stu init];
        
        Student *stu = [[Student alloc] init];
        
        //[stu setAge:100];
        
        //int age = [stu age];
        
        //NSLog(@"age is %i", age);
        
        [stu setAge:17 andNo:1];
        
        NSLog(@"age is %i and no is %i", [stu age], [stu no]);
        
        // 释放对象
        [stu release];
    }
    return 0;
}

二、static和变量

#include <stdio.h>
// 如果在不同源文件出现了同名的内部变量,那么这些变量将互不干扰
static int b;

// 用static修饰的全部变量,可以称为内部变量
static int a;

void testA() {
    printf("one.c中的a=%d\n", a);
}

main.c

#include <stdio.h>

static int b;

void testA();

extern int a;

int main(int argc, const char * argv[])
{
    a = 10;
    
    testA();
    return 0;
}

int a;

 

 

// 完整地声明一个函数,需要用到extern关键字,表示引用一个外部函数
// extern void one();

// 其实extern又是废的,所以可以省略

 

// 定义一个one函数
// 完整地定义一个外部函数需要extern关键字
//extern void one() {
//    printf("调用了one函数\n");
//}

// 内部函数,需要用static关键字修饰,说明不能在其他文件中访问

 

三、typedef

#include <stdio.h>



// #define Integer int
// 给基本数据类型起别名
void test() {
    typedef int Integer;
    
    typedef Integer MyInteger;
    
    typedef unsigned int UInteger;
    
    int a = 10;
    
    Integer b = 9;
    
    UInteger c = 11;
    
    MyInteger d = 89;
}

// 给指针类型起别名
void test1() {
    char *s = "itcast";
    
    typedef char * String;
    
    String s1 = "itcast";
}

void test2() {
//    struct MyPoint {
//        float x;
//        float y;
//    };
//    struct MyPoint p = {10, 10};
    
    
    typedef struct {
        float x;
        float y;
    } Point;
    
    Point p = {10, 10};
}

void test3() {
    typedef struct {
        float x;
        float y;
    } Point;
    
    typedef Point * PP;
    
//    typedef struct Point {
//        float x;
//        float y;
//    } * PP;
    
    Point point = {10.0f, 20.0f};
    
    PP pp = &point;
    
    printf("x=%f, y=%f\n", pp->x, pp->y);
}

void test4() {
    typedef enum {
        spring,
        summer,
        autumn,
        winter
    } Season;
    
    Season s = spring;
}

int sum(int a, int b) {
    int c = a + b;
    printf("%d+%d=%d\n", a, b, c);
    return c;
}

// 给指向函数的指针定义一个别名SumPoint
void test5() {
    typedef int (*SumPoint)(int, int);
    
    SumPoint p = sum;
    
    (*p)(4, 5);
}

void test6() {
    typedef char * String1;
    #define String2 char *
    
    String1 s1,s2;
//    String1 s1;
//    String1 s2;
    
    String2 s3,s4;
//    char *s3, s4;
//    char *s3;
//    char s4;
}

int main(int argc, const char * argv[])
{
    
    int a, b;
    
//    int a;
//    int b;
    
    return 0;
}

 

 

转载于:https://www.cnblogs.com/N-eal/p/4525235.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值