• 【Java SE】对象的构造及初始化


    1、利用构造方法初始化

    构造方法(也称为构造器)是一个特殊的成员方法,名字必须与类名相同,在创建对象时,由编译器自动调用,并且在整个对象的生命周期内只调用一次。

    1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Student {
    2. String name;
    3. // 无参构造方法(没有参数的构造方法)
    4. public Student() {
    5. this("张三");
    6. }
    7. // 有参构造方法
    8. public Student(String name) {
    9. this.name = name;
    10. }
    11. // 输出name和age
    12. public void print() {
    13. System.out.println("姓名:" + name);
    14. }
    15. public static void main(String[] args) {
    16. Student stu = new Student();
    17. stu.print();
    18. }
    19. }

    注意:

    构造方法:名字与类名相同,没有返回值类型,设置为void也不行
    一般情况下使用public修饰
    在创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次

    构造方法的作用就是对对象中的成员进行初始化,并不负责给对象开辟空间。

    构造方法的特性:

    1. 名字必须与类名相同
    2. 没有返回值类型,设置为void也不行
    3. 创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次(相当于人的出生,每个人只能出生一次)
    4. 构造方法可以重载(用户根据自己的需求提供不同参数的构造方法)如上面的代码的无参和有参构造方法就构成了重载。

    如果在类中自己没有定义任何构造方法,编译器会默认生成一个不带参数的构造方法。

    但是如果自己定义了构造方法,则系统不会再生成,需要自己构造完善。

    (当我们构造了带参数的构造方法的时候一定要去构造一个不带参数的构造方法,否则会出错)

    另外在构造方法中,可以通过this调用其他构造方法来简化代码

    1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Student {
    2. String name;
    3. // 无参构造方法(没有参数的构造方法)
    4. public Student() {
    5. this("张三");
    6. }
    7. // 有参构造方法
    8. public Student(String name) {
    9. this.name = name;
    10. }
    11. // 输出name和age
    12. public void print() {
    13. System.out.println("姓名:" + name);
    14. }
    15. public static void main(String[] args) {
    16. Student stu = new Student();
    17. stu.print();
    18. }
    19. }

    这里注意:
    1、this(...)必须是构造方法中第一条语句
    2、不能形成循环(也就是说不能在构造方法之间来回循环调用)

    绝大多数情况下使用public来修饰,特殊场景下会被private修饰

    2、利用默认初始化

    我们知道局部变量在使用时必须要初始化,不进行初始化就会报错,而成员变量可以不用呢?

    原因在于对象空间被申请好之后,对象中包含的成员已经设置好了初始值,比如:

    3、就地初始化

    就地初始化就是在定义成员变量的时候直接进行初始化操作:

    1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Student {
    2. String name="张三";
    3. // 输出name和age
    4. public void print() {
    5. System.out.println("姓名:" + name);
    6. }
    7. public static void main(String[] args) {
    8. Student stu = new Student();
    9. stu.print();
    10. }
    11. }

    注意:代码编译完成后,编译器会将所有给成员初始化的这些语句添加到各个构造方法中(这里这个程序是系统默认的构造方法)

  • 相关阅读:
    学习使用Scrapy框架进行高效的爬取,了解其基本结构和使用方法
    【无标题】
    【Python】PySpark 数据计算 ④ ( RDD#filter 方法 - 过滤 RDD 中的元素 | RDD#distinct 方法 - 对 RDD 中的元素去重 )
    行为型模式 - 命令模式
    【软件工程】五、面向对象方法学 & 软件项目管理
    pycharm 快捷键 及 高级设置
    C++:内存管理
    jmeter采集ELK平台海量业务日志( 采用Scroll)
    时间日期类
    Day1_9 Java学习之DQL语言与完整性约束
  • 原文地址:https://blog.csdn.net/Java_ttcd/article/details/126172936