理解Java构造器中的"this"
Calling Another Constructor
if the first statement of a constructor has the form this(...), then the constructor calls another constructor of the same class. For example,
public Employee(double s)
{
this("Employee #" + nextId, s);
nextId++;
}
When you call `new Employee(60000), the `Employee(double)` constructor calls the `Employee(String, double)` constructor.
import java.util.*;
/*
- This program demonstrates object construction.
*/
public class ConstructorTest
{
public static void main(String[] args)
{
// fill the staff array with three Employee objects
Employee[] staff = new Employee[3];
staff[0] = new Employee("Harry", 40000);
staff[1] = new Employee(60000);
staff[2] = new Employee();
//print out inofrmation about all Employee objects
for(Employee anEmployee : staff)
{
System.out.println("name =" + anEmployee.getName() + ", id =" + anEmployee.getId() + ", salary="
+ anEmployee.getSalary());
}
}
}
class Employee
{
private static int nextId;
private int id;
private String name = ""; //instance field initialization
private double salary;
//static initialization block
{
Random generator = new Random();
// set nextId to a random number between 0 and 9999
nextId = generator.nextInt(10000);
}
// object initialization block
{
id = nextId;
nextId++;
}
// three overload constructors
public Employee(String n, double s)
{
name = n;
salary = s;
System.out.println("Constructor: Employee(String n, double s) is called.");
}
public Employee(double s)
{
// calls the Employee(String, doulbe)constructor
this("Employee #" + nextId, s);
}
// the default constructor
public Employee()
{
// name initialization to "" --see above
// salary not explicitly set -- initialized to 0
// id initialized in initialization block
System.out.println("The default constructor Employee() is called.");
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public int getId()
{
return id;
}
}
输出结果:
Constructor: Employee(String n, double s) is called.
Constructor: Employee(String n, double s) is called.
The default constructor Employee() is called.
Disconnected from the target VM, address: '127.0.0.1:36707', transport: 'socket'
name =Harry, id =257, salary=40000.0
name =Employee #258, id =4283, salary=60000.0
name =, id =2113, salary=0.0
Process finished with exit code 0
理解Java构造器中的"this"的更多相关文章
- 深入理解Java虚拟机--中
深入理解Java虚拟机--中 第6章 类文件结构 6.2 无关性的基石 无关性的基石:有许多可以运行在各种不同平台上的虚拟机,这些虚拟机都可以载入和执行同一种平台无关的字节码(ByteCode),从而 ...
- 轻松理解 Java开发中的依赖注入(DI)和控制反转(IOC)
前言 关于这个话题, 网上有很多文章,这里, 我希望通过最简单的话语与大家分享. 依赖注入和控制反转两个概念让很多初学这迷惑, 觉得玄之又玄,高深莫测. 这里想先说明两点: 依赖注入和控制反转不是高级 ...
- 垃圾回收相关(深入理解Java虚拟机中的内容)
程序计数器.虚拟机栈和本地方法栈这三个区域属于线程私有的,只存在于线程的生命周期内,线程结束之后也会消失,因此不需要对这三个区域进行垃圾回收.垃圾回收主要是针对 Java 堆和方法区进行. 判断一个对 ...
- 理解Java序列化中的SerialVersionUid
一.前言 SerialVersionUid,简言之,其目的是序列化对象版本控制,有关各版本反序列化时是否兼容.如果在新版本中这个值修改了,新版本就不兼容旧版本,反序列化时会抛出InvalidClass ...
- 理解Java虚拟机中的栈、堆、堆栈
JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 栈区: 每个线程包含一个栈区,栈中只保存方法中(不包括对象的成员变量)的基础数据类型和自定义对象的引用(不 ...
- 深入理解Java中的锁
转载:https://www.jianshu.com/p/2eb5ad8da4dc Java中的锁 常见的锁有synchronized.volatile.偏向锁.轻量级锁.重量级锁 1.synchro ...
- 转:理解Java泛型
JDK 5.0 中增加的泛型类型,是 Java 语言中类型安全的一次重要改进.但是,对于初次使用泛型类型的用户来说,泛型的某些方面看起来可能不容易明白,甚至非常奇怪.在本月的“Java 理论和实践”中 ...
- 深刻理解Java中final的作用(一):从final的作用剖析String被设计成不可变类的深层原因
声明:本博客为原创博客,未经同意,不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(原文链接为http://blog.csdn.net/bettarwang/article/det ...
- 深入理解Java中的不可变对象
深入理解Java中的不可变对象 不可变对象想必大部分朋友都不陌生,大家在平时写代码的过程中100%会使用到不可变对象,比如最常见的String对象.包装器对象等,那么到底为何Java语言要这么设计,真 ...
随机推荐
- H700关闭Direct PD Mapping
Attached Enclosure doesn't support in controller's Direct mapping modePlease contact your system sup ...
- java文件转码
完整项目带lib 参考 http://toyota2006.iteye.com/blog/540316 判断编码 package change; import info.monitorenter.cp ...
- Windowed functions can only appear in the SELECT or ORDER BY clauses
尝试做分页处理 select row_number over (orderby id asc) as rownum,* from table where rownum>=(@page*@page ...
- pve之命令
pvesr list ha-manager status pvecm nodes pvecm status pveperf qm list root@cu-pve06:~# pvesr list Jo ...
- Linux 软件安装到哪里合适,目录详解
文章来源: https://blog.csdn.net/qq_22771739/article/details/83933473 Linux 的软件安装目录是也是有讲究的,理解这一点,在对系统管理是有 ...
- 【转】PyQt5系列教程(七)控件
PyQt5系列教程(七)控件 软硬件环境 Windows 10 Python 3.4.2 PyQt 5.5.1 PyCharm 5.0.4 前言 控件是PyQt应用程序的基石.PyQt5自带很多不 ...
- Linux 初始化系统 systemd - journald 日志
journalctl 中文手册 archlinux - journal systemd-journald 用于检索 systemd 的日志,是 systemd 自带的日志系统. 1. systemd- ...
- Attribute 'num_units' in Tensorflow BasicLSTMCell blocks
在之前使用Tensorflow来做音乐识别时,LSTM给出了非常让人惊喜的学习能力.当时在进行Tuning的时候,有一个参数叫做num_units,字面看来是LTSM单元的个数,但最近当我试图阅读Te ...
- 利用多态,简易实现电脑usb连接设备案例
package cn.learn.Practice03; public interface UsbInterface { void open(); //打开usb void close(); //关闭 ...
- java_第一年_JavaWeb(8)
前面说到,JSP在运行时会被编译成Servlet源代码,通过_jspServlet方法处理请求,此时该方法会传递和提供9个与web开发相关的对象进行使用,开发人员在JSP页面通过对这些变量即可引用这9 ...