当前位置: 首页 > news >正文

11、Java 变量作用域、构造方法官方教程

文章目录

  • 一、变量作用域
  • 二、构造方法(Constructor)
    • (1) 官方教程解释构造方法
    • (2) 构造方法概念
    • (3) this 的本质 ☆☆☆☆☆
  • 三、对象创建流程分析

一、变量作用域

Java 中的变量有3种:
📖 ① 全局变量:被定义在类中(成员变量
📖 ② 局部变量:被定义在成员方法、代码块、静态代码块中定义的变量
📖 ③ 参数:方法声明中的变量


There are several kinds of variables(变量):

📋 Member variables(成员变量) in a class:these are called fields(属性)
📋 Variables in a method or block of code(代码块):these are called local variables(局部变量)
📋 Variables in method declarations(方法声明):these are called parameters(参数)


✏️ 全局变量(成员变量)作用域为:整个类体
✏️ 局部变量(除全局变量之外的变量)作用域为:它所在的代码块
✏️ 全局变量可以不赋值,直接使用(全局变量有默认值)
✏️ 局部变量必须赋值后才能使用
✏️ 参数的值在方法被调用的时候才有

public class VariableDomain {
    // 全局变量
    private String name;

    {
        int age = 10; // 局部变量
    }

    static {
        double pai = 3.14; // 局部变量
    }

    // num1、description 参数
    public void test(int num1, String description) {
        String hobby = "睡觉"; // 局部变量
    }
}

📜 全局变量名和局部变量名可以一样,访问的时候遵循就近原则
📜 在同一作用域中(eg:同一成员方法中),不能有重名的局部变量
📜 同一类的同一代码块中的成员变量也不能重名

全局变量和局部变量重名的时候,访问遵循就近原则:

public class VariableDomain {
    // 全局变量
    private String name = "张浩男";

    public static void main(String[] args) {
        VariableDomain domain = new VariableDomain();
        domain.test();
    }

    private void test() {
        String name = "莫松"; // 局部变量(可以和全局变量重名)
        // output: name = 莫松
        System.out.println("name = " + name);
    }
}

不同类的成员变量可以重名:
在这里插入图片描述

📜 全局变量(成员变量)的生命周期长:【对象就像一个人🙍,成员变量就像一个人的手👋】全局变量的生命伴随着对象的存在而存在,便随着对象的销毁而销毁
📜 局部变量生命周期短:与它所在的代码块共生
📜 全局变量可以在本类或其他类中使用
📜 局部变量只能在它所在类的指定方法中使用
📜 全局变量可以被修饰符修饰(eg:private、static、final)
📜 局部变量不能被修饰符修饰

二、构造方法(Constructor)

(1) 官方教程解释构造方法

📝 A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations:except that they use the name of the class and have no return type.
📝 ① 中包含构造方法(可通过调用构造方法从一个类模板中创建一个对象)
📝 ② 声明构造方法和声明成员方法一样,但也有区别(✒️构造方法的方法名和类名一模一样;✒️构造方法没有返回值类型)

下面是一个构造方法的例子:

public class Student {
    private String name;
    private int age;
    private double score;

    // (1) 没有返回类型
    // (2) 方法名和类名一致
    public Student(String stuName, int stuAge, double stuScore) {
        // 构造方法的方法体中一般给成员变量赋值
        name = stuName;
        age = stuAge;
        score = stuScore;
    }
}

📝 To create a new Student object called tom, a constructor is called by the new operator.
📝 要想创建一个名为 tom 的 Student 对象,可通过 new 运算符调用构造方法
new 运算符调用构造方法,创建 Student 对象:

Student zhn = new Student("张浩男", 12, 99.5); 

📝 new Student("张浩男", 12, 99.5): creates space in memory(内存空间) for the object and initializes its fields
📝 new Student("张浩男", 12, 99.5): 该代码在内存中为对象开辟了内存空间,并且初始化了成员变量的值

📝 Although Student only has one constructor, it could have others, including a no-argument constructor(无参构造方法):

    /**
     * Student 类的无参构造方法
     */
    public Student() {
        name = "庆医";
        age = 8;
        score = 100;
    }

📝 和成员方法一样,构造方法也是可以重载(Override)的:方法名和类名一致,参数列表各不相同


📝 Both constructors could have been declared in Student because they have different argument lists(参数列表). As with methods(与方法一样), the Java platform differentiates(区分) constructors on the basis of the number of arguments in the list and their types. You cannot write two constructors that have the same number and type of arguments for the same class, because the platform would not be able to tell(区分) them apart. Doing so causes a compile-time error.
📝 两个构造方法(有参构造方法和无参构造方法)都可在 Student 类中被声明,因为它们有不同的参数列表。与成员方法一样,Java 平台可通过参数数量和参数类型来区分构造方法。在同一个类中,你不能写两个拥有相同参数数量和参数类型的构造方法,因为 Java 平台无法区分。写两个拥有相同参数数量和参数类型的构造方法将会导致编译时错误
在这里插入图片描述

💡 You don’t have to provide any constructors for your class, but you must be careful when doing this. The compiler(编译器) automatically provides a no-argument, default constructor(默认构造方法) for any class without constructors. This default constructor will call the no-argument constructor of the superclass(超类). In this situation, the compiler will complain(抱怨、埋怨、发恼骚) if the superclass(超类) doesn’t have a no-argument constructor so you must verify(验证) that it does. If your class has no explicit(明确的、显式的) superclass, then it has an implicit (隐式的、含蓄的)superclass of Object, which does have a no-argument constructor.
💡 你并不是必须得給你的类提供一个构造方法(你可以不同你的类提供任何构造方法)。若不给类提供任何构造方法的话,你必须当心一点。编译器会自动提供一个无参的默认构造方法,这个默认的无参构造方法是提供给那些没有任何构造方法的类的。这个默认的无参构造方法将会调用超类的无参构造方法,所以必须确保超类拥有无参构造方法,否则编译器会发恼骚(报错的)😡。如果你的类没有明确的超类,你的类会有一个隐式的超类(Object),Object 类是所有类的超类(是 Java 平台提供的),Object 类百分百拥有一个无参构造方法。

💚 You can use access modifiers(访问修饰符) in a constructor’s declaration to control which other classes can call the constructor.
💚 你可以在声明构造方法的使用使用访问修饰符(public、protected、private),用以控制哪些其他类可以调用这个构造方法。

💜 If another class cannot call a Student constructor, it cannot directly(直接地) create Student objects.
💜 如果其他类不能调用 Student 类的构造方法,其他类就无法直接地创建 Student 对象。

(2) 构造方法概念

🍀 构造方法:也叫构造器,用于更方便地创建一个对象
🍀 方法名必须和类名一模一样
🍀 没有返回值类型
🍀 可以重载(方法名一样,方法签名不一样)

构造方法案例:

public class Handsome {
    private int age;
    private double weight;

    public Handsome() {

    }

    public Handsome(int age) {
        this.age = age;
    }

    public Handsome(int age, double weight) {
        this.age = age;
        this.weight = weight;
    }
}

使用构造方法创建 Handsome 对象:

	// 可通过3种构造方法创建 Handsome 对象
	Handsome hs1 = new Handsome();
	Handsome hs2 = new Handsome(17);
	Handsome hs3 = new Handsome(17, 123.5);

(3) this 的本质 ☆☆☆☆☆

🍀 this指向当前对象的引用

this 的用途:
🍀 ① 访问当前类中定义的成员变量
🍀 ② 调用当前类中定义的方法(包括构造方法)

public class Computer {
    private String brand;
    private double price;

    public Computer(String brand) {
        // this 作用:调用当前类中定义的方法(包括构造方法)
        this(brand, 0.0);
    }

    public Computer(String brand, double price) {
        // this 作用:访问当前类中定义的成员变量
        this.brand = brand;
        this.price = price;
    }
}

🍀 在类中直接写成员变量、直接调用成员方法,默认都是访问和调用当前对象的

public class Cat {
    public int age;

    public void eat() {
        // 等价于:System.out.println(this.age + "eat()");
        System.out.println(age + "eat()");
    }

    public void run() {
        // 等价于:this.eat();
        eat();
    }
}

💙 this 的本质是一个隐藏的、位置最靠前的方法参数。(面向对象的语言的 this 都是这样的)
在这里插入图片描述


🌱 只能在构造方法中用 this 调用其他构造方法
🌱 如果在构造方法 A 中调用了其他构造方法,调用构造方法的语句必须是构造方法 A 中的第一条语句
在这里插入图片描述


🌼 默认构造方法(Default Constructor):如果一个类没有自定义构造方法,编译器会自动为它提供无参数的默认构造方法
🌼 一旦自定义了构造方法,默认构造方法就不再存在

三、对象创建流程分析

public class Person {
    private int age = 17;
    private String name;

    public Person(String name, int age) {
        this.age = age;
        this.name = name;
    } 
}
	Person zhn = new Person("张浩男", 18);

对象创建流程:
🥤 ① 方法区加载一次类信息
🥤 ② 在堆中分配对象的内存空间
🥤 ③ 完成对象属性初始化(属性默认初始化;显示初始化;构造器初始化)
🥤 ④ 对象在堆中的内存地址赋值给 zhn(zhn 指向对象在堆中的地址)
在这里插入图片描述

Bye-Bye 如发现错误,请不吝赐教!

相关文章:

  • NumPy的常用函数
  • 字符串常量池与StringBuilder
  • 时间序列中的特征选择:在保持性能的同时加快预测速度
  • 谷粒商城 (九) --------- 前端基础 Vue 篇
  • MySQL锁机制详解-表锁与行锁
  • mysql行锁,表锁,间隙锁
  • 【GEE笔记11】数值Number(常见指令方法4)
  • Linux_进程概念
  • 【PAT乙】2022秋季赛后总结
  • nginx + tomcat 搭建负载均衡、动静分离(tomcat多实例)
  • CSRF(跨站请求伪造)攻击和预防
  • 【都 Java17 了,还不了解 Java 8 ? 】一文带你深入了解 Java 8 新特性
  • 解决国产机SVN连接失败的问题
  • MySQL 存储过程创建指定表结构
  • 我们这样的人
  • .pyc 想到的一些问题
  • 【附node操作实例】redis简明入门系列—字符串类型
  • Docker入门(二) - Dockerfile
  • Gradle 5.0 正式版发布
  • interface和setter,getter
  • tab.js分享及浏览器兼容性问题汇总
  • Travix是如何部署应用程序到Kubernetes上的
  • Vue学习第二天
  • 订阅Forge Viewer所有的事件
  • 对超线程几个不同角度的解释
  • 分类模型——Logistics Regression
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 京东美团研发面经
  • 使用 Docker 部署 Spring Boot项目
  • 事件委托的小应用
  • 格斗健身潮牌24KiCK获近千万Pre-A轮融资,用户留存高达9个月 ...
  • 交换综合实验一
  • ​如何在iOS手机上查看应用日志
  • # include “ “ 和 # include < >两者的区别
  • # 计算机视觉入门
  • #define、const、typedef的差别
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • #单片机(TB6600驱动42步进电机)
  • (39)STM32——FLASH闪存
  • (day 12)JavaScript学习笔记(数组3)
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (二)JAVA使用POI操作excel
  • (附源码)python房屋租赁管理系统 毕业设计 745613
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (考研湖科大教书匠计算机网络)第一章概述-第五节1:计算机网络体系结构之分层思想和举例
  • (一)为什么要选择C++
  • ***详解账号泄露:全球约1亿用户已泄露
  • ..thread“main“ com.fasterxml.jackson.databind.JsonMappingException: Jackson version is too old 2.3.1
  • ./configure、make、make install 命令
  • .NET 4.0网络开发入门之旅-- 我在“网” 中央(下)
  • .NET 5种线程安全集合
  • .NET Standard 的管理策略
  • .NET 中什么样的类是可使用 await 异步等待的?
  • .NET中的Event与Delegates,从Publisher到Subscriber的衔接!
  • /*在DataTable中更新、删除数据*/