1>概念:将对象引用置于新类中即可,不管是否是基本类型或引用类型

  1. class WaterSource{
  2. private String s;
  3. WaterSource(){
  4. System.out.println("WaterSource");
  5. s = "Constructor";
  6. }
  7. @Override
  8. public String toString() {
  9. return s;
  10. }
  11. }
  12. public class SprinklerSystem {
  13. private String value1,value2,value3,value4;
  14. //在定义的地方进行了初始化
  15. private WaterSource source = new WaterSource();
  16. private int i;
  17. private float f;
  18. @Override
  19. public String toString() {
  20. return "SprinklerSystem{" +
  21. "value1='" + value1 + '\'' +
  22. ", value2='" + value2 + '\'' +
  23. ", value3='" + value3 + '\'' +
  24. ", value4='" + value4 + '\'' +
  25. ", source=" + source +
  26. ", i=" + i +
  27. ", f=" + f +
  28. '}';
  29. }
  30. //在构建对象的时候 何时对组合的数据类型进行初始化,以及初始化的时候如何取值
  31. public static void main(String[] args) {
  32. final SprinklerSystem sprinklerSystem = new SprinklerSystem();
  33. System.out.println(sprinklerSystem);
  34. }
  35. }

2>🤔思考:在何处对组合类型的数据进行初始化

  • 在定义对象的地方。以为着这些对象总能够在构造器调用之前被初始化。
  • 在类的构造器中。
  • 在使用这些对象之前,这种形式成为惰性初始化。
  • 使用实例初始化——————???????????

    1. class Soap {
    2. private String s;
    3. Soap() {
    4. System.out.println("Soap()");
    5. s = "Constructed";
    6. }
    7. @Override
    8. public String toString() {
    9. return s;
    10. }
    11. }
    12. public class Bath {
    13. private String // 1,Initializing at point of definition:
    14. s1 = "Happy",
    15. s2 = "Happy",
    16. s3,
    17. s4;
    18. private Soap castille;
    19. private int i;
    20. private float toy;
    21. //2,在类的构造器中
    22. public Bath() {
    23. System.out.println("Inside Bath()");
    24. s3 = "Joy";
    25. toy = 3.14f;
    26. castille = new Soap();
    27. }
    28. // 普通的代码块 称之为: 4,Instance initialization:(每次new的时候都会执行)
    29. {
    30. i = 47;
    31. }
    32. @Override
    33. public String toString() {
    34. if (s4 == null) // 3,Delayed initialization:
    35. {
    36. s4 = "Joy";
    37. }
    38. return
    39. "s1 = " + s1 + "\n" +
    40. "s2 = " + s2 + "\n" +
    41. "s3 = " + s3 + "\n" +
    42. "s4 = " + s4 + "\n" +
    43. "i = " + i + "\n" +
    44. "toy = " + toy + "\n" +
    45. "castille = " + castille;
    46. }
    47. public static void main(String[] args) {
    48. Bath b = new Bath();
    49. System.out.println(b);
    50. }
    51. }

3>对实例初始化的理解,实例代码块(亦称为普通代码块);类比static代码块理解

  1. public class InstanceInit {
  2. String name;
  3. //只会执行一次
  4. static{
  5. System.out.println("static 代码块");
  6. //init();静态代码块中不能调用普通方法,
  7. }
  8. //普通的代码块 每次new的时候都会执行;代码块中亦可调用普通方法
  9. {
  10. name = "abc";
  11. System.out.println(name+"===========");
  12. init();
  13. }
  14. public void init(){
  15. System.out.println("init");
  16. }
  17. public static void main(String[] args) {
  18. InstanceInit instanceInit1 = new InstanceInit();
  19. InstanceInit instanceInit2 = new InstanceInit();
  20. InstanceInit instanceInit3 = new InstanceInit();
  21. }
  22. }