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

一、变量作用域

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 如发现错误,请不吝赐教!

  • 74
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 109
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 109
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jagochan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值