由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 尚未完成定义的类怎么可以作变量类型?
相关主题
help for running CPU intensive program!!String[] a = c.toArray(new String[0])
structure in Java??怎么返回一个变量的地址?
帮我了解一下64bit JVMTIJ上写错了?
Red Black Tree code in java关于java执行SQL之后的内存问题?
Java basic concept(4)[提问] The JVM could not be started
Where I can find comparison of JVMsJava是如何处理ArrayList和LinkedList的内存的?
an Array questionjava小弱请教 java -Xmx40960m
How would clear a BufferedImage?what is really hard is testing
相关话题的讨论汇总
话题: kernel话题: class话题: node话题: public话题: why
进入Java版参与讨论
1 (共1页)
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.

1 (共1页)
进入Java版参与讨论
相关主题
what is really hard is testingJava basic concept(4)
尚未完成定义的类怎么可以作变量类型? (转载)Where I can find comparison of JVMs
用stl的程序一般比不用stl library的size大多少?an Array question
C/C++ QuestionsHow would clear a BufferedImage?
help for running CPU intensive program!!String[] a = c.toArray(new String[0])
structure in Java??怎么返回一个变量的地址?
帮我了解一下64bit JVMTIJ上写错了?
Red Black Tree code in java关于java执行SQL之后的内存问题?
相关话题的讨论汇总
话题: kernel话题: class话题: node话题: public话题: why