简体   繁体   English

使用私有构造函数扩展 class

[英]Extend a class with private constructer

Is it possible to extend a class with a nonvisible constructor without having to know what this constructor do and implement it again?是否可以使用不可见的构造函数扩展 class 而不必知道该构造函数做什么并再次实现它?

import java.sql.DriverManager;
    
public class myDriverManager extends DriverManager {
    public myDriverManager() {
        super(); //Fehler: the constructor DriverManager() ist not visible
    }
    
}

You can't extend DriverManager , but you can implement DataSource instead您不能扩展DriverManager ,但可以实现DataSource代替

NOTE: The DataSource interface, new in the JDBC 2.0 API, provides another way to connect to a data source.注意:JDBC 2.0 API 中新增的 DataSource 接口提供了另一种连接数据源的方式。 The use of a DataSource object is the preferred means of connecting to a data source.使用数据源 object 是连接数据源的首选方式。

Every constructor's first line is either a call to its super class constructor or to any current class constructor, so even if you don't keep it compiler will keep a super();每个构造函数的第一行要么调用它的超级 class 构造函数,要么调用任何当前的 class 构造函数,所以即使你不保留它,编译器也会保留一个super(); as first line.作为第一行。

Now coming to DriverManager its only constructor is private and when you extend it to your class, its constructor will try to call the DriverManager 's constructor as per above logic and gives Compile time error since its private and becomes not visible.现在来到DriverManager ,它唯一的构造函数是private的,当你将它扩展到 class 时,它的构造函数将尝试按照上述逻辑调用DriverManager的构造函数,并给出编译时间错误,因为它是私有的并且变得不可见。

The case will be same even if you don't declare the class without constructor即使您没有声明没有构造函数的 class,情况也会相同

public class myDriverManager extends DriverManager {
    
}

This will also give the same error, because the compiler will create a default constructor and its first line will be super();这也会给出同样的错误,因为编译器将创建一个默认构造函数,并且它的第一行将是super(); by default, again by the above logic, constructor is again invisible.默认情况下,再次按照上述逻辑,构造函数再次是不可见的。

So basically what happens is when you extend DriverManager your class has to call super();所以基本上发生的事情是当你扩展DriverManager你的 class 必须调用super(); at some part of its code, and gives compile time error and hence DriverManager cannot be extended to any class.在其代码的某些部分,并给出编译时错误,因此DriverManager不能扩展到任何 class。

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

相关问题 用其他包中的私有构造函数扩展类 - extend class with private constructor in other package 将父类设为私有,但使子类中的字段与“ extend”一起使用 - Making parent class private but make fields useable with 'extend' in subclass 如何在`java.util`中扩展package-private类 - How to extend package-private class in `java.util` Junit for Java Clock,无需任何额外的类或构造函数或注入 - Junit for java Clock without any extra class or constructer or Injection 私有Java扩展 - Private extend in Java 创建自定义视图:如何扩展类并访问基类的私有成员变量? - Creating a custom view: how can I extend a class and access the base class's private member variables? 类扩展类的适配器 - Adapter for class extend class 您如何从另一个类调用构造方法并询问其含义? - How do you call a constructer method from another class and ask what it equals? 我为 para 构造函数创建的 class 的 object 显示错误。 我得到的错误是找不到符号 - The object that I created of the class for para constructer is showing a error. The error that I am getting is that that the symbol cant be found 使用带有嵌套静态类的类的良好做法,然后使用更多静态方法扩展包私有抽象类以保持组织? - Good practice to use a class w/ nested static classes, that then, extend package-private abstract classes with more static methods to stay organized?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM