扩展没有构造函数的类 [英] Extend a class who has no constructor

查看:245
本文介绍了扩展没有构造函数的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无法修改的外部类,我想向与我的项目相关的类中添加自定义功能,因此我创建了一个类,对该类进行了扩展以添加所需的其他方法.

I've got an external class I cannot modify and I want to add customized functionality to that class related to my project, so I created a class which extends it in order to add the extra methods I need.

问题是Car类没有构造函数,所以我不能使用super,创建Car对象的唯一方法是通过返回完全填充的Car对象的本机方法.

The problem is that Car class doesn't have a constructor, so I can't use super, the only way to create a Car object is through a native method that returns a fully filled Car object.

public class Car {
    //The only way to create a car  
    public native Car readFromFile (String file);

    //Many other variables and methods

}

我的自定义类:

import com.example.Car;

public class CustomCar extends Car {
    public void extraMethod1(){
        //do something using Car variables
    }
}

很显然我无法转换为(CustomCar),因为我有ClassCastException,所以如何从该本机方法获取Car并在该对象上使用CustomCar的方法?

Obviously I cannot cast to (CustomCar) as I have ClassCastException, so how can I get a Car from that native method and use on that object the methods from CustomCar?

我应该在CustomCar中有一个可变的Car并在CustomCar方法中使用它吗?

Should I just have a variable Car in CustomCar and use it in CustomCar methods?

import com.example.Car;

public class CustomCar {
    Car car;
    public void extraMethod1(){
        car.doSomething(); //do something using Car variables

    }
}

还是我应该创建一个复制所有父变量的新构造函数?

Or should I just create a new constructor copying all the parents variables?

非常感谢您的帮助!

推荐答案

我怀疑您的问题可能基于误解.您的示例类Car具有一个默认的构造函数,等效于编写以下代码:

I suspect that your question may be based on a misconception. Your example class Car has a default constructor that is equivalent to writing this:

    public Car() { }

仅当类没有声明任何显式构造函数时,才会添加默认构造函数.

The default constructor is added if and only if a class doesn't declare any explicit constructors.

如果将CustomCar声明为Car的子类,则可以使用默认的Car构造函数.

If you declare CustomCar as a subclass of Car it can use the default Car constructor.

第二个问题是您已将readFromFile声明为

A second issue is that you have declared readFromFile as

    public native Car readFromFile (String file);

必须是

    public static native Car readFromFile (String file);

可用. (如果readFromFile确实是实例方法,并且确实是创建Car实例的唯一方法,那么您将遇到引导问题.如何创建第一个Car?)

to be usable. (If readFromFile is genuinely an instance method, and genuinely the only way that Car instances get created, then you have a bootstrapping problem. How do you create the first Car?)

话虽如此,如果Car类的默认构造函数不起作用(即,它没有正确初始化Car的状态),那么您就会遇到问题:

Having said that, if the default constructor for your Car class doesn't work (i.e. it doesn't initialize the state of a Car correctly) then you have a problem:

  • 如果CustomCar(...)仅使用Car(),则超类将无法正确初始化.

  • If CustomCar(...) simply uses Car(), the superclass will not be properly initialize.

如果将其分类为Car.readFromFile(),则结果将是一个新对象,该对象是Car,而不是CustomCar.

If it class Car.readFromFile() the result will be a new object that is a Car, not a CustomCar.

可能有一些方法可以解决此问题.例如:

There are possibly some ways to work around this. For example:

  • 您可以在Car中添加一个显式构造函数,以执行必要的初始化.

  • You could add an explicit constructor to Car that does the necessary initialization.

您可以将CustomCar实施为真正的Car实例的包装,该实例使用本机Car方法实例化CustomCar.然后,将CustomCar实现委托委派给包装的实例.如果没有其他问题,则应该可以将CustomCar设置为Car的子类型.

You could implement CustomCar as a wrapper for a real Car instance that CustomCar instantiates using the native Car method. Then have the CustomCar implementation delegate to the wrapped instance. If there are no other problems, you should be able to make CustomCar a subtype of Car.

哪种方法更好?这很难说.这取决于您的实际用例的细节;例如通过本机方法创建的对象是否具有隐藏状态或特殊的身份相关"属性? (为什么要使用本机代码创建它?)

Which approach is better? It is hard to say. It depends on details of your real use-case; e.g. does the object created by the native method have hidden state, or special "identity related" properties? (Why are you creating it using native code?)

如果这些变通办法无法正常工作,则您有问题. Car::readFromFile不可能成为任何Car子类所需的构造函数.

If there reasons that these workarounds won't work, then you have a problem. There is no way that Car::readFromFile can be the constructor that any Car subclass requires.

这篇关于扩展没有构造函数的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆