2013年11月18日星期一

Please help me look at this little program

I first created a package, and then in the main program can not run . Themselves unable to find an error , please help.

package com.corejava;
import java.util.*;
public class Test2
{
private String name;
private double salary;
private int id;
private static int count = 0;
public Test2(String name, double salary)
{
this.name = name;
this.salary = salary;
count++;
   id = count;
}
}


main program for
import com.corejava. *;
import java.util. *;

public class Test
{
public static void main(String[] args)
{
Employee[] student = new Employee[3];
student[0] = new Employee("abc", 200);
student[1] = new Employee("efg", 300);
student[2] = new Employee("opq", 400);

for(Employee i: student)
{
System.out.println("name = " + i.name + "salary = " + i.salary + ", and the id is " + i.getId());
}
}
}


runtime error is



------ Solution ------------------------------------ --------
Test2 classes have not seen you define getId method , find the normal
------ Solution --------- -----------------------------------
Test2 classes have not seen you define the getId method , find the normal
------ Solution ------------------------------ --------------
getid () that come ?
------ Solution - -------------------------------------------
private property needs with get and set methods to read and write .
------ Solution ---------------------------------------- ----
getId () method
------ Solution --------------------------- -----------------
have a class named Employee Mody
------ Solution - -------------------------------------------


you should take a look at the basis of Java , at least read through the book 's basic syntax and then encounter problems come questions.
your question too basic . This is the public private protected Package of various access permissions problem.
public in all classes which can be referenced ; private referenced only in this category ( note that this class is a subclass of the parent class can not access private methods , only the inner class can access the class private properties and methods of modification ); protected modified method or property can only be accessed by subclasses , other classes can not be accessed . Package can only classes in the same package for a visit.
look book. Your question is too basic , and inappropriate questions come up . Wasting your own points !
------ For reference only -------------------------------------- -
is my own wrong, the help of everyone I finally realized output. 6th floor right in saying that no Employee class should be Test2 class I put the main stickers are restrained .

import com.corejava.*;
import java.util.*;

public class Test
{
public static void main(String[] args)
{
Test2[] student = new Test2[3];
student[0] = new Test2("abc", 200);
student[1] = new Test2("efg", 300);
student[2] = new Test2("opq", 400);

for(Test2 i: student)
{
System.out.println("name = " + i.name + "salary = " + i.salary + ", and the id is " + i.id);
}
}
}


In addition, the package I put all the variables into a public , why can not private it ?
after the reform package for the

package com.corejava;
import java.util.*;
public class Test2
{
 public String name;
 public double salary;
 public int id;
 private static int count = 0;
public Test2(String name, double salary)
{
this.name = name;
this.salary = salary;
count++;
   id = count;
}
}


If the variable is private, there are mistakes

Why ?


------ For reference only ---------------------------------- -----
outside the class can not access private member
------ For reference only ----------------------- ----------------
you know private ( private ) and public ( public ) Why the difference
------ For reference only ---------------------------------------
in everyone's prompt me private and the public has to understand that the problem is very basic , but sometimes , the underlying problem is not necessarily in terms of the book will be able to timely understand . Through constantly practicing constantly asking questions in order to better grasp.

没有评论:

发表评论