基本是按照JAVA核心技术P113 输入的。。可是搞不懂了:(
public class StaticTest
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];
staff[0] = Employee("xdream",100);
staff[1] = Employee("xdream1",200);
staff[2] = Employee("xdream2",300);
//print all
for (int i = 0 ; i <staff.length ; i ++ )
{
Employee e = new staff[i];
e.setid();
System.out.println("name="+e.getName+"id="+e.getId()+"Salary="+e.getSalary()); }
int n = Employee.getNextId();
System.out.println("NextId is "+n); }
}class Employee
{
public Employee(String n,double s)
{
name = n;
Salary = s;
id = 0;
}
public String getName()
{
return name;
}
public double getSalary()
{
return Salary;
}
public int getId()
{
return id;
}
public void setid()
{
id = nextId;
nextId++;
} public static int getNextId()
{
return nextId;
}
private int id;
private String name;
private double Salary;
private static int nextId = 1;}
public class StaticTest
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];
staff[0] = Employee("xdream",100);
staff[1] = Employee("xdream1",200);
staff[2] = Employee("xdream2",300);
//print all
for (int i = 0 ; i <staff.length ; i ++ )
{
Employee e = new staff[i];
e.setid();
System.out.println("name="+e.getName+"id="+e.getId()+"Salary="+e.getSalary()); }
int n = Employee.getNextId();
System.out.println("NextId is "+n); }
}class Employee
{
public Employee(String n,double s)
{
name = n;
Salary = s;
id = 0;
}
public String getName()
{
return name;
}
public double getSalary()
{
return Salary;
}
public int getId()
{
return id;
}
public void setid()
{
id = nextId;
nextId++;
} public static int getNextId()
{
return nextId;
}
private int id;
private String name;
private double Salary;
private static int nextId = 1;}
public static void main(String[] args)
{
Employee[] staff = new Employee[3];
staff[0] = new Employee("xdream",100);//没有用new
staff[1] = new Employee("xdream1",200);
staff[2] = new Employee("xdream2",300);
//print all
for (int i = 0 ; i <staff.length ; i ++ )
{
Employee e = staff[i];//多了new
e.setid();
System.out.println("name="+e.getName()+"id="+e.getId()+"Salary="+e.getSalary());//e.getName,函数少了括号 }
int n = Employee.getNextId();
System.out.println("NextId is "+n); }
}class Employee
{
public Employee(String n,double s)
{
name = n;
Salary = s;
id = 0;
}
public String getName()
{
return name;
}
public double getSalary()
{
return Salary;
}
public int getId()
{
return id;
}
public void setid()
{
id = nextId;
nextId++;
} public static int getNextId()
{
return nextId;
}
private int id;
private String name;
private double Salary;
private static int nextId = 1;}
public static void main(String[] args) {
Employee[] staff = new Employee[3];
staff[0] = new Employee("xdream",100);
staff[1] = new Employee("xdream1",200);
staff[2] = new Employee("xdream2",300);//这里没有new
//print all
for (int i = 0 ; i <staff.length ; i ++ ){
Employee e = staff[i];//这里多了个new
e.setid();
System.out.println("name="+e.getName()+"id="+e.getId()+"Salary="+e.getSalary());//这里的getName少了个括号! }
int n = Employee.getNextId();
System.out.println("NextId is "+n); }
}class Employee{
private int id;
private String name;
private double Salary;
private static int nextId = 1; public Employee(String n,double s){
name = n;
Salary = s;
id = 0;
}
public String getName(){
return name;
}
public double getSalary(){
return Salary;
}
public int getId(){
return id;
}
public void setid(){
id = nextId;
nextId++;
} public static int getNextId(){
return nextId;
}
}
Employee e = staff[i];//多了new
为什么这里不用new呢?
Employee e = staff[i];//多了new
为什么这里不用new呢?
//怎么可以加new了??
我稍微的看了下staff[]已经是一个实例化了的啊 !
staff[i]都已经 又具体的值了的阿 !
你在new.........