zoukankan      html  css  js  c++  java
  • 理解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
    
  • 相关阅读:
    ASP.NET中的状态保持(转载)
    Win7系统下VS2008安装SP1补丁解决JQuery无智能提示的问题
    合并多个List<T>类型并通过LINQ按指定属性排序
    ASP.NET状态保持:ViewState
    第一部分 CLR基础:第1章 CLR的执行模型
    Oracle存储过程知识汇总
    ASP.NET中Button控件的CommandName和CommandArgument属性用法
    PL/SQL Developer使用
    ASP.NET常用代码汇总
    PL/SQL Developer中文注释乱码的解决办法
  • 原文地址:https://www.cnblogs.com/yaos/p/14014297.html
Copyright © 2011-2022 走看看