Topic: l类与类之间的关系问题!

  Print this page

1.l类与类之间的关系问题! Copy to clipboard
Posted by: april518
Posted on: 2005-03-09 17:02

代码如下:

public class CubbyHole {
private int contents;
private boolean available = false;

public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) { }
}
available = false;
notifyAll();
return contents;
}

public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) { }
}
contents = value;
available = true;
notifyAll();
}
}




public class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number;

public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}

public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = cubbyhole.get();
System.out.println("Consumer #" + this.number
+ " got: " + value);
}
}
}




public class Producer extends Thread {
private CubbyHole cubbyhole;
private int number;

public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}

public void run() {
for (int i = 0; i < 10; i++) {
cubbyhole.put(i);
System.out.println("Producer #" + this.number
+ " put: " + i);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}




public class Producer extends Thread {
private CubbyHole cubbyhole;
private int number;

public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}

public void run() {
for (int i = 0; i < 10; i++) {
cubbyhole.put(i);
System.out.println("Producer #" + this.number
+ " put: " + i);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}




public class ProducerConsumerTest {
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);

p1.start();
c1.start();
}
}



问:这几个类之间既没有继承关系也没有明显的接口,那么他们是如何调用彼此的方法呢?特别是cubbyhole与 consumer,producer 之间的关系,小弟有一点不明白! 难道放到一个PROJECT里面就可以完成调用了?突然想到的问题,百思不得其解.望各位大哥示下!在此谢过

2.Re:l类与类之间的关系问题! [Re: april518] Copy to clipboard
Posted by: jfs771
Posted on: 2005-05-18 10:49

public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}

public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}

看看构造函数里的这个CubbyHole c参数,应该就是将实例通过参数的形式通知
另一个类实例,本人也是新学的JAVA

3.Re:l类与类之间的关系问题! [Re: april518] Copy to clipboard
Posted by: leon0122
Posted on: 2005-05-19 17:08

以上的程序都是传递对象。
比如说,在类A中,有一个类B的对象,那么,在类A中可以通过类B的对象来调用
类B的方法,去实现你想要的操作,上面的程序就是这个原理

4.Re:l类与类之间的关系问题! [Re: april518] Copy to clipboard
Posted by: 2352439
Posted on: 2005-05-19 19:03

class A
{
String name="guofeng";
public void getInfo()
{
System.out.println(name);
}
}
class B
{
public static void main(String[] args)
{
A a = new A();
a.getInfo();
}
}

See this procedure, you are a little bit understand?


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923