68-71 this、static 静态初始化块 传值方式

本文详细解析了Java中对象创建的过程,包括空间分配、成员变量初始化、构造方法执行及对象地址返回等步骤。此外,还介绍了this关键字的用途、static修饰符的特点以及静态初始化块的作用。

1、创建对象分为如下四步:

分配对象空间,并将对象成员变量初始化为0或者空

执行属性值的显示初始化

执行构造方法

返回对象的地址给相关的变量

this 创建好对象的地址

常用于:

在程序中产生二义性之处,this指明当前对象

构造方法中,指向正要初始化的对象

使用this调用重载的构造方法,避免相同的初始化代码,只能在构造方法中,位于构造方法第一句

this不能用在static中

2、static

修饰的成员变量和方法,属于类

静态类中不能用非静态成员

3、静态初始化块

构造方法用于对象的初始化,静态初始化用于类的初始化操作,在静态初始化块中不能直接访问static成员

4、参数传值机制

值传递

传递的是值得副本

 

### 通过 Context 静态方法或单例模式实现 在 Android 开发中,`Context` 是一个非常重要的类,它提供了访问应用程序资源和类的接口。当需要在不同组件之间递数据时,可以通过静态方法或单例模式结合 `Context` 来实现[^3]。 #### 单例模式结合 Context 实现 单例模式是一种设计模式,确保一个类只有一个实例,并提供一个全局访问点。以下是如何使用单例模式结合 `Context` 进行递的示例: 1. **提前初始化方式** 在 `Application` 的 `onCreate()` 方法中初始化单例对象,并将 `Context` 递给它。这种方式虽然简单,但可能会阻塞主线程并减慢应用启动速度[^2]。 ```java public class Singleton { private static Singleton instance; private Context context; private Singleton(Context context) { this.context = context; } public static void init(Context context) { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(context.getApplicationContext()); } } } } public static Singleton getInstance() { if (instance == null) { throw new IllegalStateException("Singleton must be initialized first!"); } return instance; } public void setValue(String key, String value) { SharedPreferences sharedPreferences = context.getSharedPreferences("prefs", Context.MODE_PRIVATE); sharedPreferences.edit().putString(key, value).apply(); } public String getValue(String key) { SharedPreferences sharedPreferences = context.getSharedPreferences("prefs", Context.MODE_PRIVATE); return sharedPreferences.getString(key, null); } } ``` 在 `Application` 中初始化: ```java public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); Singleton.init(this); } } ``` 2. **延迟初始化方式** 推荐使用延迟初始化方式,只有在需要时才创建单例对象。这种方式可以避免不必要的资源消耗,并提高应用性能[^2]。 ```java public class Singleton { private static volatile Singleton instance; private Context context; private Singleton(Context context) { this.context = context.getApplicationContext(); } public static Singleton getInstance(Context context) { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(context); } } } return instance; } public void setValue(String key, String value) { SharedPreferences sharedPreferences = context.getSharedPreferences("prefs", Context.MODE_PRIVATE); sharedPreferences.edit().putString(key, value).apply(); } public String getValue(String key) { SharedPreferences sharedPreferences = context.getSharedPreferences("prefs", Context.MODE_PRIVATE); return sharedPreferences.getString(key, null); } } ``` 使用时: ```java Singleton singleton = Singleton.getInstance(context); singleton.setValue("key", "value"); String value = singleton.getValue("key"); ``` #### 静态方法结合 Context 实现 如果不需要复杂的单例模式,可以直接使用静态方法来实现简单的递。以下是一个示例: ```java public class StaticUtils { private static Context context; public static void init(Context ctx) { context = ctx.getApplicationContext(); } public static void setValue(String key, String value) { SharedPreferences sharedPreferences = context.getSharedPreferences("prefs", Context.MODE_PRIVATE); sharedPreferences.edit().putString(key, value).apply(); } public static String getValue(String key) { SharedPreferences sharedPreferences = context.getSharedPreferences("prefs", Context.MODE_PRIVATE); return sharedPreferences.getString(key, null); } } ``` 初始化: ```java StaticUtils.init(this); // 在 Application 或 Activity 中调用 ``` 使用: ```java StaticUtils.setValue("key", "value"); String value = StaticUtils.getValue("key"); ``` ### 注意事项 - 使用 `Context` 时,应尽量使用 `Application` 的上下文,以避免内存泄漏[^2]。 - 如果需要在多线程环境中操作共享资源(如 `SharedPreferences`),必须确保线程安全[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值