我对抽象类有基本的了解。它本身不能实例化,只能由实现它的类或匿名类实例化。我希望它是正确的..!
但是我遇到了下面的代码。
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
类的类是在运行时创建的类!
答案 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)