1. Define members in the interface
C#. As shown in the figure, I added a field n to the interface ITest, then vs directly displays a red bottom line, and the error is The interface cannot contain the field
java, as shown below. The compilation also reports an error, but it is not that the interface cannot be included but that the assignment is missing, so let's assign the value and see again. You can see that there is no problem, that is, the member variables are public static final by default. We know that final must be assigned a value, so if it is not assigned, the place will be compiled.
After the assignment, it was found that there was no problem, as shown below:
2. Define some methods in the interface
C#, we all know that the methods in an interface need to be implemented by subclasses, so our methods in the interface cannot be implemented. If an implementation is added, there will be an error. The error is that the interface cannot be defined.
#Java, that is, jdk1.8, has added some methods that can be implemented. It can be implemented in default and static. As shown below, there is no problem compiling. So does c# have default and static methods? In the interface, sorry there are none.
# It is not available in c#. For static, default interface methods cannot be defined. Because it is invalid.
Summary:
1. In java, interfaces can contain fields, but these fields are implicitly static and final, which is not allowed in C# If there are fields in the interface, the compiler will prompt an error when compiling;
2. In Java, method declarations can have public modifiers (even if this is not necessary), but in C#, explicitly It is illegal to specify the public modifier for a method in an interface.
Recommended tutorial: Getting started with java development
The above is the detailed content of The difference between c# interface and java interface. For more information, please follow other related articles on the PHP Chinese website!