C 结构和 Java 类之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5413001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between C structures and Java classes?
提问by O_O
I'm a newbie to Java, but somewhat familiar to C. I wanted to know -- what differences are there between C structures and Java objects and invoking their methods? Or are the totally equivalent?
我是 Java 的新手,但对 C 有点熟悉。我想知道 - C 结构和 Java 对象以及调用它们的方法之间有什么区别?或者是完全等价的?
For example, the Bicycle structure:
例如,自行车结构:
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();
}
}
The reason why I am asking is because they look so similar! Thanks!
我问的原因是因为它们看起来很相似!谢谢!
回答by templatetypedef
If you leave method overriding out of the picture, then you can think of Java classes and methods as a pair of a C-style struct
and a set of functions that operate on those struct
s. For example, if you have a class like this:
如果不考虑方法覆盖,那么您可以将 Java 类和方法视为一对 C 样式struct
和一组对这些struct
s进行操作的函数。例如,如果你有一个这样的类:
public class MyJavaClass {
private int x;
public int getX() {
return x;
}
public int setX(int value) {
x = value;
}
}
This would be similar to writing C code to this effect:
这类似于为此效果编写 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;
}
The main idea is that a method is similar to a function that takes the receiver object as an implicit "this" parameter. In C, you have to explicitly pass the receiver as a parameter to the function, while in Java this is done implicitly through the object.method()
syntax.
主要思想是,方法类似于将接收者对象作为隐式“this”参数的函数。在 C 中,您必须将接收者作为参数显式传递给函数,而在 Java 中,这是通过object.method()
语法隐式完成的。
If you start introducing method overriding, this becomes a bit more complicated because the method that you invoke on an object depends on the dynamictype of the object, not the statictype. One way of simulating this is using something called a vtableor virtual function table,so named because of C++'s virtual
keyword. The idea is that each object stores a pointer to a table of function pointers, one per function that can be overridden, and when a method is called on the object the appropriate function pointer is selected out of the table and called. So, more properly, the above Java object might look something like this:
如果您开始引入方法覆盖,这会变得有点复杂,因为您在对象上调用的方法取决于对象的动态类型,而不是静态类型。模拟这一点的一种方法是使用称为vtable或虚函数表的东西,之所以如此命名是因为 C++ 的virtual
关键字。这个想法是每个对象存储一个指向函数指针表的指针,每个函数一个可以被覆盖,当在对象上调用一个方法时,从表中选择适当的函数指针并调用。因此,更准确地说,上面的 Java 对象可能如下所示:
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
};
Whenever you created an instance of MyJavaClass
, you'd set up its vtable doing something like this:
每当您创建 的实例时MyJavaClass
,您都会设置其 vtable 执行以下操作:
struct MyJavaClass* mjc = malloc(sizeof *mjc);
mjc->vtable = &MyJavaClassVtableInstance;
Then, when invoking a function like this (in Java):
然后,在调用这样的函数时(在 Java 中):
myJavaClass.getX();
In C it would look like
在 C 中它看起来像
myJavaClass->vtable->getX(myJavaClass);
So in a sense a Java class is just a struct with some extra metainformation. Of course, to the programmer it looks totally different - there's encapsulation, polymorphism, a stricter type system, etc. - but at the level of native code a regular C struct and a Java class probably look very similar.
所以从某种意义上说,Java 类只是一个带有一些额外元信息的结构。当然,对于程序员来说,它看起来完全不同——有封装、多态、更严格的类型系统等——但在本机代码级别,常规 C 结构和 Java 类可能看起来非常相似。
Hope this helps!
希望这可以帮助!
回答by Mahesh
C
struct cannot have methods/functions in it. It is only a collection of different data types. class
can have both variables and methods declared.
C
struct 中不能包含方法/函数。它只是不同数据类型的集合。class
可以同时声明变量和方法。
回答by mgiuca
A Java class lets you define fields, like a C struct, but has the following additional features:
Java 类允许您定义字段,如 C 结构,但具有以下附加功能:
- As you state in your question, you can add methods to a class which can operate on the data.
- You can declare both fields and methods
private
(and a few other access settings), which means only other methods of the class can access them, not code from outside. (It wouldn't make sense to have this on structs, since there would be no way to access private fields -- this only makes sense when you allow methods). - Classes can inherit from other classes. Among other things, this lets you pass an object of type B where a type A was expected, as long as B inherits from A.
- 正如您在问题中所述,您可以向可以操作数据的类添加方法。
- 您可以同时声明字段和方法
private
(以及一些其他访问设置),这意味着只有类的其他方法可以访问它们,而不能从外部访问代码。(在结构上使用它是没有意义的,因为将无法访问私有字段——这仅在您允许方法时才有意义)。 - 类可以从其他类继承。除其他外,只要 B 继承自 A,这允许您在预期类型 A 的地方传递类型 B 的对象。
There is another subtle difference. In C, structs are value typesbut in Java, classes are reference types. That means when you copy a struct in C (such as assigning to a variable or passing as an argument), it gets all of its fields copied, whereas in Java, when you copy a class object, it just copies the referenceto the object (so if you modify its fields, you modify the original object as well, not just the copy).
还有一个微妙的区别。在 C 中,结构是值类型,但在 Java 中,类是引用类型。这意味着当您在 C 中复制结构时(例如分配给变量或作为参数传递),它会复制其所有字段,而在 Java 中,当您复制类对象时,它只会复制对对象的引用(所以如果你修改它的字段,你也会修改原始对象,而不仅仅是副本)。
In C, you often use pointers tostructs to emulate the reference behaviour of Java. In Java, you don't use pointers because class objects are already references.
在 C 中,您经常使用指向结构的指针来模拟 Java 的引用行为。在 Java 中,您不使用指针,因为类对象已经是引用。
回答by JB Nizet
The key differences, IMHO are that classes allow
恕我直言,主要区别在于课程允许
These are two very important features of OOP.
这是 OOP 的两个非常重要的特性。