首页 技术 正文
技术 2022年11月9日
0 收藏 679 点赞 3,638 浏览 1347 个字

1.Java 接口的访问权限

 interface A{}//接口A包访问权限   public interface A{}//接口A公有访问   interface A{     void function1();     public void function2();//function1,function2 都是public的,即接口默认方法默认是public的,也必须是public的。
                    //另,接口中的域(成员变量)默认是static,final的
                //也必须是这样的。
}

2.接口的类实现问题

  public class B implements A{     public void function1(){}     public void function2(){}//必须是public,因为Java编译器不允许在继承关系中访问权限降低。                  //必须同时实现接口A中的全部方法,否则编译出错。这与抽象基类不同                 //如果没有完全是实现抽象基类中的抽象方法,最多是不能创建对象。   }

3.接口与抽象基类的交叉问题(片面总结)

package JavaProject;
interface A{
static void test1(){System.out.println("interface A");};//接口中的static方法,必须有函数体!
void test2();
}
public abstract class B implements A{
public static void test1(){System.out.println("class B");}
public abstract void test2();//不需要实现的方法定义为抽象
public static void main(String[]args){
B.test1();
A.test1();
//new B();//error
}
}

4.导出类中,基类,接口方法的覆盖重载和实现

下面是 《Thinking in Java》Forth Edition P181 中的实例

package interfaces;
interface I1{void f();}
interface I2{void f(int i);}
interface I3{int f();}
class C{
public int f(){return 1;}
}class C2 implements I1,I2{
public void f(){}
public int f(int i){return 1;}
}
class C3 extends C implements I2{
public int f(int i){return 1;}
}
class C4 extends C implements I3{
//Identical. no problem:
public int f(){return 1;}
}
//!class C5 extends C implements I1{}
//!interface I4 extends I1,I3{}

去掉最后两行的注释编译出错,此例中覆盖,实现和重载搅在了一起,而且重载方法仅通过返回值类型是区分不开的

在打算组合的不同接口中使用相同的方法名通常会造成代码可读性的混乱,应尽量避免这种情况。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900