具体类型你可以用getClass()打出来看看,事实上你也没有必要去知道具体类型是什么呵呵.

解决方案 »

  1.   

    看了一下jdk的代码,大概是这样
    在URL()中有一
    URLStreamHandler handler对象;
    在URL构造时 根据条件为handler 附值 根据文档---
    for example, for HTTP an
    HttpURLConnection will be returned, and for JAR a
    JarURLConnection will be returned.
    handler 可以是HttpURLConnection 或者JarURLConnection 
    所以这个时候URLConnection yc=HttpURLConnection 抽象类可以是子类的引用
    不过 yc.getInputStrea()这一句反倒没看懂
      

  2.   

    这是一个abstract factory 模式,看一下design patterns,上面说的很清楚
      

  3.   

    1.看不懂你说的什么。
    他随便这样写一下不就有了一个子类
    return new sun.net.www.protocol.http.HttpURLConnection();
    当然这是一个象征性的写法。
    2.sun.net.www.protocol.http.HttpURLConnection
      

  4.   

    to   yyzh(答题容易分难得)  
         这段说明我也看到了,但是handler 在代码中找不到与HttpURLConnection 或者JarURLConnection 类有任何关系的啊to  linux_328(企鹅) 
        JAVA与模式中说这是个工厂方法模式。to funcreal(为中华之崛起而编程)  
       关键是 代码中没有return new sun.net.www.protocol.http.HttpURLConnection(); 
    但是yahoo.openConnection();怎么就返回HttpURLConnection类型了呢?
      

  5.   

    public URL(URL context, String spec, URLStreamHandler handler)->handler = getURLStreamHandler(protocol)->static URLStreamHandler getURLStreamHandler(String protocol)
      

  6.   

    这个函数我已经找到了如下:static URLStreamHandler getURLStreamHandler(String protocol) { URLStreamHandler handler = (URLStreamHandler)handlers.get(protocol);
    if (handler == null) {     boolean checkedWithFactory = false;     // Use the factory (if any)
        if (factory != null) {
    handler = factory.createURLStreamHandler(protocol);
    checkedWithFactory = true;
        }     // Try java protocol handler
        if (handler == null) {
    String packagePrefixList = null; packagePrefixList
        = (String) java.security.AccessController.doPrivileged(
                        new sun.security.action.GetPropertyAction(
            protocolPathProp,""));
    if (packagePrefixList != "") {
        packagePrefixList += "|";
    } // REMIND: decide whether to allow the "null" class prefix
    // or not.
    packagePrefixList += "sun.net.www.protocol"; StringTokenizer packagePrefixIter =
        new StringTokenizer(packagePrefixList, "|"); while (handler == null &&
           packagePrefixIter.hasMoreTokens()) {     String packagePrefix =
          packagePrefixIter.nextToken().trim();
        try {
            String clsName = packagePrefix + "." + protocol +
      ".Handler";
    Class cls = null;
    try {
                                cls = Class.forName(clsName);
                            } catch (ClassNotFoundException e) {
        ClassLoader cl = ClassLoader.getSystemClassLoader();
        if (cl != null) {
            cls = cl.loadClass(clsName);
        }
    }
    if (cls != null) {
        handler  =
          (URLStreamHandler)cls.newInstance();
    }
        } catch (Exception e) {
    // any number of exceptions can get thrown here
        }
    }
        }     synchronized (streamHandlerLock) { URLStreamHandler handler2 = null; // Check again with hashtable just in case another
    // thread created a handler since we last checked
    handler2 = (URLStreamHandler)handlers.get(protocol); if (handler2 != null) {
        return handler2;
    } // Check with factory if another thread set a
    // factory since our last check
    if (!checkedWithFactory && factory != null) {
        handler2 = factory.createURLStreamHandler(protocol);
    } if (handler2 != null) {
        // The handler from the factory must be given more
        // importance. Discard the default handler that
        // this thread created.
        handler = handler2;
    } // Insert this handler into the hashtable
    if (handler != null) {
        handlers.put(protocol, handler);
    }     }
    } return handler;    }String clsName = packagePrefix + "." + protocol +  ".Handler";其中 packagePrefix = "sun.net.www.protocol";
         protocol    = “http”但是  Class cls = Class.forName("sun.net.www.protocol.http.Handler"); 这个是什么类呢
    能否再讲详细些啊,谢谢了
      

  7.   

    Handler这个类找到了在 rt\sun\net\www\protocol\http\Handler.class 
    但是我现在就不清楚在那里实列化成 HttpURLConnection 的呢
      

  8.   

    Class.forName("sun.net.www.protocol.http.Handler");的作用是要求JVM查找并加载指定的类在 sun.net.www.protocol.http.Handler 类中应该有一个静态方法产生产生自己的一个实例
      

  9.   

    yahoo.openConnection()返回的就是URLConnection啊
      

  10.   

    yahoo.openConnection();
    我觉得是个工厂方法的single模式,防止对同一个url产生多个connection。
    大家觉的呢?
      

  11.   

    工厂模式有很多种:
    abstract factory
    factory builder
    factory method
    singleton
    prototype