指向抽象类

时间:2019-03-12 15:36:40

标签: java abstract-class

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

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

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类的类是在运行时创建的类!

1 个答案:

答案 0 :(得分:4)

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

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

oracle.xml.jaxp.JXSAXParserFactory

然后

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

向<{> Classloader 请求JXSAXParserFactory Class对象(Class<JXSAXParserFactory>)。

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

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

作为JXSAXParserFactory extends SAXParserFactory,可以上映。 这是正确的名词。

扩展表示继承父亲的签名。显然,通过转换,您可以释放该子类的其他暴露成员。


SAXParserFactory#newSAXParser在返回SAXParser方面总是受到限制,但是底层实现(基本上是逻辑)将有所不同。

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