zoukankan      html  css  js  c++  java
  • 利用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
    ", sizeof(name1));
            user1.name = malloc(sizeof(name1));
            printf("sizeof(user1.name): %d 个字节
    ", sizeof(user1.name));//*user1.name 首地址就1个字节。
            printf("user1.name占地: %d 个字节
    ", strlen(user1.name));//为什么是24个字节呢,刚刚分配过来,就是有很大?
    
            strcpy(user1.name, name1);
            printf("sizeof(user1.name): %d 个字节
    ", sizeof(user1.name));//*user1.name 首地址就1个字节。
            printf("user1.name占地: %d 个字节
    ", 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
    ", user1.name);
            printf("%p
    ", user1.name);
            printf("%d
    ",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
    ", src->name);
        //printf("%d
    ", sizeof(src->name));
        //printf("%d
    ", sizeof(*(src->name)));
        dest->name = malloc(strlen(src->name)+1);//+1表示
        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
    ", 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;
    
    }
  • 相关阅读:
    2020春软件工程助教工作总结 第三周
    Zend Framework MVC的结构
    Zend_Cache的使用
    小油2018 win7旗舰版64位GHOST版的,安装telnet客户端时,提示:出现错误。并非所有的功能被成功更改。
    redis常用配置参数详解
    CentOS 7 源码编译安装 Redis
    Linux(CentOS)下设置nginx开机自动启动(2个办法)
    CST,CET,UTC,GMT,DST,Unix时间戳几种常见时间概述与关系(转)
    PHP_OS的常见值
    PHP_SELF、 SCRIPT_NAME、 REQUEST_URI区别
  • 原文地址:https://www.cnblogs.com/letben/p/5240372.html
Copyright © 2011-2022 走看看