简体   繁体   English

在Java中创建抽象类URLConnection或HttpURLConnection的对象

[英]Creating an object of abstract class URLConnection or HttpURLConnection in Java

I saw the following code in a tutorial: 我在教程中看到了以下代码:

URLConnection connection = new URL("http://example.com").openConnection();

How is that possible? 怎么可能? The API says that URLConnection (and also the Subclass HttpURLConnection) is an abstract class. API说URLConnection(以及Subclass HttpURLConnection)是一个抽象类。 But abstract classes cannot be instantiated. 但是抽象类无法实例化。 I also saw other tutorials with (for example) this code: 我还看到了其他教程(例如)这段代码:

URL url = new URL( "http://java-tutor.com/index.html" );
URLConnection con = url.openConnection();

Why is there no 'new'? 为什么没有'新'? For me that's very strange, so can someone explain this to me. 对我来说这很奇怪,所以有人可以向我解释。

openConnection();

creates an instance of implementation of URLConnection class (sun.net.www.protocol.http.HttpURLConnection @Credit goes to Sotirios). 创建URLConnection类的实现实例(sun.net.www.protocol.http.HttpURLConnection @Credit转到Sotirios)。

Based on javadoc : 基于javadoc

If for the URL's protocol (such as HTTP or JAR), there exists a public, specialized URLConnection subclass belonging to one of the following packages or one of their subpackages: java.lang, java.io, java.util, java.net, the connection returned will be of that subclass. 如果对于URL的协议(例如HTTP或JAR),存在属于以下包之一或其子包之一的公共专用URLConnection子类:java.lang,java.io,java.util,java.net,返回的连接将是该子类。 For example, for HTTP an HttpURLConnection will be returned , and for JAR a JarURLConnection will be returned. 例如, 对于HTTP,将返回HttpURLConnection ,对于JAR,将返回JarURLConnection。

First of all, you don't instantiate URLConnection but URL at: 首先,您不要实例化URLConnection而是在以下位置实现URL

URLConnection connection = new URL("http://example.com").openConnection();

URLConnection is the variable type (which can be abstract or even an interface ), URL is the specific class you instantiate. URLConnection是变量类型(可以是abstract 甚至是 interface ), URL是您实例化的特定类。

About the no new word, openConnection() returns URLConnection object so you don't need to create one yourself. 关于no new word, openConnection()返回URLConnection对象,因此您不需要自己创建一个。

URL is a factory that creates a URLConnection based (if memory serves me correctly) by a scheme. URL是一个工厂,它通过一个方案创建一个基于URLConnection (如果内存正确地为我提供服务)。 You can have HttpsURLConnection or HttpURLConnection . 你可以有HttpsURLConnectionHttpURLConnection

URLConnection is indeed an abstract class. URLConnection确实是一个抽象类。 It cannot be instantiated. 它无法实例化。

So as not to be useless, however, for every abstract class in a class structure there must be a subclass (maybe several levels removed) that is concrete, ie that can be instantiated. 但是,为了避免无用,对于类结构中的每个抽象类,必须有一个具体的子类(可能删除了几个级别),即可以实例化。

URL.openConnection() is returning one of those subclasses to you. URL.openConnection()正在向您返回其中一个子类。 You could verify this with a debugger, if you like. 如果您愿意,可以使用调试器验证这一点。 URLConnection is the type of the reference you call "con," not the type of the object referred to by that reference. URLConnection是您调用“con”的引用类型,而不是该引用引用的对象的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM