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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zuoyou 中级黑马   /  2015-6-7 17:30  /  589 人查看  /  12 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.io.*;
  2. class  TestMybuffered
  3. {
  4.         public static void main(String[] args) throws IOException
  5.         {
  6.                 MyBufferedReader mbr = new MyBufferedReader(new FileReader("Fileread.java"));
  7.                 String line=null;
  8.                 while((line=mbr.readLine())!=null)
  9.                         System.out.println(line);

  10.                 mbr.myclose();
  11.                 System.out.println("Hello World!");
  12.         }
  13. }

  14. class MyBufferedReader
  15. {
  16.         private Reader r;

  17.         MyBufferedReader(Reader r)
  18.         {
  19.                 this.r=r;
  20.         }

  21.         public String readLine() throws IOException
  22.         {
  23.                 StringBuilder sb= new StringBuilder();
  24.                 int ch=0;
  25.                 while((ch=this.r.read())!=-1)
  26.                 {
  27.                         if(ch=='\r')
  28.                                 continue;
  29.                         if(ch=='\n')
  30.                                 return sb.toString();
  31.                         else
  32.                                 sb.append((char)ch);
  33.                
  34.                 }
  35.                 if(sb.length()!=0)
  36.                         return sb.toString();
  37.                 return null;
  38.         }
  39.         public void myclose() throws IOException
  40.         {
  41.         this.r.close();
  42.         }
  43. }
复制代码


这是我自己写的一个MyBufferedReader类,在循环读取的时候,ch=this.r.read() 中this应不应该加啊?我看老师的视频中好像没有这个this!

12 个回复

倒序浏览
我会在变量名相同的情况下才加.
虽然加上也不会报错,
但是感觉有的加有的不加,整个代码很不好看
回复 使用道具 举报
这个问题比较复杂,问一下老师比较有可靠性,权威性!
回复 使用道具 举报
一个类中成员方法的局部变量跟成员变量同名,建议加上this
回复 使用道具 举报
不用加this
回复 使用道具 举报
加上比较好把,最起码看起来比较明朗.
回复 使用道具 举报
我又测试了一下发现加与不加,结果都一样啊!
  1. import java.io.*;
  2. class  TestMybuffered
  3. {
  4.         public static void main(String[] args) throws IOException
  5.         {
  6.                 MyBufferedReader mbr = new MyBufferedReader(new FileReader("Fileread.java"));
  7.                 MyBufferedReader mbr1 = new MyBufferedReader(new FileReader("a.txt"));
  8.                 String line=null;
  9.                 while((line=mbr.readLine())!=null)
  10.                         System.out.println(line);
  11.         System.out.println("第二个文件");
  12.                 mbr.myclose();

  13.                 String line1=null;
  14.                 while((line1=mbr1.readLine())!=null)
  15.                         System.out.println(line1);

  16.                 mbr1.myclose();
  17.                
  18.         }
  19. }

  20. class MyBufferedReader
  21. {
  22.         private Reader r;

  23.         MyBufferedReader(Reader r)
  24.         {
  25.                 this.r=r;
  26.         }

  27.         public String readLine() throws IOException
  28.         {
  29.                 StringBuilder sb= new StringBuilder();
  30.                 int ch=0;
  31.                 while((ch=r.read())!=-1)
  32.                 {
  33.                         if(ch=='\r')
  34.                                 continue;
  35.                         if(ch=='\n')
  36.                                 return sb.toString();
  37.                         else
  38.                                 sb.append((char)ch);
  39.                
  40.                 }
  41.                 if(sb.length()!=0)
  42.                         return sb.toString();
  43.                 return null;
  44.         }
  45.         public void myclose() throws IOException
  46.         {
  47.         this.r.close();
  48.         }
  49. }
复制代码
回复 使用道具 举报
明显不用。this一般是用于区分一个类里方法内和方法外有同名属性或者对象时才加。你的readLine方法内没有定义名字叫r的变量,所以调用r这个名字时JVM会自动查找到方法外成员变量区域。

如果你readLine方法内部定义了一个叫r的变量,那么不加this就会调用方法内这个变量,加了this就是调用方法外的那个成员变量。
回复 使用道具 举报
我想 this 是用来避免成员变量被局部变量覆盖的情况, 这里没有重名的局部变量啊
回复 使用道具 举报
在方法替中,局部数据会覆盖全局数据,如果没有局部数据,也就无所谓替换了,但是我一般情况是加的,一个是看上去好理解点,其次是,如果我修改方法的时候,不会再次去加,麻烦
回复 使用道具 举报
我写的话都加。 看起来比较容易懂。            
回复 使用道具 举报
我认为不用加
回复 使用道具 举报
不用加。因为你已经指明了是r的read方法
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马