在创建对象后实现接口

时间:2022-09-23 17:35:38

I was wondering if it was possible for me to implement an interface after the object has been created?

我想知道在创建对象后是否可以实现接口?

I have'nt found anything on it except for this link but it's not really what i'm looking for plus it's for android programming

我没有找到任何东西,除了这个链接,但它不是我真正想要的加上它的Android编程

Is there any way to acheive this or do i have to extends the object then add the interface to it?

有没有办法实现这个或我是否必须扩展对象然后添加接口?

No code as it is a theorical question.

没有代码,因为它是一个理论问题。

1 个解决方案

#1


2  

You can't implement an interface after an object has been created. Implementing an interface is not a method you can call on an object.

创建对象后无法实现接口。实现接口不是可以在对象上调用的方法。

Even if you could, what would be the point? By implementing an interface, you "make a promise" that you will define the methods that are in the interface. In fact your program won't even compile if you don't. So if you want to implement an interface, you need to do it in the class definition and you have to define all the methods of the interface. Only then can you create instances of the class. You can't change the methods of an object, or add to it.

即使你可以,有什么意义呢?通过实现一个接口,您“做出承诺”,您将定义接口中的方法。事实上,如果你不这样做,你的程序甚至都不会编译。因此,如果要实现接口,则需要在类定义中执行此操作,并且必须定义接口的所有方法。只有这样才能创建类的实例。您无法更改对象的方法或添加对象。

#1


2  

You can't implement an interface after an object has been created. Implementing an interface is not a method you can call on an object.

创建对象后无法实现接口。实现接口不是可以在对象上调用的方法。

Even if you could, what would be the point? By implementing an interface, you "make a promise" that you will define the methods that are in the interface. In fact your program won't even compile if you don't. So if you want to implement an interface, you need to do it in the class definition and you have to define all the methods of the interface. Only then can you create instances of the class. You can't change the methods of an object, or add to it.

即使你可以,有什么意义呢?通过实现一个接口,您“做出承诺”,您将定义接口中的方法。事实上,如果你不这样做,你的程序甚至都不会编译。因此,如果要实现接口,则需要在类定义中执行此操作,并且必须定义接口的所有方法。只有这样才能创建类的实例。您无法更改对象的方法或添加对象。