t**********s 发帖数: 930 | 1 菜鸟问题:
在定义一个类时为什么可以其中的变量就可以定义成这个类呢? The definition of
the class has not finished yet, hasn't it?
Like this:
// PART OF THE MACHINE SIMULATION. DO NOT CHANGE.
package nachos.machine;
/**
* An OS kernel.
*/
public abstract class Kernel {
/** Globally accessible reference to the kernel. */
public static Kernel kernel = null;
My question is:
The class definition of Kennel should only be used elsewhere outside this
defintion, shouldn't it?
Why kernel is of type Kernel?
Thanks! |
m******t 发帖数: 2416 | 2
Because Java is way superior to C++, yeah baby!
Seriously though, I don't see why we _shouldn't_ be able to do that.
All the compiler needs to know at that point is the name of the class,
not the entire definition.
【在 t**********s 的大作中提到】 : 菜鸟问题: : 在定义一个类时为什么可以其中的变量就可以定义成这个类呢? The definition of : the class has not finished yet, hasn't it? : Like this: : // PART OF THE MACHINE SIMULATION. DO NOT CHANGE. : package nachos.machine; : /** : * An OS kernel. : */ : public abstract class Kernel {
|
t**********s 发帖数: 930 | 3 还是不明白啊。
这不是用自己定义自己吗?
一般Java类的定义里出现的变量都定义成其他除这个类之外的类型。
比如我们可以说蛇就是象绳子样的东西。我们不说蛇象蛇,这等于白说。
我的Java水平有限,请高人指点。
我现在正上Operating System这门课,其中的Nachos project 这种现象出现好几次.
public abstract class Kernel {
/** Globally accessible reference to the kernel. */
public static Kernel kernel = null;
/**
* Allocate a new kernel.
*/
public Kernel() {
// make sure only one kernel is created
Lib.assertTrue(kernel == null);
kernel = this;
}
public abstract void initialize(St
【在 m******t 的大作中提到】 : : Because Java is way superior to C++, yeah baby! : Seriously though, I don't see why we _shouldn't_ be able to do that. : All the compiler needs to know at that point is the name of the class, : not the entire definition.
|
g*****g 发帖数: 34805 | 4 Why is this even a question? If this is not allowed, how do you
define a tree? A node in the tree will always look like
class Node {
Node left;
Node right;
}
Java variables are by references, for a compiler, you allocate
a node object, with 2 pointers on the heap, and you are done,
it doesn't do deep allocation and I don't see why there should be
a problem.
【在 t**********s 的大作中提到】 : 还是不明白啊。 : 这不是用自己定义自己吗? : 一般Java类的定义里出现的变量都定义成其他除这个类之外的类型。 : 比如我们可以说蛇就是象绳子样的东西。我们不说蛇象蛇,这等于白说。 : 我的Java水平有限,请高人指点。 : 我现在正上Operating System这门课,其中的Nachos project 这种现象出现好几次. : public abstract class Kernel { : /** Globally accessible reference to the kernel. */ : public static Kernel kernel = null; : /**
|
A**o 发帖数: 1550 | 5 you can also declare a field variable (class member) after you reference it.
class A {
void b() {
c = 5;
...
}
int c;
}
Although it's hard to read. it's valid. |
t**********s 发帖数: 930 | 6 这个还是好理解的, 毕竟c被定义成了整数型.
但如果c被定义成A类 (public static A c),我就不能理解了.
class A {
void b() {
c = null;
...
}
A c;
it. |
A**o 发帖数: 1550 | 7 Just take class definition and variable declaration as 2 separate stuffs.
Then, what's the problem? the former is describing how the class should be
made. the later is just a pointer in the memory to an instance or nowhere. |
P*****f 发帖数: 2272 | 8 人家都给你说乐,这种变量申明方式在java里面是refernece(想象成指针).
不象c/c++里是代表实例。
【在 t**********s 的大作中提到】 : 这个还是好理解的, 毕竟c被定义成了整数型. : 但如果c被定义成A类 (public static A c),我就不能理解了. : class A { : void b() { : c = null; : ... : } : A c; : it.
|