利用C语言结构体模拟一个简单的JavaBean

利用C语言模拟一个Javabean

仅封装了,“无参构造函数”,“带参构造函数”,"toString方法"

 

#include <stdio.h>


struct User{

    int ID;
    char *name;
    char *password;
    int age;

};
void newUser(struct User *,const int ,const char *,const char *,const int);

void printUserInfo(struct User *);
void copyUser(struct User *,const struct User *);

int main(){
    //char *p;
    //printf("%d", sizeof(p));

    //char p[10];
    //strcpy(p, NULL); 不合法

    //BasePart
        struct User user1;
        user1.ID = 1;
        char name1[] = "lifei";
        //printf("%d\n", sizeof(name1));
        user1.name = malloc(sizeof(name1));
        printf("sizeof(user1.name): %d 个字节\n", sizeof(user1.name));//*user1.name 首地址就1个字节。
        printf("user1.name占地: %d 个字节\n", strlen(user1.name));//为什么是24个字节呢,刚刚分配过来,就是有很大?

        strcpy(user1.name, name1);
        printf("sizeof(user1.name): %d 个字节\n", sizeof(user1.name));//*user1.name 首地址就1个字节。
        printf("user1.name占地: %d 个字节\n", strlen(user1.name));//这里就比较好,结果是5

        char password1[] = "123456";
        user1.password = malloc(sizeof(password1));
        strcpy(user1.password, password1);
        user1.age = 24;
        printUserInfo(&user1);
        //printf(user1.name);
    
        /*printf("%s\n", user1.name);
        printf("%p\n", user1.name);
        printf("%d\n",sizeof(user1.name));*/
    


    
    //struct User user2;
    //user2 = user1;//因为不面向对象,所以不可能点出来,所以 要传递两个对象进去对嘛
    //user2.ID = 3;
    //strcpy(user1.name, "rio");//这里越界有个异常,最好自己写个啥,然后就按之前视频里演示的那样,新来谁,就把谁创建,再赋值进去。TODO
    //printUserInfo(&user2);
    //printUserInfo(&user1);
    //这种修改 肯定不行,仅年龄跟id改了,但是姓名跟密码是一起改的,所以,姓名跟密码要指定新的位置。

    struct User user2;
    copyUser(&user2, &user1);//以上两句话相当于 :User user2 = new User(user1);
    strcpy(user1.name, "rio");
    user2.ID = 2;

    printUserInfo(&user2);//相当于toString();还是重载过的
    printUserInfo(&user1);

    struct User user3;
    newUser(&user3, 3,"reeven", "qwerty", 28);//以上两句话,相当于 User user3 = new User(3,"reeven", "qwerty", 28);
    printUserInfo(&user3);



    free(user1.name);
    free(user1.password);
    free(user2.name);
    free(user2.password);
    free(user3.name);
    free(user3.password);
    getchar();
    return 0;



}

void copyUser(struct User *dest, const struct User *src){

    dest->ID = src->ID;
    //printf("%s\n", src->name);
    //printf("%d\n", sizeof(src->name));
    //printf("%d\n", sizeof(*(src->name)));
    dest->name = malloc(strlen(src->name)+1);//+1表示\0
    if (src->name != NULL){
        strcpy(dest->name, src->name);
    }
    dest->password = malloc(strlen(src->password) + 1);
    if (src->password != NULL){
        strcpy(dest->password, src->password);
    }
    dest->age = src->age;
}
void printUserInfo(const struct User *user){

    printf("用户信息:id:%d,用户名:%s,密码:%s,年龄:%d\n", user->ID, user->name, user->password, user->age);
}

/** 
    相当于构造函数了。。。
*/
void newUser(struct User *user,const int id, const char *name, const char *password,const int age){

    user->ID = id;
    user->name = malloc(strlen(name) + 1);
    if (name != NULL){
        strcpy(user->name, name);
    }
    user->password = malloc(strlen(password) + 1);
    if (password != NULL){
        strcpy(user->password, password);
    }
    user->age = age;

}

 

posted on 2016-03-03 22:31  木鸟飞  阅读(674)  评论(0编辑  收藏  举报

导航