我理解如果一个类实现包含同名默认方法的多个接口,那么我们需要在子类中重写该方法,以便明确定义我的方法将执行的操作.
问题是,见下面的代码:
interface A {
default void print() {
System.out.println(" In interface A ");
}
}
interface B {
default String print() {
return "In interface B";
}
}
public class C implements A, B {
@Override
public String print() {
return "In class C";
}
public static void main(String arg[]) {
// Other funny things
}
}
Run Code Online (Sandbox Code Playgroud)
现在,接口A和B都有一个名为'print'的默认方法,但我想覆盖接口B的print方法 - 返回字符串并按原样保留A的打印方式.但是这段代码不能编译给出:
Overrides A.print
The return type is incompatible with A.print()
Run Code Online (Sandbox Code Playgroud)
很明显,编译器试图覆盖A的打印方法,我不明白为什么!
#include<stdio.h>
int main()
{
struct byte
{
int one:1;
};
struct byte var = {1}; // statement A
printf("%d", var.one);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这里,我想我们已经将整数变量'one'的位存储器限制为1,即; 它只能存储0和1而在'语句A'我们用'1'初始化了这个变量,这应该没问题,因为'one'仍然可以在其内存中保留一点但是它在控制台上打印-1!有帮助吗?