“Objets de tableau Java” Réponses codées

tableau de classe Java d'objets

public class MainClass
{  
    public static void main(String args[])
    {
        System.out.println("Hello, World!");
        //step1 : first create array of 10 elements that holds object addresses.
        Emp[] employees = new Emp[10];
        //step2 : now create objects in a loop.
        for(int i=0; i<employees.length; i++){
            employees[i] = new Emp(i+1);//this will call constructor.
        }
    }
}

class Emp{
    int eno;
    public Emp(int no){
        eno = no;
        System.out.println("emp constructor called..eno is.."+eno);
    }
}
Witty Wildebeest

Objets de tableau Java

class Police
{
	public int id;
	public String name;
	Police(int id, String name)
	{
		this.id = id;
		this.name = name;
	}
}
class PoliceClass
{
	public static void main (String[] args)
	{
		
		Police[] array;
		array = new Police[4];
		array[0] = new Police(3242,"Peter");
		array[1] = new Police(4353,"Adil");
		array[2] = new Police(6433,"Preet");
		array[3] = new Police(3434,"Ranjeet");

		for (int i = 0; i < array.length; i++)
			System.out.println("Police array Element at " + i + " : " +
						array[i].id +" "+ array[i].name);

}
}
Unsightly Unicorn

Réponses similaires à “Objets de tableau Java”

Questions similaires à “Objets de tableau Java”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code