
隐式变量声明:
- 界面中声明的变量自动为公共、静态和最终。
- 在大型程序中创建共享常量是非常有用的。
代码示例:
// interface que contém constantes
interface iconst {
int min = 0;
int max = 10;
string errormsg = "boundary error";
}
class iconstd implements iconst {
public static void main(string[] args) {
int nums[] = new int[max];
for (int i = min; i = max)
system.out.println(errormsg);
else {
nums[i] = i;
system.out.print(nums[i] + " ");
}
}
}
}
注:虽然对常量有用,但该技术可能存在争议。
接口可扩展
接口继承:
- 其它接口可以通过extends关键字继承。
- 实现衍生接口的类别必须实现整个接口链的所有方法。
代码示例:
// Interface A
interface A {
void meth1();
void meth2();
}
// Interface B estende A
interface B extends A {
void meth3();
}
// Classe que implementa A e B
class MyClass implements B {
public void meth1() {
System.out.println("Implement meth1().");
}
public void meth2() {
System.out.println("Implement meth2().");
}
public void meth3() {
System.out.println("Implement meth3().");
}
}
class IFExtend {
public static void main(String[] args) {
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}
重要提示:如果删除 meth1() 编译错误会发生,因为必须实现所有的接口方法。
以上是接口和扩展中变量的详细内容。请关注图灵教育的其他相关文章!