从C转到JAVA学习路之struct与class对比


       JAVA里最牛B的最基本的就是类,而C语言中的struct也可以定义自己的数据结构, 它们有很多相似的也有区别。在我看来它们之间最大的不同是struct里不支持方法,class里支持。
       方法是什么?就是函数,也就是说JAVA中的成员变量与函数归属于一个类的,C语言中有办法做到吗? 我想到的就是函数指针。
       先看如下C代码:
#include<stdio.h>
float Getlen(float r)//求周长
{
    return 3.14*r*r;
}
float Getrare(float r)//求面积
{
    return 2*3.14*r;
}
typedef struct s_circle
{
    float  r;
    float (*getlen)(float);
    float (*getrare)(float);
}Circle;
void main()
{
    Circle  cl;
    cl.getlen = Getlen;//关键点就在这里,将函数与类型关联起来
    cl.getrare = Getrare;
    printf("%f %f",cl.getlen(1), cl.getrare(2));
}



     我定义了一个Circle类型,里面有3个成员,一个普通变量,2个函数指针。在main()里面我将cl的函数指针与上面两个函数关联起来了,在使用的时候就直接调用,发现没有跟JAVA中的类非常相似了,都可以直接用方法了。
  下面看下JAVA中实现这个功能:
class Circle
{
    double  r;
    double getlen(double r)//求周长
	{
	    return 3.14*r*r;
	}
    double getrare(double r)//求面积
	{
	    return 2*3.14*r;
	}
}

public class example {
	 public static void main(String[] args) {
               Circle  cl = new Circle();
	       System.out.println(cl.getlen(1)+" "+cl.getrare(2));
	 }
}
     大家对比一下,都要定义成员,都要定义2个函数,使用时也是直接调用方法,是不是并没有什么区别嘛。
    如果只写一个类真的没什么区别,但是JAVA是以类为基本构造起来的,所以它将类功能发挥到了极致,加了很多功能。
    下面来看下类的继承与C中struct的关系:
     看下C的两个结构体:学生与工人
typedef struct s_student
{
    int age;
    char name[10];
    char sex[4];
    char number[20];//学号
    int  grade;//年级
}Student;
typedef struct s_work
{
    int age;
    char name[10];
    char sex[4];
    char company[20];//公司
    char type[10];  //工种
}Work;
      大家可以发现age, name, sex重复了,如果再定义老师,农民等类型。发现还是要重复写这3个成员,因为是人都会有这3个成员。所以可以提取出来一个类型,如下
typedef struct s_human
{
    int age;
    char name[10];
    char sex[4];
}Human;
typedef struct s_student
{
    Human info;
    char number[20];//学号
    int  grade;//年级
}Student;
typedef struct s_work
{
    Human info;
    char company[20];//公司
    char type[10];  //工种
}Work;
      这样以后不管定义多少种身份,都可以用Human这个基本信息了。
      细心的同学应该会发现,这尼妹跟JAVA里类的继承很像嘛。不过JAVA做的更完美, struct类型下的成员信息是全部公开可访问的,但现实生活中不是这样的,比如女生的年龄,你的存款等。java类成员可以用public, private, protect修饰。
       public就是公开都可以访问, private就是私有。如果你的存款想让家人或者儿子们知道,那就用protect修饰。这太生活化了,真的很好用很符合现实生活中的对象存在方式。

 总结: 面向对象和面向结构是给开发人员看的,对计算机来的就是执行语句,操作内存,根本不存在什么区别。
      这是对比C学JAVA的最后一篇文章了,后面将要用JAVA的思想去考虑问题,不过经过这次对比我发现学起JAVA来很爽,效率高了很多。
     为什么呢打个恶俗的比方就知道了:
      直接学JAVA:就像你面前站着一个穿了几十件衣服的苍老师,任你发挥,你需要先一件件的脱掉衣服,可能还没到脱完就兴趣没了。
      对比学JAVA:这个就像你面前站个没穿衣服的苍老师,你发挥完之后再慢慢的一件一件把衣服穿上,这时心态好多了(我现在就是这样感觉)
 
   
  
  

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#include"stdio.h" #include"stdlib.h" #define NULL 0 struct student { long num; char name[20]; int score[6]; struct student *next; }; void show() { printf("\nthere is a cataloge as follow.\n"); printf("***************************************\n"); printf("* *\n"); printf("* 1. create *\n"); printf("* 2. Insert *\n"); printf("* 3. print *\n"); printf("* 4. delete *\n"); printf("* 5. modify *\n"); printf("* 6. save_to_file *\n"); printf("* 7. lode from file *\n"); printf("* 8. exit *\n"); printf("***************************************\n"); printf("please input 1--8 to choice what you want:\n"); } struct student *create() { struct student *head,*p,*last; int i; int temp2; char name[20]; p=head=(struct student *)malloc(sizeof(struct student)); head->next=NULL; last=p; while(1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); /*last->next=p; last=p;*/ p->next=NULL; printf("number:"); scanf("%ld",&p->num); if(p->num==0)break; getchar(); printf("name:"); scanf("%s",p->name); printf("score:"); for(i=0;i<6;i++) { scanf("%d",&temp2); p->score[i]=temp2; } printf("next student's information.\n"); } free(p); return head; } void Insert(struct student *head) { struct student *p,*q; int score; long num; int i; printf("\nEnter the student's information you want to insert.\n"); printf("number:"); scanf("%ld",&num); q->num=num; getchar(); printf("name:"); scanf("%s",q->name); printf("score:(chinese,math,english,biology,physics,chemistry)\n"); for(i=0;i<6;i++) { scanf("%d",&score); q->score[i]=score; } q->next=NULL; p=head; while(p->next->num<q->num&&p->next!=NULL) p=p->next; q->next=p->next; p->next=q; if(p->next==NULL) p->next=q; } void delete(struct student *head) { struct student *p,*q; long num; printf("enter the student's information you want to delete.\n"); printf("number:"); scanf("%ld",&num); getchar(); p=head; while(p->next!=NULL&&p->next->num!=num) p=p->next; q=p->next; p->next=p->next->next; free(q); } void print(struct student *head) { struct student *p; int i; p=head->next; if(p==NULL) { printf("\nthere is no information.\n"); exit(0); } printf("\nnumber\tnamme\tchinese\tmath\tenglish\tbiology\tphysics\tchemistry\n"); while(p!=NULL) { printf("\n%ld\t%s",p->num,p->name); for(i=0;i<6;i++) printf("\t%d",p->score[i]); p=p->next; } } void modify(struct student *head) { struct student *p; int choice,i; long num; char name[20]; int score[6]; printf("\nEnter what student's information you want to modify.\n"); printf("number:"); scanf("%ld",&num); getchar(); printf("\nname:"); scanf("%s",name); printf("\n"); p=head->next; while(p->num!=num&&p->name[20]!=name[20]&&p!=NULL) p=p->next; printf("\nplease choice what you want to modify:1-number 2-name 3-score.\n"); scanf("%d",&choice); switch(choice) { case 1:printf("\nEnter the true number:"); scanf("%ld",&num); p->num=num; break; case 2:printf("\nEnter the true name:"); scanf("%s",p->name); break; case 3:printf("\nEnter the right score:"); for(i=0;i<6;i++) { scanf("%d",&score[i]); p->score[i]=score[i]; } break; } } void save_in(struct student *head) { struct student *p; FILE *fp; char file_name[30]; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"w"))==NULL) { printf("can't open the file.\n"); exit(0); } p=head; while(p->next!=NULL) { fwrite((void*)p->next,sizeof(struct student),1,fp); p=p->next; } fclose(fp); } struct student *load_from_file() { struct student *head,*p,*last; FILE *fp; char file_name[30]; head=(struct student *)malloc(sizeof(struct student)); last=head; head->next=NULL; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"r"))==NULL) { printf("can't open the file.\n"); exit(0); } p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; while(fp=fread((void *)p,sizeof(struct student),1,fp)==1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; } free(p); fclose(fp); return head; } void main() { struct student *la; int choice; /*char Yes_No;*/ la=(struct student *)malloc(sizeof(struct student)); la->next=NULL; while(1) { show(); scanf("%d",&choice); switch(choice) { case 1:la=create(); break; case 2:Insert(la); break; case 3:print(la); break; case 4:delete(la); break; case 5:modify(la); break; case 6:save_in(la); break; case 7:la=load_from_file(); break; case 8:exit(0); } } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值