首页 > Java > java教程 > 正文

Java 9接口中的私有方法

王林
发布: 2023-08-20 14:05:08
转载
1297人浏览过

java 9接口中的私有方法

以下是一个示例,展示了如何在Java 9接口中使用私有方法 −

示例

interface my_int{
   public abstract void multiply_vals(int a, int b);
   public default void add_vals(int a, int b){
      sub_vals(a, b);
      System.out.print("Default method result ");
      System.out.println(a + b);
   }
   private void sub_vals(int a, int b){
      System.out.print("Private method result ");
      System.out.println(a - b);
   }
   private static void div(int a, int b){
      System.out.print(" Private static method result ");
      System.out.println(a / b);
   }
}
public class my_new_int implements my_int{
   @Override
   public void multiply_vals(int a, int b){
      System.out.print("Abstract method result ");
      System.out.println(a * b);
   }
   public static void main(String[] args){
      my_int in = new my_new_int();
      in.multiply_vals(11, 34);
      in.add_vals(78, 0);
   }
}
登录后复制

Output

Abstract method result 374
Private method result 78
Default method result 78
登录后复制

An interface named ‘my_int’ is defined, that has an abstract function, without a body. Another 默认函数被定义为基本上将两个数字相加。另一个名为‘sub_vals’的函数是 defined, which subtracts the two numbers.

Another static function named div is defined that divides the two values. A class named ‘my_new_int’ implements the previously defined interface. It overrides the function that multiplies values and redefines it. In the main function, an instance of the interface is created, and the ‘multiply_vals’ function is called by passing specific values. Similarly, the ‘add_vals’ function is also called by passing specific values. The respective output is displayed on the console.

Android中JNI编程的那些事儿 中文WORD版
Android中JNI编程的那些事儿 中文WORD版

本文档主要讲述的是Android中JNI编程的那些事儿;JNI译为Java本地接口。它允许Java代码和其他语言编写的代码进行交互。在android中提供JNI的方式,让Java程序可以调用C语言程序。android中很多Java类都具有native接口,这些接口由本地实现,然后注册到系统中。希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看

Android中JNI编程的那些事儿 中文WORD版 0
查看详情 Android中JNI编程的那些事儿 中文WORD版

以上就是Java 9接口中的私有方法的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号