Java 9接口中的私有方法

2023年 8月 28日 47.4k 0

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.

以上就是Java 9接口中的私有方法的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论