B是实现A接口的类,new B() 构造了一个B的实例。
我感觉应该在其他地方调用时应该:
...String str a="";
我感觉应该在其他地方调用时应该:
...String str a="";
解决方案 »
- 如何在单个窗口的范围内监听所有键盘事件
- 如何在JFrame标题栏中添加按钮和改变最大化按钮样式
- 在Java中实现在Oracle中创建存储过程的功能,就是运行java程序,就能在Oracle中创建存储过程、
- 一个小程序,想用线程实现在jpanel上搞出一行button,但是我的程序做出来后button不会动,程序改了三小时了,未果,请高指点江山:代码如下
- 这么简单的程序出的是什么问题呀!
- 请java高手指点我一下
- 请教一个小问题:如何实现 copy功能组件 未选中所要复制对象时的冻结状态?
- 这是什么意思?大家看看
- java applet 不能運行?
- 怎么问题都是给20分呢?
- ~请问各位大虾这本书的出版社是??有分!!!!!
- 知道哪里有卖 <csdn开发高手> 第二期的么?
...String stra="";
B clsB=new B();
stra=clsB.play("zenmele");
//有代码可以贴上来吗?
public boolean play(String i);
}
/////////////////////
public class B implements interA{ public B() {
}
public boolean play(String i){
if (i.length()>2)
return true;
else
return false;
}
}
////////////////////
public class clsA {
public clsA() {
}
public static void main(String[] args){
String strA="asdlfkafs";
AB Abinst=new AB();
boolean blnA=Abinst.play(strA);
if(blnA==true)
System.out.println("OK!");
}
}
//搂住对不起,回了两个废帖子
我看了几遍你的问题,我把其他类中的调用形式写上来了,你说的不是这种情况吗?我理解你的意思是说
boolean blnA=new AB();
这不可能啊!
你没理解我的意思 boolean blnA=new AB(); 这当然是不可能的!
我问的重点并不是实例化的问题~不实例化当然不行!我是想知道在实例化了B之后,
B bb=new B();为什么不用写bb.play(参数)而play就能自动执行,而却此时也就更谈不上为play方法中的参数赋值了?
在B类的构造函数中并没有调用play(参数)!!!
我贴一段程序,大家看看~~~~~~~
这个程序是应用FilterReader接口从一个文件中过滤出HTML标签,也就是过滤文件中<...> 尖括号之间的内容。
import java.io.*;/**
* A simple FilterReader that strips HTML tags (or anything between
* pairs of angle brackets) out of a stream of characters.
**/
public class RemoveHTMLReader extends FilterReader {
/** A trivial constructor. Just initialize our superclass */
public RemoveHTMLReader(Reader in) { super(in); }
boolean intag = false; // Used to remember whether we are "inside" a tag /**
* This is the implementation of the no-op read() method of FilterReader.
* It calls in.read() to get a buffer full of characters, then strips
* out the HTML tags. (in is a protected field of the superclass).
**/
public int read(char[] buf, int from, int len) throws IOException {
int numchars = 0; // how many characters have been read
// Loop, because we might read a bunch of characters, then strip them
// all out, leaving us with zero characters to return.
while (numchars == 0) {
numchars = in.read(buf, from, len); // Read characters
if (numchars == -1) return -1; // Check for EOF and handle it. // Loop through the characters we read, stripping out HTML tags.
// Characters not in tags are copied over previous tags
int last = from; // Index of last non-HTML char
for(int i = from; i < from + numchars; i++) {
if (!intag) { // If not in an HTML tag
if (buf[i] == '<') intag = true; // check for tag start
else buf[last++] = buf[i]; // and copy the character
}
else if (buf[i] == '>') intag = false; // check for end of tag
}
numchars = last - from; // Figure out how many characters remain
} // And if it is more than zero characters
return numchars; // Then return that number.
}
/**
* This is another no-op read() method we have to implement. We
* implement it in terms of the method above. Our superclass implements
* the remaining read() methods in terms of these two.
**/
public int read() throws IOException {
char[] buf = new char[1];
int result = read(buf, 0, 1);
if (result == -1) return -1;
else return (int)buf[0];
}
/** This class defines a main() method to test the RemoveHTMLReader */
public static class Test {
/** The test program: read a text file, strip HTML, print to console */
public static void main(String[] args) {
try {
if (args.length != 1)
throw new IllegalArgumentException("Wrong number of args");
// Create a stream to read from the file and strip tags from it
BufferedReader in = new BufferedReader(
new RemoveHTMLReader(new FileReader(args[0])));
// Read line by line, printing lines to the console
String line;
while((line = in.readLine()) != null)
System.out.println(line);
in.close(); // Close the stream.
}
catch(Exception e) {
System.err.println(e);
System.err.println("Usage: java RemoveHTMLReader$Test" +
" <filename>");
}
}
}
}
你的事件接口的解释让我明白了一点,既然是系统调用并有系统传递给方法需要的参数,那么这个系统是如何知道他所需要传递的参数的具体值的呢?就好像上面的read()方法,个人理解第一个read(char[] buf,int from,int len)方法在这段代码中from应该表示的是文件的起始点,而len应该表示的是从起始点读取的长度,在main函数中大家看到对实现了过滤功能的RmoveHTMLReader类的实例化是包含在 BufferedReader in = new BufferedReader(
new RemoveHTMLReader(new FileReader(args[0])));
这个语句里面的,后面并没有显示的调用方法read(.....)方法,那么在系统隐式调用read(...)方法的同时系统还应该传递给这个方法需要的参数from,len。那么系统是如何知道需要传递的值的呢?
public int read() throws IOException {
char[] buf = new char[1];
int result = read(buf, 0, 1);
if (result == -1) return -1;
else return (int)buf[0];
}
这第2个read()方法也许是个突破口,上面的那一大段程序要完成从头到尾过滤的功能就得从0(from)开始过滤,过滤的长度就应该是整个文件的长度(len)。但我还是不知道系统是如何知道要传递给参数的具体值的??在这个例子中也就是说系统如何知道该把0和要过滤的长度传递给from 和len???
这些好像都是些基本原理,你应该多看一下书。有什么不对,才讨论。
关于这些基本原理的应该看看那本书?我看了一些介绍基本原理的都没有讲到我所需要的内容~
怎么使用文档搞清楚这些调用关系?麻烦介绍一下!!
他是继承FilterReader这个父类的,对于你的迷惑我只能推测出
read函数是作为FilterReader的接口实现
而在进行 super(in)构造时,read函数被调用
interface a {
//构造
a() {
}
a( String str) {
play( str );
}public boolean play(String i);
}
noratong(诺拉):针对RemoveHTMLReader程序段的解释的调用关系有点复杂~要是正在写代码的时候如何能划分出这个调用关系?
有点领悟~知其然不知其所以然~痛苦咧!