简体   繁体   English

如何在不扩展Java中的Abstract类的情况下访问abstract类方法

[英]How to access the abstract class method without extending the Abstract class in java

I am new to java and when i am searching over internet for getting the HTTP response code through java, i came to know about [HttpURLConnection] Class in java. 我是Java的新手,当我通过Internet搜索以通过Java获取HTTP响应代码时,我才知道Java中的[HttpURLConnection]类。 This class is abstract class hence i cant instantiate this class. 此类是抽象类,因此我无法实例化此类。 So my queestion is I have a URL object and how to use this abstract class in my code. 所以我的问题是我有一个URL对象以及如何在我的代码中使用此抽象类。 This could be Very Basic Question but your answer would help to understand java fundamentally. 这可能是“非常基本的问题”,但您的回答将有助于从根本上理解Java。

Thanks in Advance 提前致谢

https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html

You get a HttpURLConnection object from a URL instance that represents an http: (or https: ) protocol URL, from its openConnection method . 您可以从URL实例的openConnection方法中获得一个HttpURLConnection对象,该URL实例表示一个http:https: openConnection协议URL。 What you get back is a reference to an object conforming to the interface defined by the URLConnection abstract class (but it'll be an instance of an HttpURLConnection subclass). 您得到的是对一个对象的引用,该对象符合URLConnection抽象类定义的接口(但是它将是HttpURLConnection子类的实例)。

You often don't need to know the concrete class you're working with; 您通常不需要了解您正在使用的具体课程。 you code to an interface that defines the things you should be able to do with what you have. 您将代码编码到一个界面该界面定义了您应该能够使用自己拥有的东西进行操作。 Those things then get done by the concrete class of the object you have a reference to. 然后,这些事情由您所引用的对象的具体类完成。

Re this specifically: 具体来说:

is there way to user methods of Abstract class without using the class which extends the Abstract Class 有没有办法使用Abstract类的用户方法而不使用扩展Abstract类的类

Not if they're instance methods, no. 如果它们是实例方法,则不会。 The clue is in the name. 线索就是名字。 ;-) To use an instance method, you must have an instance. ;-)要使用实例方法,您必须具有一个实例。 To have an instance, you must have a concrete (non-abstract) class that the instance is a member of. 要拥有一个实例,您必须具有该实例所属的具体(非抽象)类。

But again: Often you don't care what that class is, just that you have an instance conforming to the interface you need to use. 再次提醒您:通常,您不必关心该类是什么,而只是拥有符合您需要使用的接口的实例。

You can't instantiate an abstract class needs an subclass to implement specific methods. 您不能实例化一个抽象类,它需要一个子类来实现特定的方法。

You can access its method with an anonymous class for example: 您可以使用匿名类访问其方法,例如:

new HttpURLConnection() { /* here you need to implement many empty methods */ }.getOutputStream();

But for your problem, I think you want a real HttpURLConnection that can be acquired by the URL object. 但是对于您的问题,我认为您想要一个可以由URL对象获取的真实HttpURLConnection。

HttpURLConnection connection = urlObject.openConnection();
connection.setDoInput(true);
connection.setDoOutput(false);
// input = true & output = false means a GET
// input = true & output = true means a POST
connection.connect();
InputStream content = connection.getInputStream();

You have to use a concrete class which is sub class of the abstract class to call the method. 您必须使用作为抽象类的子类的具体类来调用该方法。

Or if the function you want to use is static, you can call it directly. 或者,如果要使用的函数是静态的,则可以直接调用它。 Below is a very small example to show it. 下面是一个非常小的示例来显示它。

abstract class SimpleAbstract {
public static String testString() {
    return "You got me";
}

public abstract void doMagic();}
public class AbstractExample {
public static void main(String[] args) {
    System.out.println(SimpleAbstract.testString());        
}}

Here is a example to intialize abstract class without extending it 这是一个初始化抽象类而不扩展它的示例

    abstract class Abstractclass{
       public void someMethod(){
          System.out.println("Hello");
       }
       abstract public void anotherMethod();
    } 
    public class Demo {
       public static void main(String args[])
       { 
         //creation of object .........................

          Abstractclass obj = new Abstractclass(){
              public void anotherMethod(){
                 System.out.println("Hello12222");
                              }
                };    
         obj.anotherMethod();
         obj.someMethod();
       }
    }

output : Hello12222 Hello 输出: Hello12222 Hello

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

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