帮忙看一个简单的关于super的程序

ipotang 2003-09-14 04:04:57
import java.io.*;
public class Grep extends FilterInputStream
{String substring;
DataInputStream in;
public Grep(DataInputStream in,String substring)
{super(in);
this.in=in;
this.substring=substring;
}
public final String readLine( ) throws IOException
{String line;
do
{line=in.readLine( );
}
while(line!=null)&&(line.indexof(substring)==-1);
return line;
}
public void main(String [] args)
{if((args.length==0)||(args.length>2))
{System.out.println("error!");
System.exit(0);
}
try
{DataInputStream d=new DataInputStream(new FilterInputStream(args[1]));
Grep g=new Grep(d,args[0]);
String line;
for(;;)
{line=g.readLine( );
if(line==null)
break;
System.out.println(line);
}
g.close( );
}
catch(IOException e)
System.err.println(e);
}
}
super是当前对象的直接父类,那么super(in);this.in=in是什么功能。
小第刚学,请指教。

...全文
23 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jscsqb 2003-09-15
  • 打赏
  • 举报
回复
将当前对象的in成员变量设成父对象成员变量in的同值
feibaook 2003-09-14
  • 打赏
  • 举报
回复
你要区分清楚:super和this两个关键字。
super:意指其父对象,也就是当前类的超类。比如super(in);就是调用其父类的构造方法。
this:指的是当前对象。比如:this.in = in;就是说为当前类的in属性赋值。
例如:
class DemoSuper {
DemoSuper(String str) {
System.out.println(str);
}
}

class DemoChild extends DemoSuper {
private String str = new String();
DemoChild(String str){
super(str); //调用父类的构造方法。
this.str = str; //为当前对象的属性:str赋值。
}
}
dmhorse 2003-09-14
  • 打赏
  • 举报
回复
In c++

this.in=in => Grep::in = in;
dmhorse 2003-09-14
  • 打赏
  • 举报
回复
super(in) call FilterInputStream constructor

this.in=in
initialize the member Grep =>DataInputStream in;


62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧