day10--接口的常量的定义和使用

This commit is contained in:
xx
2026-10-08 14:09:25 +08:00
parent dfced7ea6e
commit 2c2fc1dd8c
2 changed files with 29 additions and 0 deletions
@@ -0,0 +1,18 @@
package com.inmind.interface_constant05;
/*
接口中的内容特点:
1.抽象方法(jdk7之前):制定规则,必须遵守的规范
2.默认方法(jdk8):优化解决接口升级的问题,选择性地沿用,或者扩展新功能
3.静态方法(jdk8):定义一些为接口本身相关的,通用的工具类方法
4.私有方法(jdk9): 对默认方法和静态方法,进行共有代码的抽取复用,且不让外界调用
5.常量:定义了公共的标准值,为接口的行为提供规范,配套的固定值
*/
public class Demo05 {
public static void main(String[] args) {
System.out.println(MyInterface.a);
int b = 10+MyInterface.a;
System.out.println(b);
System.out.println(MyInterface.name);
}
}
@@ -0,0 +1,11 @@
package com.inmind.interface_constant05;
/*
接口中定义"成员属性",其实是一个常量,编译器会在成员属性前,默认加上【public static final】!!!
final:最终,被它修饰的内容就不能更改
接口中的常量的作用:为接口的行为规范,提供配套的固定值
接口中的常量如何使用:接口名.常量名来调用
*/
public interface MyInterface {
int a = 10;
public static final String name = "张三";
}