B是实现A接口的类,new B() 构造了一个B的实例。
我感觉应该在其他地方调用时应该:
...String str a="";

解决方案 »

  1.   

    //上面操作失误。sorry
    ...String stra="";
       B clsB=new B();
    stra=clsB.play("zenmele");
    //有代码可以贴上来吗?
      

  2.   

    public interface A {
      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();
    这不可能啊!
      

  3.   

    回:ayayanvren(狂狮) 
    你没理解我的意思 boolean blnA=new AB(); 这当然是不可能的!
    我问的重点并不是实例化的问题~不实例化当然不行!我是想知道在实例化了B之后,
      B bb=new B();为什么不用写bb.play(参数)而play就能自动执行,而却此时也就更谈不上为play方法中的参数赋值了?
     
      

  4.   

    没有调用怎么可能执行?嗬嗬,代码中当然会用调用play的地方,你可以看看B的构造器里面是否调用了play方法
      

  5.   

    那你看看这个play(..)方法是在哪调用的啊,按照楼主的意思,好象应该是在B类的构造方法中调用的吧?
      

  6.   

    回:sundaydreamer(盲盲)  yaray(雅睿,生活在别处) 
        在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>");
                }
            }
        }
    }
      

  7.   

    回:noratong(诺拉) 
       你的事件接口的解释让我明白了一点,既然是系统调用并有系统传递给方法需要的参数,那么这个系统是如何知道他所需要传递的参数的具体值的呢?就好像上面的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。那么系统是如何知道需要传递的值的呢?
      

  8.   

    还有一点~这个RemoveHTMLReader类实现了两个read方法,其中第2个read()并没有带任何参数而在这个read()方法中又调用了第一个read(char buf[],int from,int 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???
      

  9.   

    对不起,刚才我没仔细看你的代码,你的代码跟我所说的事件的原理还不是一回事,但事件的原理正如我所说的那样,只要你用过事件,那么应该很清楚我所说的。但现在就你所贴出的代码来看,RemoveHTMLReader类的read的方法并不是没有被调用,它是通过其它方法来调用的,具体在那个方法里调用,我还不是很清楚,要文档。因为它一个readLine方法可能调用了几层关系。至于你说的没有给read方法传入from和len参数,那是因为没有调用那个read方法,而是调用的无参的read方法,你应该看见这个类有两个read方法吧。所以系统也无需知道传递的值。
    这些好像都是些基本原理,你应该多看一下书。有什么不对,才讨论。
      

  10.   

    回:noratong(诺拉) 
       关于这些基本原理的应该看看那本书?我看了一些介绍基本原理的都没有讲到我所需要的内容~
    怎么使用文档搞清楚这些调用关系?麻烦介绍一下!!
      

  11.   

    由于你的实例化类是RemoveHTMLReader
    他是继承FilterReader这个父类的,对于你的迷惑我只能推测出
    read函数是作为FilterReader的接口实现
    而在进行 super(in)构造时,read函数被调用
      

  12.   

    我个人猜想也就是
    interface a {
      //构造
      a() {
      }
      a( String str) {
         play( str );
       }public boolean play(String i);
          
    }
      

  13.   

    B只是接口A一中实现而已,在第三者调用play(String)方法的时候不想知道play(String)里面具体怎么处理字符串,也有可能不知道。所以就定义了一个接口A, 具体A由谁去实现,去怎么实现他并不关心,比如说B的实现是想把字符全部转换成大写,而另外有了一个实现接口A的C, 而它的play(String)方法是想把所有的字符转换成小写。因此接口很灵活,以前写的代码不用改动,只要直接实现这个接口就行了,所以接口的功能就是这个作用。明白否?
      

  14.   

    回:在构造函数中调用方法我想是不成立的!
    noratong(诺拉):针对RemoveHTMLReader程序段的解释的调用关系有点复杂~要是正在写代码的时候如何能划分出这个调用关系?
    有点领悟~知其然不知其所以然~痛苦咧!