First of all, you must understand the role of the constructor. The constructor is to initialize the attributes or methods of the class. It is automatically called at the moment of new. Then the problem is that the interfaces of Java and PHP cannot be new. So why do we need a constructor? There is no way to call it at all
Because the interface will not have its own instance, there is no need for a constructor.
Why doesn’t an interface have its own instance? Because the interface only defines a series of abstract functions, when using the interface, you will use the class that implements the interface, and you will not use the interface directly. Use it directly, those functions are just definitions and cannot be used
Of course, in Java 8, functions defined by interfaces can have default implementations, but you still cannot directly use the interface to call such functions.
Interface cannot be instantiated. If it can be new, what is the point of having an interface? However, after Java 8, interfaces can have default methods, which can be called directly
Hey...the simpler the question, the harder it is to answer. I can only say that when the Java language was designed, the interface was defined as a specification that a class needs to follow. It was designed to make up for the shortcomings of single inheritance. There is a book "Programmer's Cry" in which the author criticizes the design of Java interfaces and writes: "Goslin said that if the Java language is rewritten, interfaces will not be considered."
I tentatively think that the questioner wants to use the constructor in the interface because he wants to do some code reuse in the class that specifically implements the interface.
interface I {
public I() {
system.out.println("不管你怎么实现,我最牛逼");
}
}
There are some conflicts between "code reuse" and "separation of interface implementation". Each different type is a compromise between the pros and cons.
interface chooses minimum reuse and maximum separation
It is easy to reuse general classes, but the interfaces are not separated
Abstract classes are somewhere in between.
As @MaxValue said, if the interface can have a constructor, it is almost the same as abstract class positioning.
First of all, you must understand the role of the constructor. The constructor is to initialize the attributes or methods of the class. It is automatically called at the moment of new. Then the problem is that the interfaces of Java and PHP cannot be new. So why do we need a constructor? There is no way to call it at all
Why is the king’s
王
three horizontal and one vertical?Interfaces define behavior, not production methods
How do you instantiate it?
Because the interface will not have its own instance, there is no need for a constructor.
Why doesn’t an interface have its own instance? Because the interface only defines a series of abstract functions, when using the interface, you will use the class that implements the interface, and you will not use the interface directly. Use it directly, those functions are just definitions and cannot be used
Of course, in Java 8, functions defined by interfaces can have default implementations, but you still cannot directly use the interface to call such functions.
Interface cannot be instantiated. If it can be new, what is the point of having an interface? However, after Java 8, interfaces can have default methods, which can be called directly
Hey...the simpler the question, the harder it is to answer. I can only say that when the Java language was designed, the interface was defined as a specification that a class needs to follow. It was designed to make up for the shortcomings of single inheritance. There is a book "Programmer's Cry" in which the author criticizes the design of Java interfaces and writes: "Goslin said that if the Java language is rewritten, interfaces will not be considered."
Abstract classes are OK. An interface cannot define method details, only a series of its behaviors.
I tentatively think that the questioner wants to use the constructor in the interface because he wants to do some code reuse in the class that specifically implements the interface.
There are some conflicts between "code reuse" and "separation of interface implementation". Each different type is a compromise between the pros and cons.
interface chooses minimum reuse and maximum separation
It is easy to reuse general classes, but the interfaces are not separated
Abstract classes are somewhere in between.
As @MaxValue said, if the interface can have a constructor, it is almost the same as abstract class positioning.