文章目录

二、构造方法(Constructor)

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

(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 theirtypes. 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 thesuperclass(超类). 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 ofObject, 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);

Java 变量作用域、构造方法官方教程相关推荐

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

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

  2. 《java基础知识》Java变量作用域

    对于在作用域里定义的变量,作用域同时决定了它的"可见性"以及"存在时间".在JAVA里,作用域是由花括号的位置决定的.JAVA用一对大括号作为语句块的范围,称为 ...

  3. java变量作用域Scope

    一.变量Scope 作用域同时决定了它的"可见性"以及"存在时间".在C,C++和Java里,作用域是由花括号的位置决定的.变量的作用域分为四个级别:类级.对象 ...

  4. JAVA - 变量作用域

    对于在作用域里定义的变量,作用域同时决定了它的"可见性"以及"存在时间".在JAVA里,作用域是由花括号的位置决定的.JAVA用一对大括号作为语句块的范围,称为 ...

  5. java 变量作用域 c语言_C语言深入理解 - 常量与变量

    ++++++++++++++++++++++++++++++++++++++++++ <C语言深入理解系列 - 常量与变量> 查看其它博文请关注原创作者. 本文系本站原创,欢迎转载! 转载 ...

  6. java 变量作用域 c语言_java - 数据结构 c语言 作用域问题

    问题用//标出来了,同样的语句为什么放入循环结果就完全不同了? #include #include #define OK 1 #define NO 0 int nn=0; typedef struct ...

  7. java编程入门pdf_Java 8编程入门官方教程(第6版) [(美)Schildt H.] 中文完整pdf扫描版[233MB]...

    Java8编程入门官方教程(第6版)针对新版JavaSE8对内容进行了全面更新.在畅销书作者Herbert Schildt(施密特)的帮助下,可以即刻开始学习Java程序设计的基础知识.<Jav ...

  8. Java官方教程(七-3)Using Objects 使用对象(2021.4.10)

    前言 本文是橙子出于兴趣爱好对Java官方教程的尝试翻译,几乎每日更新,感兴趣的朋友可以关注一下橙子:翻译过程中尽可能多的对一些关键词保留了英文原文,如果你想看最纯正的英文原版教材却又看不懂,可以试着 ...

  9. 【Java基础系列教程】第三章 Java变量与运算符

    一.Java程序基本结构 1.1 基本结构 Java程序基本结构示例代码: /** * 这里是文档注释 * 这是一个HelloWorld程序 */ public class HelloWorld {p ...

最新文章

  1. HTTP POST GET 区别详解
  2. 编译安装mysql带boost_编译安装mysql时报缺少boost1.59后,编译安装boost1.59
  3. SSH-KeyGen 的用法 【转载】
  4. Linux 常見的登錄檔檔名
  5. 《学习OpenCV》课后习题解答(第三章)(仅供参考)
  6. java中Jackson_java 中的好东西 jackson
  7. 从零开始学习音视频编程技术(七) FFMPEG Qt视频播放器之SDL的使用
  8. oracle update 改为 merge
  9. 利用Python系统性爬取微博评论
  10. CSRF跨站请求伪造漏洞修复方案
  11. easyui表格 序号如何进行自适应宽度
  12. 智商情商哪个重要_智商和情商哪个更重要 一辩辩词
  13. 字符串处理工具类,主要是针对内容txt对标点符号进行处理-java处理字符串符号工具类实现逻辑
  14. 自动化测试之数据驱动测试
  15. rdkit smiles支持和扩展
  16. Word控件Spire.Doc 【图像形状】教程(11): 如何在 C# 中为 Word 中的图像设置 Transeperant 颜色
  17. 高性价比WIFI图传方案快速入门教程
  18. Using the Scheduler Application - JDE Scheduler Job 原理
  19. c++ string assign和operator=
  20. C语言 半加器与全加器 详解

热门文章

  1. CAN总线基础知识(一)
  2. Linux中文件搜索,查找,读取
  3. inter cpu 测试软件,Intel官方CPU检测工具
  4. HuaWei(手机)瘦身
  5. Python代码实现飞机大战
  6. h5邮件的邮箱 支持_GitHub - cqjsqh/email: h5邮箱地址汇总
  7. 计算机等级考试二级 Python 语言程序设计考试大纲(2022 版)
  8. SAPトランザクション一覧(メモ)
  9. opcache php7,让子弹飞~利用 OPcache 扩展提升 PHP7 性能 | Laravel 篇
  10. 稳定婚配问题的所有可能解