A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Up↑Lee↗ 中级黑马   /  2014-4-2 17:14  /  963 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 Up↑Lee↗ 于 2014-4-2 20:50 编辑

有两段代码,有一段代码用到另外一段代码中的类。怎么办??我试着把代码 1 放在一个包中,然后在代码2中进行导包,为什么操作失败??
代码1:
  1. package pack1;  //自己加的
  2. import java.io.*;
  3. class MyBufferedReader extends Reader
  4. {
  5.         
  6.         private Reader r;
  7.         MyBufferedReader(Reader r)
  8.         {
  9.                 this.r = r;
  10.         }

  11.         //可以一次读一行数据的方法。
  12.         public String myReadLine()throws IOException
  13.         {
  14.                 //定义一个临时容器。原BufferReader封装的是字符数组。
  15.                 //为了演示方便。定义一个StringBuilder容器。因为最终还是要将数据变成字符串。
  16.                 StringBuilder sb = new StringBuilder();
  17.                 int ch = 0;
  18.                 while((ch=r.read())!=-1)
  19.                 {
  20.                         if(ch=='\r')
  21.                                 continue;
  22.                         if(ch=='\n')
  23.                                 return sb.toString();
  24.                         else
  25.                                 sb.append((char)ch);
  26.                 }

  27.                 if(sb.length()!=0)
  28.                         return sb.toString();
  29.                 return null;               
  30.         }

  31.         /*
  32.         覆盖Reader类中的抽象方法。

  33.         */
  34.         public int read(char[] cbuf, int off, int len) throws IOException
  35.         {
  36.                 return r.read(cbuf,off,len) ;
  37.         }

  38.         public void close()throws IOException
  39.         {
  40.                 r.close();
  41.         }
  42.         public void myClose()throws IOException
  43.         {
  44.                 r.close();
  45.         }
  46. }


  47. class  MyBufferedReaderDemo
  48. {
  49.         public static void main(String[] args) throws IOException
  50.         {
  51.                 FileReader fr = new FileReader("buf.txt");

  52.                 MyBufferedReader myBuf = new MyBufferedReader(fr);

  53.                 String line = null;

  54.                 while((line=myBuf.myReadLine())!=null)
  55.                 {
  56.                         System.out.println(line);
  57.                 }

  58.                 myBuf.myClose();
  59.         }
  60. }
复制代码
代码2:
  1. import pack1.*;   //自己加的
  2. import java.io.*;

  3. class MyLineNumberReader extends MyBufferedReader
  4. {
  5.         private int lineNumber;
  6.         MyLineNumberReader(Reader r)
  7.         {
  8.                 super(r);
  9.         }

  10.         public String myReadLine()throws IOException
  11.         {

  12.                 lineNumber++;
  13.                 return super.myReadLine();
  14.         }
  15.         public void setLineNumber(int lineNumber)
  16.         {
  17.                 this.lineNumber = lineNumber;
  18.         }
  19.         public int getLineNumber()
  20.         {
  21.                 return lineNumber;
  22.         }
  23. }

  24. /*
  25. class MyLineNumberReader
  26. {
  27.         private Reader r;
  28.         private int lineNumber;
  29.         MyLineNumberReader(Reader r)
  30.         {
  31.                 this.r = r;
  32.         }

  33.         public String myReadLine()throws IOException
  34.         {

  35.                 lineNumber++;
  36.                 StringBuilder sb = new StringBuilder();

  37.                 int ch = 0;

  38.                 while((ch=r.read())!=-1)
  39.                 {
  40.                         if(ch=='\r')
  41.                                 continue;
  42.                         if(ch=='\n')
  43.                                 return sb.toString();
  44.                         else
  45.                                 sb.append((char)ch);
  46.                 }
  47.                 if(sb.length()!=0)
  48.                         return sb.toString();
  49.                 return null;
  50.         }
  51.         public void setLineNumber(int lineNumber)
  52.         {
  53.                 this.lineNumber = lineNumber;
  54.         }
  55.         public int getLineNumber()
  56.         {
  57.                 return lineNumber;
  58.         }

  59.         public void myClose()throws IOException
  60.         {
  61.                 r.close();
  62.         }
  63. }
  64. */
  65. class  MyLineNumberReaderDemo
  66. {
  67.         public static void main(String[] args) throws IOException
  68.         {
  69.                 FileReader fr = new FileReader("BufferedWriterDemo.java");

  70.                 MyLineNumberReader mylnr = new MyLineNumberReader(fr);

  71.                 String line = null;
  72.                 mylnr.setLineNumber(100);
  73.                 while((line=mylnr.myReadLine())!=null)
  74.                 {
  75.                         System.out.println(mylnr.getLineNumber()+"::"+line);
  76.                 }

  77.                 mylnr.myClose();
  78.         }
  79. }
复制代码



评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 恭喜 25分

查看全部评分

3 个回复

倒序浏览
本帖最后由 李军辉 于 2014-4-2 17:35 编辑

把class MyLineNumberReader类定义为开放的public class MyLineNumberReader   你家门关着,我进不去,赶紧开门吧
回复 使用道具 举报
要在一个类中访问另外一个类中的变量要确保几点:
1.要在访问的类中导如被访问的类
2.(同包条件下)被访问的类的属性必须不能是private

举个例子
public class A{
   public int a=0;
   public static int c=0;//static类型数据

}

public class B{

  A a=new A();
  int b=a.a;//调用

  int d=A.b;//static类型数据调用可以不用实例

}

一般的类中的属性都是设置为private或者protected的,在设置各属性相对应的get set方法.而不是直接调用属性的(除非特殊情况).楼主慢慢领会吧

评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1

查看全部评分

回复 使用道具 举报
A类使用另外一个包中的B类,B类必须是public的

public class B

类(class)
可见性修饰符:
public—在所有类中可见,在其他包中可以用import导入。
缺省—就是没有修饰符,在同一个包中的类中可见,在其他包中不能用import导入。

评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马