Topic: 为什么编译通不过?

  Print this page

1.为什么编译通不过? Copy to clipboard
Posted by: Butterfly_125
Posted on: 2005-07-21 02:28

import java.io.*;
public class PipeStreamTest
{
  public static void main(String args[])
  {
    try
    {
      Thread e1 = new Sender();
      Thread e2 = new Receiver();
      PipedOutputStream out = e1.getOutputStream();
      PipedInputStream in = e2.getInputStream();
      out.connect(in);
      e1.start();
      e2.start();
    }
    catch(IOException e)
    {
      System.out.println(e.getMessage());
    }
  }
}

class Sender extends Thread
{
  private PipedOutputStream out = new PipedOutputStream();
  public PipedOutputStream getOutputStream()  //PipedOutputStream为返回类型
  {
    return out;
  }
  public void run()
  {
    String s = new String("My name is YuanPing");
    try
    {
      out.write(s.getBytes());
      out.close();
    }
    catch(IOException e)
    {
      System.out.println(e.getMessage());
    }
  }
}

class Receiver extends Thread
{
  private PipedInputStream in = new PipedInputStream();
  public PipedInputStream getInputStream()
  {
    return in;
  }
  public void run()
  {
    String s = null;
    byte bt[] = new byte[1024];
    int len;
    try
    {
      len = in.read(bt);
      s = new String(bt,0,len);
      System.out.println("The following message comes from Sender:\n" + s);
      in.close();
    }
    catch(IOException e)
    {
      System.out.println(e.getMessage());
    }
  }
}

编译报的错:
D:\PipeStreamTest.java:10: 找不到符号
符号: 方法 getOutputStream()
位置: 类 java.lang.Thread
PipedOutputStream out = e1.getOutputStream();
^
D:\PipeStreamTest.java:11: 找不到符号
符号: 方法 getInputStream()
位置: 类 java.lang.Thread
PipedInputStream in = e2.getInputStream();
^
2 错误

getOutputStream()getInputStream()明明在Sender和Receiver中自己定义了啊!
为什么还这报错?高手们,帮帮忙吧!Sad

2.Re:为什么编译通不过? [Re: Butterfly_125] Copy to clipboard
Posted by: ranchgirl
Posted on: 2005-07-21 06:01

I am going to provide you two ways to make it work, one is better, the other is not very good in your case, but very useful in many other cases. The second way is in comments.

However, the other way will also help you to understand some important Java/OO concepts. Do read them, try hard to make yourself understood.


import java.io.*;
public class PipeStreamTest
{
public static void main(String args[])
{
try
{
// Best use the original classes here
// instead of unnecessary using the super class
Sender e1 = new Sender();
Receiver e2 = new Receiver();

// Now, compiler knows the real types of e1 and e2
PipedOutputStream out = e1.getOutputStream();
PipedInputStream in = e2.getInputStream();

/*
// Using super class to initial subclasses is perfect legal here
Thread e1 = new Sender();
Thread e2 = new Receiver();

// However, super class don't have getOutputStream() or getInputStream() methods
// explicit downcasts are required !!!
// Runtime will know it (dynamic binding), but now, it is compile time!!!
PipedOutputStream out = ((Sender)e1).getOutputStream();
PipedInputStream in = ((Receiver)e2).getInputStream();
*/
out.connect(in);
e1.start();
e2.start();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}

class Sender extends Thread
{
private PipedOutputStream out = new PipedOutputStream();
public PipedOutputStream getOutputStream() //PipedOutputStream?????
{
return out;
}
public void run()
{
String s = new String("My name is YuanPing");
try
{
out.write(s.getBytes());
out.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}

class Receiver extends Thread
{
private PipedInputStream in = new PipedInputStream();
public PipedInputStream getInputStream()
{
return in;
}
public void run()
{
String s = null;
byte bt[] = new byte[1024];
int len;
try
{
len = in.read(bt);
s = new String(bt,0,len);
System.out.println("The following message comes from Sender:\n" + s);
in.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}


Read my comments in the code carefully, and you will actually learn a lot of Java and concepts of OO programming after you understand them.

Happy learning and good luck!

3.Re:为什么编译通不过? [Re: Butterfly_125] Copy to clipboard
Posted by: Butterfly_125
Posted on: 2005-07-21 14:21

哈哈,可能是我英文不太好的原因吧,我把comments and corrected codes read for N times!But,I am really understand your ideas at last.
Your answers very useful and particular every times!!

What a good man you are!
Thank you my really friends!


   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