不知道如何将Java类转换为C头和代码

不知道如何将Java类转换为C头和代码,c,struct,C,Struct,我有以下java类: class Rect { int width; int height; Rect (int a, int b){ width = a; height = b; } int area() { return width * height; } } 但我不确定如何将其转换为C代码 到目前为止,我有: 标题.h #pragma once struct rect { int

我有以下java类:

class Rect {

    int width;
    int height;

    Rect (int a, int b){
        width = a;
        height = b;
    }

    int area() {
        return width * height;
    }
}
但我不确定如何将其转换为C代码

到目前为止,我有:

标题.h

#pragma once
struct rect {
    int width, height;
};

int area();
Header.c

int area() {
//How do I access these?       
//return width * height;
}
Main

#include "Header.h"

int main(int argc, char* argv[]) {
    struct rect rect;

    rect.height = 5;
    rect.width = 10;

    //How do I call rect.area() in here?    
}

您不能在
C
中执行此操作,但可以使用函数指针模拟此操作。创建一个函数指针,然后为其分配函数
区域
。(可能在某些初始化函数中)。但是你也必须做很多事情来清楚地模仿它

例如,这里没有
这个
的概念。因此,每次调用都必须传递结构的地址。所以我不确定这会有多大帮助

struct rect {
    int width, height;
    int (*f_area)(struct rect *);
};

int area(struct rect *p){
   return p->width*p->height;
}


这样说吧

 int ar = rectvar->area(&rectvar);

但现在你可能会说这真是愚蠢的事情。但是,
C
并不能满足你的要求。是的,就是这样

这就是当你扩展一种语言以获得它所没有的特性时,事情会变成什么样子。你可以像这样简单地使用面积函数-使你免于生活中的一些困难

struct rect rv;
rv.height = 10;
rv.width = 20;
int ar = area(&rv);
Basic C更喜欢传递指针。OO语言对每个方法都有一个隐式参数
this
——可以这么说

通常,该方法将标记为“类:”


结构和功能高于结构:

struct rect {
    int width, height;
};

struct rect *new_rect(int w,int h) {
    struct rect *this = malloc(sizeof struct rect); // should check bad alloc...
    this->width = w;
    this->height = h;
    return this;
}

int area(struct rect *this) { // in a method, 'this' can be viewed as an implicit parameter...
   return this->width*this->height;
}

int main(int argc, char* argv[]) {
    struct rect *rect = new_rect(5,10);
    area(rect);
    // beware of memory leaks, should add some destructor...
}

C不提供从对象调用函数并使其对特定对象的数据(即Java和C++中的隐式
this
指针)进行操作的机制。必须在函数的参数列表中指定对象:

struct Rect {
  int width;
  int height;
};

int area( struct Rect r )       // struct keyword is necessary
{
  return r.width * r.height;
}

int main( void )
{
  struct Rect foo;              // struct keyword is necessary
  int a = area( foo );
  ...
}
在C语言中,
struct
标记名本身并不是自动的类型名-您必须使用
struct Rect
来声明该类型的对象。如果希望
Rect
本身就是一个类型名,则必须使用
typedef
功能:

typedef struct Rect Rect;
现在,您可以使用
Rect
本身作为类型名称:

Rect foo;
此特定示例按值传递
struct Rect
对象-形式参数
r
是内存中与实际参数
foo
不同的对象,它接收
foo
内容的副本。这意味着对函数中的
r.width
r.height
所做的任何更改都不会反映在
foo

如果函数需要修改
foo
的内容,则需要向其传递指针:

void scale( struct Rect *r, int wscale, int hscale )
{
  r->width *= wscale;   // r->width equivalent to (*r).width
  r->heignt *= hscale;  // '->' implicitly dereferences pointer
}                       // before accessing member

int main( void )
{
  struct Rect foo = {10, 10}; // initialize width and height to 10
  ...
  scale( &foo, 2, 3 );
  ...
}
人们通常会通过指针传递
struct
类型,因为创建大型
struct
类型的副本可能会很昂贵。如果传递指向不需要修改对象的函数的指针,请确保将其限定为
const

int area ( const struct Rect *r ) { return r->width * r->height; }
这样,如果您不小心试图写入,编译器可能会对您大喊大叫
宽度
高度

您知道C不是一种OOP语言,是吗?Java中的方法有一个隐藏的、隐式的
this
参数。在C中,所有参数都是显式的,所以你也必须使参数显式。“我希望ReCT能够自己调用这个”——这意味着你需要一个类,即“C类”,这是C++之类的东西。但是,大学给我的书把java和C比作很多。所以我需要了解如何将这个类转换为C代码请不要将代码作为图片发布。调用方法的模式可以通过一些技巧捕获
#define call(o,m,a)o->m(o,a)
。瓦拉格斯也能玩这个把戏。@Jean BaptisteYunès:是的。这是一种方法……但我真的不认为总的来说,试图用
C
来模拟这类事情是一种过分的做法。谢谢你的评论。我不同意,你可以从指定OO中获益,并在C中实现它,因为你不能/不能访问一些面向对象语言编译器。@ Jean BaptisteYun:啊,那是真的,但是为什么不在这里使用C++呢?更加突出。我承认你说的话。
Rect foo;
void scale( struct Rect *r, int wscale, int hscale )
{
  r->width *= wscale;   // r->width equivalent to (*r).width
  r->heignt *= hscale;  // '->' implicitly dereferences pointer
}                       // before accessing member

int main( void )
{
  struct Rect foo = {10, 10}; // initialize width and height to 10
  ...
  scale( &foo, 2, 3 );
  ...
}
int area ( const struct Rect *r ) { return r->width * r->height; }