≈佐

—— 有恒则成。

统计

最新评论

特性论述:重载构造器、默认构造函数、对象初始化、静态初始化、实例域初始化

import java.util.*;
public class ConstructorTest
{
    
public static void main(String[] args)
    {
        Employee[] staff
=new Employee[3];
        
        staff[
0]=new Employee("Harry",40000);
        staff[
1]=new Employee(6000);
        staff[
2]=new Employee();
        
        
for(Employee e:staff)
            System.out.println(
"name=" + e.getName()
                    
+",id=" + e.getId()
                    
+",salary="+e.getSalary());
    }
}

class Employee
{
    
public Employee(String n,double s)
    {
        name
=n;
        salary
=s;
    }
    
public Employee(double s)
    {
        
this("Employee #"+nextId,s);
    }
    
public Employee()
    {
    }
    
public String getName()
    {
        
return name;
    }
    
public double getSalary()
    {
        
return salary;
    }
    
public int getId()
    {
        
return id;
    }
    
private static int nextId;
    
private int id;
    
private String name="";
    
private double salary;
    
    
static
    {
        Random generator
= new Random();
        nextId
=generator.nextInt(10000);
    }
    {
        id
=nextId;
        nextId
++;
    }
}

posted on 2009-01-08 00:05 ≈佐 阅读(216) 评论(0)  编辑  收藏 所属分类: J2SE练习册