C结构和Java类之间有什么区别?
我是Java新手,但对C有点熟悉。我想知道——C结构和Java对象以及调用它们的方法之间有什么区别?还是完全等同 例如,自行车结构:C结构和Java类之间有什么区别?,java,c,Java,C,我是Java新手,但对C有点熟悉。我想知道——C结构和Java对象以及调用它们的方法之间有什么区别?还是完全等同 例如,自行车结构: class BicycleDemo { public static void main(String[] args) { // Create two different Bicycle objects Bicycle bike1 = new Bicycle(); Bicycle bike2 =
class BicycleDemo {
public static void main(String[] args) {
// Create two different Bicycle objects
Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();
// Invoke methods on those objects
bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}
我问这个问题的原因是因为它们看起来非常相似!谢谢
C
struct中不能包含方法/函数。它只是不同数据类型的集合<代码>类可以声明变量和方法。关键区别在于类允许
这是OOP的两个非常重要的特性 如果不考虑方法重写,那么可以将Java类和方法看作是一对C风格的
struct
和一组在这些struct
上操作的函数。例如,如果您有这样一个类:
public class MyJavaClass {
private int x;
public int getX() {
return x;
}
public int setX(int value) {
x = value;
}
}
struct MyJavaClass_Vtable {
void (*getX)(struct MyJavaClass* this);
void (*setX)(struct MyJavaClass* this, int value);
};
struct MyJavaClass {
struct MyJavaClass_Vtable* vtable;
int x;
};
int MyJavaClass_getX(struct MyJavaClass* this) {
return this->x;
}
void MyJavaClass_setX(struct MyJavaClass* this, int value) {
this->x = value;
}
/* A global instance of the vtable for MyJavaClass */
struct MyJavaClass_Vtable MyJavaClassVtableInstance = {
&MyJavaClass_getX,
&MyJavaClass_setX
};
struct MyJavaClass* mjc = malloc(sizeof *mjc);
mjc->vtable = &MyJavaClassVtableInstance;
这类似于为此编写C代码:
struct MyJavaClass {
int x;
};
int MyJavaClass_getX(struct MyJavaClass* this) {
return this->x;
}
void MyJavaClass_setX(struct MyJavaClass* this, int value) {
this->x = value;
}
其主要思想是,方法类似于将接收方对象作为隐式“this”参数的函数。在C语言中,必须显式地将接收器作为参数传递给函数,而在Java中,这是通过object.method()
语法隐式传递的
如果开始引入方法重写,这会变得有点复杂,因为对对象调用的方法取决于对象的动态类型,而不是静态类型。模拟这种情况的一种方法是使用称为vtable或virtualfunction表的东西,之所以这样命名是因为C++的virtual
关键字。其思想是,每个对象都存储一个指向函数指针表的指针,每个函数一个指针可以被重写,当在对象上调用方法时,从表中选择并调用相应的函数指针。因此,更恰当地说,上面的Java对象可能看起来像这样:
public class MyJavaClass {
private int x;
public int getX() {
return x;
}
public int setX(int value) {
x = value;
}
}
struct MyJavaClass_Vtable {
void (*getX)(struct MyJavaClass* this);
void (*setX)(struct MyJavaClass* this, int value);
};
struct MyJavaClass {
struct MyJavaClass_Vtable* vtable;
int x;
};
int MyJavaClass_getX(struct MyJavaClass* this) {
return this->x;
}
void MyJavaClass_setX(struct MyJavaClass* this, int value) {
this->x = value;
}
/* A global instance of the vtable for MyJavaClass */
struct MyJavaClass_Vtable MyJavaClassVtableInstance = {
&MyJavaClass_getX,
&MyJavaClass_setX
};
struct MyJavaClass* mjc = malloc(sizeof *mjc);
mjc->vtable = &MyJavaClassVtableInstance;
每当您创建MyJavaClass
的实例时,您都会设置其vtable,如下所示:
public class MyJavaClass {
private int x;
public int getX() {
return x;
}
public int setX(int value) {
x = value;
}
}
struct MyJavaClass_Vtable {
void (*getX)(struct MyJavaClass* this);
void (*setX)(struct MyJavaClass* this, int value);
};
struct MyJavaClass {
struct MyJavaClass_Vtable* vtable;
int x;
};
int MyJavaClass_getX(struct MyJavaClass* this) {
return this->x;
}
void MyJavaClass_setX(struct MyJavaClass* this, int value) {
this->x = value;
}
/* A global instance of the vtable for MyJavaClass */
struct MyJavaClass_Vtable MyJavaClassVtableInstance = {
&MyJavaClass_getX,
&MyJavaClass_setX
};
struct MyJavaClass* mjc = malloc(sizeof *mjc);
mjc->vtable = &MyJavaClassVtableInstance;
然后,在调用这样的函数时(在Java中):
在C中,它看起来像
myJavaClass->vtable->getX(myJavaClass);
因此,从某种意义上讲,Java类只是一个具有一些额外元信息的结构。当然,对于程序员来说,它看起来完全不同——有封装、多态性、更严格的类型系统等等——但在本机代码级别,常规C结构和Java类可能看起来非常相似
希望这有帮助 Java类允许您定义字段,如C结构,但具有以下附加功能:
- 正如您在问题中所述,您可以向可以对数据进行操作的类添加方法
- 您可以声明字段和方法
(以及一些其他访问设置),这意味着只有类的其他方法可以访问它们,而不是从外部访问代码。(在结构上使用此选项没有意义,因为无法访问私有字段——只有在允许方法的情况下才有意义)private
- 类可以从其他类继承。除此之外,只要B从a继承,就可以在预期a类型的位置传递B类型的对象
在C语言中,经常使用指向结构的指针来模拟Java的引用行为。在Java中,您不使用指针,因为类对象已经是引用。而且一个结构不能从另一个结构继承。@Chris-这一点很好<代码>C没有继承的概念。你应该把它作为答案发布。你忘记了最重要的一个:继承()