面的class Employee里面的
public Employee(String n, double s)
{
name = n;
salary = s;
}
其中上面的为什么要n和s
构造函数可以自己写,就相当于写一个函数/**
@version 1.00 2000-01-27
@author Cay Horstmann
*/import java.util.*;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 information about all Employee objects
for (int i = 0; i < staff.length; i++)
{
Employee e = staff[i];
System.out.println("name=" + e.getName()
+ ",id=" + e.getId()
+ ",salary=" + e.getSalary());
}
}
}class Employee
{
// three overloaded constructors
public Employee(String n, double s)
{
name = n;
salary = s;
} public Employee(double s)
{
// calls the Employee(String, double) constructor
this("Employee #" + nextId, s);
} // the default constructor
public Employee()
{
// name initialized to ""--see below
// salary not explicitly set--initialized to 0
// id initialized in initialization block
}
//以上你写了三个构造函数,如果你写类,没有写构造函数,那么系统会默认一个构造函数,就是
不带参数的,什么都不做的构造函数,在这里就相当于第三个构造函数,一但你自己定义了构造
函数,那么系统默认的就没有了,需要你自己再写一遍。
//下面的方法就是你自己想怎么写就怎么写了。通常这些方法用来实现你的算法、访问类的私有
变量,因为类的私有变量(private)只有该类的方法才可以访问 public String getName()
{
return name;
} public double getSalary()
{
return salary;
} public int getId()
{
return id;
} private int id;
private static int nextId; // object initialization block
{
id = nextId;
nextId++;
} // static initialization block
static
{
Random generator = new Random();
// set nextId to a random number between 0 and 9999
nextId = generator.nextInt(10000);
} private String name = ""; // instance variable initialization
private double salary;
}
public Employee(String n, double s)
{
name = n;
salary = s;
}
其中上面的为什么要n和s
构造函数可以自己写,就相当于写一个函数/**
@version 1.00 2000-01-27
@author Cay Horstmann
*/import java.util.*;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 information about all Employee objects
for (int i = 0; i < staff.length; i++)
{
Employee e = staff[i];
System.out.println("name=" + e.getName()
+ ",id=" + e.getId()
+ ",salary=" + e.getSalary());
}
}
}class Employee
{
// three overloaded constructors
public Employee(String n, double s)
{
name = n;
salary = s;
} public Employee(double s)
{
// calls the Employee(String, double) constructor
this("Employee #" + nextId, s);
} // the default constructor
public Employee()
{
// name initialized to ""--see below
// salary not explicitly set--initialized to 0
// id initialized in initialization block
}
//以上你写了三个构造函数,如果你写类,没有写构造函数,那么系统会默认一个构造函数,就是
不带参数的,什么都不做的构造函数,在这里就相当于第三个构造函数,一但你自己定义了构造
函数,那么系统默认的就没有了,需要你自己再写一遍。
//下面的方法就是你自己想怎么写就怎么写了。通常这些方法用来实现你的算法、访问类的私有
变量,因为类的私有变量(private)只有该类的方法才可以访问 public String getName()
{
return name;
} public double getSalary()
{
return salary;
} public int getId()
{
return id;
} private int id;
private static int nextId; // object initialization block
{
id = nextId;
nextId++;
} // static initialization block
static
{
Random generator = new Random();
// set nextId to a random number between 0 and 9999
nextId = generator.nextInt(10000);
} private String name = ""; // instance variable initialization
private double salary;
}
我眼睛花拉,最近看代码就头晕,不知道怎么回事