day10-接口interface-常量的使用

This commit is contained in:
2026-01-06 15:24:44 +08:00
parent bf48c84d45
commit ef4cc41c96
2 changed files with 24 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
package com.inmind.interface_constant05;
public class Demo05 {
public static void main(String[] args) {
//调用接口的常量
System.out.println(MyInterfaceConstant.age);
System.out.println(MyInterfaceConstant.name);
}
}

View File

@@ -0,0 +1,15 @@
package com.inmind.interface_constant05;
/*
接口中的常量定义和使用方式:
接口中是可以定义“成员变量”,在接口中的“成员变量”其实就是常量,编译器会自动加上一些内容
【public static final】 int i = 10;
final:最终,被它修饰的内容不能被更改
接口常量的作用:为接口的行为规范提供配套的固定值
接口名.常量名来调用
*/
public interface MyInterfaceConstant {
public static final int age = 10;
String name = "hello";//自动加上【public static final】
void method();
}