接口组成更新概述:
接口的组成:
常量
public static final
抽象方法
public abstract
默认方法java8
静态方法java8
私有方法java9
接口中默认方法
接口中默认方法的定义格式:
格式:public default 返回值类型 方法名(参数列表){ }
范例:public default void show3() { }
接口中默认方法的注意事项
默认方法不是抽象方法,所以不强制被重写。但是可以被重写,重写的时候去掉default关键字
public可以省略,default不能省略
package com.aynu19;
public interface MyInterface {
void show1();
void show2();
// void show3();
// public default void show3(){
// System.out.println("show3");
// }
default void show3(){
System.out.println("show3");
}
}
package com.aynu19;
public class MyInterfaceDemo {
public static void main(String[] args) {
//按照多态的方式创建对象并使用
MyInterface my=new MyInterfacelone();
my.show1();
my.show2();
my.show3();
MyInterfaceImplTwo my1=new MyInterfaceImplTwo();
my1.show1();
my1.show2();
my1.show3();
}
}
package com.aynu19;
public class MyInterfaceImplTwo implements MyInterface{
@Override
public void show1() {
System.out.println("Two show1");
}
@Override
public void show2() {
System.out.println("Two show2");
}
}
package com.aynu19;
public class MyInterfacelone implements MyInterface{
@Override
public void show1() {
System.out.println("One show1");
}
@Override
public void show2() {
System.out.println("One show2");
}
@Override
public void show3() {
System.out.println("One show3");
}
}
package com.aynu19;
public interface MyInterfaceSon extends MyInterface{
void show3();
}
One show1
One show2
One show3
Two show1
Two show2
show3
接口中静态方法的定义格式:
格式:public static 返回值类型 方法名(参数列表){ }
范例:public static void show(){ }
接口中静态方法的注意事项:
静态方法只能通过接口名调用,不能通过实现类名或者对象名调用
public可以省略,static不能省略
package com.aynu20;
public interface Inter {
//抽象方法
void show();
//默认方法
default void method(){
System.out.println("Inter 中的默认方法执行了");
}
//静态方法
public static void test(){
System.out.println("Inter 中的静态方法执行了");
}
}
package com.aynu20;
public class InterDemo {
public static void main(String[] args) {
//按照多态的方法创建对象并使用
Inter i=new InterImpl();
i.show();
i.method();
// i.test();
Inter.test();
// InterImpl.test();
}
}
package com.aynu20;
public class InterImpl implements Inter{
@Override
public void show() {
System.out.println("show方法执行了");
}
}
show方法执行了
Inter 中的默认方法执行了
Inter 中的静态方法执行了
接口中私有方法:
接口中私有方法的定义格式:
格式1:private返回值类型 方法名(参数列表){ }
范例1:private void show(){ }
格式2:private static 返回值类型 方法名(参数列表){ } //静态的私有方法
范例2:private static void method(){ }
接口中私有方法的注意事项:
默认方法可以调用私有的静态方法和非静态方法
静态方法只能调用私有的静态方法文章来源:https://www.toymoban.com/news/detail-408818.html
package com.aynu21;
public interface Inter {
default void show1(){
System.out.println("show1开始执行");
// System.out.println("初级工程师");
// System.out.println("中级工程师");
// System.out.println("高级工程师");
show();
System.out.println("show1结束执行");
}
default void show2(){
System.out.println("show2开始执行");
// System.out.println("初级工程师");
// System.out.println("中级工程师");
// System.out.println("高级工程师");
show();
System.out.println("show2结束执行");
}
private void show(){
System.out.println("初级工程师");
System.out.println("中级工程师");
System.out.println("高级工程师");
}
static void method1(){
System.out.println("method1开始执行");
// System.out.println("初级工程师");
// System.out.println("中级工程师");
// System.out.println("高级工程师");
method();
System.out.println("method1结束执行");
}
static void method2(){
System.out.println("method2开始执行");
// System.out.println("初级工程师");
// System.out.println("中级工程师");
// System.out.println("高级工程师");
method();
System.out.println("method2结束执行");
}
private static void method(){
System.out.println("初级工程师");
System.out.println("中级工程师");
System.out.println("高级工程师");
}
}
package com.aynu21;
public class InterDemo {
public static void main(String[] args) {
//按照多态的方式创建对象并使用
Inter i =new InterImpl();
i.show1();
System.out.println("--------");
i.show2();
System.out.println("--------");
Inter.method1();
System.out.println("--------");
Inter.method2();
}
}
package com.aynu21;
public class InterImpl implements Inter{
}
show1开始执行
初级工程师
中级工程师
高级工程师
show1结束执行
--------
show2开始执行
初级工程师
中级工程师
高级工程师
show2结束执行
--------
method1开始执行
初级工程师
中级工程师
高级工程师
method1结束执行
--------
method2开始执行
初级工程师
中级工程师
高级工程师
method2结束执行 文章来源地址https://www.toymoban.com/news/detail-408818.html
到了这里,关于接口组成更新的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!