interface里可以定义属性吗?(全局常量,全局变量,属性区别)

2025-05-03 15:49:18

全局常量: public static final 常量名(string a)全局变量: public static 变量名(string a)属性:(属于某个类或对象) private String a

interface里可以定义属性吗?

本质上不可以。如果强行在interface里加一个属性,则该属性自动变成public static。显然,这不算是个属性。

例如,

public interface Listener { int number; } 虽然number没加static,但是编译会自动把它按public static处理。

为什么这样呢?因为接口就是用来定义接口的,在里面加属性没有意义。

另外,静态属性还叫属性吗?不算了。因为属性表示属于,某对象有属性、这个属性就属于这个对象。但静态对象则表示,这个东西是属于大家的!

Copyright © 2022 世界杯预选赛欧洲区_世界杯在哪个国家举行 - kd896.com All Rights Reserved.