#import <Cocoa/Cocoa.h> @interface NSButton (TextColor) - (NSColor *)textColor; - (void)setTextColor:(NSColor *)textColor; @end #import "NSButton+TextColor.h" @implementation NSButton (TextColor) - (NSColor *)textColor { NSAttributedString *attrTitle = [self attributedTitle]; int len = [attrTitle length]; NSRange range = NSMakeRange(0, MIN(len, 1)); // take color from first char NSDictionary *attrs = [attrTitle fontAttributesInRange:range]; NSColor *textColor = [NSColor controlTextColor]; if (attrs) { textColor = [attrs objectForKey:NSForegroundColorAttributeName]; } return textColor; } - (void)setTextColor:(NSColor *)textColor { NSMutableAttributedString *attrTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[self attributedTitle]]; int len = [attrTitle length]; NSRange range = NSMakeRange(0, len); [attrTitle addAttribute:NSForegroundColorAttributeName value:textColor range:range]; [attrTitle fixAttributesInRange:range]; [self setAttributedTitle:attrTitle]; [attrTitle release]; } @end
我们大家都知道,对于静态变量、静态初始化块、变量、初始化块、构造器,它们的初始化顺序依次是(静态变量、静态初始化块)>(变量、初始化块)>构造器。我们也可以通过下面的测试代码来验证这一点:
Java代码
1.public class InitialOrderTest {
2.
3. // 静态变量
4. public static String staticField = "静态变量";
5. // 变量
6. public String field = "变量";
7.
8. // 静态初始化块
9. static {
10. System.out.println(staticField);
11. System.out.println("静态初始化块");
12. }
13.
14. // 初始化块
15. {
16. System.out.println(field);
17. System.out.println("初始化块");
18. }
19.
20. // 构造器
21. public InitialOrderTest() {
22. System.out.println("构造器");
23. }
24.
25. public static void main(String[] args) {
26. new InitialOrderTest();
27. }
28.}
Java代码
1.public class InitialOrderTest {
2.// 静态变量
3.public static String staticField = "静态变量";
4.// 变量
5.public String field = "变量";
6.// 静态初始化块
7.static {
8.System.out.println(staticField);
9.System.out.println("静态初始化块");
10.}
11.// 初始化块
12.{
13.System.out.println(field);
14.System.out.println("初始化块");
15.}
16.// 构造器
17.public InitialOrderTest() {
18.System.out.println("构造器");
19.}
20.public static void main(String[] args) {
21.new InitialOrderTest();
22.}
23.}
public class InitialOrderTest {
// 静态变量
public static String staticField = "静态变量";
// 变量
public String field = "变量";
// 静态初始化块
static {
System.out.println(staticField);
System.out.println("静态初始化块");
}
// 初始化块
{
System.out.println(field);
System.out.println("初始化块");
}
// 构造器
public InitialOrderTest() {
System.out.println("构造器");
}
public static void main(String[] args) {
new InitialOrderTest();
}
}
运行以上代码,我们会得到如下的输出结果:
1.静态变量
2.静态初始化块
3.变量
4.初始化块
5.构造器
这与上文中说的完全符合。那么对于继承情况下又会怎样呢?我们仍然以一段测试代码来获取最终结果:
Java代码
1.class Parent {
2. // 静态变量
3. public static String p_StaticField = "父类--静态变量";
4. // 变量
5. public String p_Field = "父类--变量";
6.
7. // 静态初始化块
8. static {
9. System.out.println(p_StaticField);
10. System.out.println("父类--静态初始化块");
11. }
12.
13. // 初始化块
14. {
15. System.out.println(p_Field);
16. System.out.println("父类--初始化块");
17. }
18.
19. // 构造器
20. public Parent() {
21. System.out.println("父类--构造器");
22. }
23.}
24.
25.public class SubClass extends Parent {
26. // 静态变量
27. public static String s_StaticField = "子类--静态变量";
28. // 变量
29. public String s_Field = "子类--变量";
30. // 静态初始化块
31. static {
32. System.out.println(s_StaticField);
33. System.out.println("子类--静态初始化块");
34. }
35. // 初始化块
36. {
37. System.out.println(s_Field);
38. System.out.println("子类--初始化块");
39. }
40.
41. // 构造器
42. public SubClass() {
43. System.out.println("子类--构造器");
44. }
45.
46. // 程序入口
47. public static void main(String[] args) {
48. new SubClass();
49. }
50.}
Java代码
1.class Parent {
2.// 静态变量
3.public static String p_StaticField = "父类--静态变量";
4.// 变量
5.public String p_Field = "父类--变量";
6.// 静态初始化块
7.static {
8.System.out.println(p_StaticField);
9.System.out.println("父类--静态初始化块");
10.}
11.// 初始化块
12.{
13.System.out.println(p_Field);
14.System.out.println("父类--初始化块");
15.}
16.// 构造器
17.public Parent() {
18.System.out.println("父类--构造器");
19.}
20.}
21.public class SubClass extends Parent {
22.// 静态变量
23.public static String s_StaticField = "子类--静态变量";
24.// 变量
25.public String s_Field = "子类--变量";
26.// 静态初始化块
27.static {
28.System.out.println(s_StaticField);
29.System.out.println("子类--静态初始化块");
30.}
31.// 初始化块
32.{
33.System.out.println(s_Field);
34.System.out.println("子类--初始化块");
35.}
36.// 构造器
37.public SubClass() {
38.System.out.println("子类--构造器");
39.}
40.// 程序入口
41.public static void main(String[] args) {
42.new SubClass();
43.}
44.}
class Parent {
// 静态变量
public static String p_StaticField = "父类--静态变量";
// 变量
public String p_Field = "父类--变量";
// 静态初始化块
static {
System.out.println(p_StaticField);
System.out.println("父类--静态初始化块");
}
// 初始化块
{
System.out.println(p_Field);
System.out.println("父类--初始化块");
}
// 构造器
public Parent() {
System.out.println("父类--构造器");
}
}
public class SubClass extends Parent {
// 静态变量
public static String s_StaticField = "子类--静态变量";
// 变量
public String s_Field = "子类--变量";
// 静态初始化块
static {
System.out.println(s_StaticField);
System.out.println("子类--静态初始化块");
}
// 初始化块
{
System.out.println(s_Field);
System.out.println("子类--初始化块");
}
// 构造器
public SubClass() {
System.out.println("子类--构造器");
}
// 程序入口
public static void main(String[] args) {
new SubClass();
}
}
运行一下上面的代码,结果马上呈现在我们的眼前:
1.父类--静态变量
2.父类--静态初始化块
3.子类--静态变量
4.子类--静态初始化块
5.父类--变量
6.父类--初始化块
7.父类--构造器
8.子类--变量
9.子类--初始化块
10.子类--构造器
现在,结果已经不言自明了。大家可能会注意到一点,那就是,并不是父类完全初始化完毕后才进行子类的初始化,实际上子类的静态变量和静态初始化块的初始化是在父类的变量、初始化块和构造器初始化之前就完成了。
那么对于静态变量和静态初始化块之间、变量和初始化块之间的先后顺序又是怎样呢?是否静态变量总是先于静态初始化块,变量总是先于初始化块就被初始化了呢?实际上这取决于它们在类中出现的先后顺序。我们以静态变量和静态初始化块为例来进行说明。
同样,我们还是写一个类来进行测试:
Java代码
1.public class TestOrder {
2. // 静态变量
3. public static TestA a = new TestA();
4.
5. // 静态初始化块
6. static {
7. System.out.println("静态初始化块");
8. }
9.
10. // 静态变量
11. public static TestB b = new TestB();
12.
13. public static void main(String[] args) {
14. new TestOrder();
15. }
16.}
17.
18.class TestA {
19. public TestA() {
20. System.out.println("Test--A");
21. }
22.}
23.
24.class TestB {
25. public TestB() {
26. System.out.println("Test--B");
27. }
28.}
Java代码
1.public class TestOrder {
2.// 静态变量
3.public static TestA a = new TestA();
4.// 静态初始化块
5.static {
6.System.out.println("静态初始化块");
7.}
8.// 静态变量
9.public static TestB b = new TestB();
10.public static void main(String[] args) {
11.new TestOrder();
12.}
13.}
14.class TestA {
15.public TestA() {
16.System.out.println("Test--A");
17.}
18.}
19.class TestB {
20.public TestB() {
21.System.out.println("Test--B");
22.}
23.}
public class TestOrder {
// 静态变量
public static TestA a = new TestA();
// 静态初始化块
static {
System.out.println("静态初始化块");
}
// 静态变量
public static TestB b = new TestB();
public static void main(String[] args) {
new TestOrder();
}
}
class TestA {
public TestA() {
System.out.println("Test--A");
}
}
class TestB {
public TestB() {
System.out.println("Test--B");
}
}
运行上面的代码,会得到如下的结果:
1.Test--A
2.静态初始化块
3.Test--B
Remove and reinsert the battery then proceed to step 2. For devices without a removable battery, long press the power key then select restart. Hold down the volume down key while restarting to start the device in Bootloader mode.
Press Volume Down and Power to start the device into Bootloader mode.
Use the Volume buttons to select up or down. Highlight Fastboot and press the Power button.
Connect the device to the computer via a usb cable
On your computer create a new folder (For Example: C:\Android) where we will be putting the following 3 files in this new folder:
- adb.exe
- AdbWinApi.dll
- fastboot.exe
Note: for Mac OS X and Linux you will only need adb and fastboot.
Run the “SDK Manager.exe” which is found in \android-sdk-windows\
Wait about 3 minutes. You will see there are many packages you can update. Please install Android SDK Platform Tools and Android SDK Tools (Please update it if your SDK version is r11).
After you have installed this, you will be able to find the files in the following locations.
To find adb.exe, AdbWinApi.dll, look in \android-sdk-windows\platform-tools
To find fastboot.exe look in \android-sdk-windows\tools
Note: fastboot.exe may be unavailable in the latest Windows Android Tools release. You can extract it from a previous release available here: android-sdk_r13-windows.zip
Mac OS X or Linux users can download the fastboot binary using the following links:
(Unzip the file before executing) fastboot-mac 59071 ed784e50cfcaba3c60380c5f448aa354 Fastboot binary, Linux
(Unzip the file before executing) fastboot 61887 9851bb6ad29cd4b60c9ba9d011ba9efd
Open up command prompt. ( Start > Run > Type CMD,). The window that appears is called Command Prompt.
Navigate to where you unzipped the ZIP file and go to the folder you just created (For Example: If you created the folder in C:\Android, then you would type in Command Prompt: cd c:\Android).
Type in Command Prompt: fastboot oem get_identifier_token.
You will see a long block of text. Copy and paste this text into the the token field below (in the command prompt: Right Click > Mark > highlight the block of text > Right click to copy).
You will see one of the following two screens:
Ref. 9a
Ref. 9b
When copying the token, start with this line:
And end with this line:
(Note: Only copy the highlighted sections above. Do not copy the INFO or (bootloader) prefix)
Paste this string of text into the token field and hit Submit in order to receive your unlock code binary file. You will receive this information in your email.
Example:
You will receive an email with the attachment: Unlock_code.bin. Save this file in the same folder as your fastboot files (For Example: C:\Android).
If, after a short period of time, you have not yet received the mail, or you have received an email from HTC, but does not contain an attached file, it is possible that more stringent spam filters and anti-virus programs will block this mail or attachment.
In order to ensure that you receive the email with the attachment, we suggest you consider using a non-corporate email server as corporate servers tend to have more stringent attachment policies. If you have not received the email at all, please check your spam folder in your email client to check if the email was filtered out.
In the command prompt type: fastboot flash unlocktoken Unlock_code.bin. In the command prompt you will see the following message:
On your phone you will now see the disclaimer. Please read this carefully as this action may void your warranty. Use the Volume buttons to highlight your choice, and the Power button to make your selection. If you select Yes, your phone will be reset to its’ factory default settings, and your bootloader will be unlocked. If you select No, your phone will reboot and no modifications will have been made.
To re-lock your bootloader, in the command prompt, type: fastboot oem lock.
Note: This will not restore the factory default lock, but will simply re-lock the bootloader so no further changes can be made. Furthermore, if you want to unlock your bootloader again, simply repeat step 12 with your original unlock key file to unlock your phone again.