我有抽象类的基础知识。它本身不能实例化,只能由实现它的类或匿名类实例化。我希望它是正确的..!

但是我遇到了下面的代码。

SAXParserFactory factory = SAXParserFactory.newInstance();


这是一个newInstance源代码:

public static SAXParserFactory newInstance()
  86:     throws FactoryConfigurationError
  87:   {
  88:     ClassLoader loader = Thread.currentThread().getContextClassLoader();
  89:     if (loader == null)
  90:       {
  91:         loader = SAXParserFactory.class.getClassLoader();
  92:       }
  93:     String className = null;
  94:     int count = 0;
  95:     do
  96:       {
  97:         className = getFactoryClassName(loader, count++);
  98:         if (className != null)
  99:           {
 100:             try
 101:               {
 102:                 Class t = (loader != null) ? loader.loadClass(className) :
 103:                   Class.forName(className);
 104:                 return (SAXParserFactory) t.newInstance();
 105:               }
 106:             catch (ClassNotFoundException e)
 107:               {
 108:                 className = null;
 109:               }
 110:             catch (Exception e)
 111:               {
 112:                 throw new FactoryConfigurationError(e,
 113:                      "error instantiating class " + className);
 114:               }
 115:           }
 116:       }
 117:     while (className == null && count < 3);
 118:     return new gnu.xml.stream.SAXParserFactory();
 119:   }
 120:
 121:   private static String getFactoryClassName(ClassLoader loader, int attempt)
 122:   {
 123:     final String propertyName = "javax.xml.parsers.SAXParserFactory";
 124:     switch (attempt)
 125:       {
 126:         case 0:
 127:           return System.getProperty(propertyName);
 128:         case 1:
 129:           try
 130:             {
 131:               File file = new File(System.getProperty("java.home"));
 132:               file = new File(file, "lib");
 133:               file = new File(file, "jaxp.properties");
 134:               InputStream in = new FileInputStream(file);
 135:               Properties props = new Properties();
 136:               props.load(in);
 137:               in.close();
 138:               return props.getProperty(propertyName);
 139:             }
 140:           catch (IOException e)
 141:             {
 142:               return null;
 143:             }
 144:         case 2:
 145:           try
 146:             {
 147:               String serviceKey = "/META-INF/services/" + propertyName;
 148:               InputStream in = (loader != null) ?
 149:                  loader.getResourceAsStream(serviceKey) :
 150:                 SAXParserFactory.class.getResourceAsStream(serviceKey);
 151:               if (in != null)
 152:                 {
 153:                   BufferedReader r =
 154:                      new BufferedReader(new InputStreamReader(in));
 155:                   String ret = r.readLine();
 156:                   r.close();
 157:                   return ret;
 158:                 }
 159:             }
 160:           catch (IOException e)
 161:             {
 162:             }
 163:           return null;
 164:         default:
 165:           return null;
 166:       }
 167:   }
 168:


如果您看到该代码,则可能在行号104和118处返回SAXParserFactory的引用类型。

在第104行,其创建动态类。我想知道如何将新创建的类转换为抽象类类型SAXParserFactory?我很困惑。

实例化SAXParserFactory之后,以下代码运行

SAXParser saxParser = factory.newSAXParser();


因此,一旦SAXParserFactory被实例化,抽象类SAXParserFactory类的newSAXParser()方法应在使用它之前实现,但是从何处调用它?因为实现SAXParserFactory类的类是在运行时创建的类!

最佳答案

97: className = getFactoryClassName(loader, count++);


该行返回扩展SAXParserFactory的类的全名。
一个例子可能是

oracle.xml.jaxp.JXSAXParserFactory


然后

102: Class t = (loader != null) ? loader.loadClass(className)
103:                            : Class.forName(className);


向类加载器询问JXSAXParserFactory Class对象(Class<JXSAXParserFactory>)。

104: return (SAXParserFactory) t.newInstance();


Class#newInstance被调用,在这种情况下,这意味着JXSAXParserFactory的no-args构造函数被调用。

作为JXSAXParserFactory extends SAXParserFactory,可以向上投射。
这是正确的术语。

扩展意味着继承父亲签名。显然,通过转换,您可以松散子类的其他暴露成员。



SAXParserFactory#newSAXParser在返回SAXParser时始终受到限制,但是基础实现(基本上是逻辑)将有所不同。

https://en.wikipedia.org/wiki/Polymorphism_(computer_science)

10-02 06:57