简体   繁体   English

如何在循环中访问对象字段?

[英]How to access object fields in a loop?

I can access the planetName, but not the Surfacematerial,Diameter etc because they are not in the array and in the object. 我可以访问planetName,但不能访问Surfacematerial,Diameter等,因为它们不在数组和对象中。 How do I access the objects in a loop and their respective fields? 如何在循环中访问对象及其各自的字段?

import java.util.Scanner; 导入java.util.Scanner; public class Planet { 公共课Planet {

    private String[] planetName;
    private String SurfaceMaterial;
    private double daysToOrbit;
    private double diameter;




public Planet(){


    planetName=new String[8];
    SurfaceMaterial="";
    daysToOrbit=0;
    diameter=0;





}


public Planet(String[] planetName, String SurfaceMaterial,double daysToOrbit, double diameter){

    this.planetName=planetName;
    this.SurfaceMaterial=SurfaceMaterial;
    this.daysToOrbit=daysToOrbit;
    this.diameter=diameter;




}

public void setPlanetName(){

    Scanner in=new Scanner(System.in);

    Planet solar[]=new Planet[8];
    for(int i=0;i<solar.length;i++){


        solar[i]=new Planet(planetName,SurfaceMaterial,daysToOrbit,diameter);
        System.out.println("Enter Planet Name::");
        planetName[i]=in.next();
        System.out.println("Enter Surface Material");
        SurfaceMaterial=in.next();
        System.out.println("Enter Days to Orbit");
        daysToOrbit=in.nextDouble();
        System.out.println("Enter Diameter");
        diameter=in.nextDouble();
    }

    for(int i=0;i<solar.length;i++){
    System.out.println(planetName[i]);
    System.out.println(this.SurfaceMaterial); //This returns only one value that has been entered at the last
    }


}









}   


    public static void main(String[] args) {

        Planet planet=new Planet();
        Scanner input=new Scanner(System.in);

    planet.setPlanetName();




        }
    }

just access like following 就像下面一样访问

object[index].member ... // or call getter setter

in your case say the first member name is name .. so call like 在您的情况下,请说第一个会员名称是姓名..所以打电话就像

staff[0].name // this will return BOB

The staff array is declared as local in the Constructor: Or if it is declared in the class context, you are hiding it. staff数组在构造函数中声明为局部数组:或者,如果在类上下文中声明,则将其隐藏。 So declare the staff array in the class context and then initialize in the constructor: 因此,在类上下文中声明staff数组,然后在构造函数中进行初始化:

class Test
{
  public Full_time [] Staff;

  public Test()
  {

    Staff = new Full_time [4];

    Staff [0] = new Full_time("BoB", 2000, 70000);
    Staff [1] = new Full_time("Joe", 1345, 50000);
    Staff [2] = new Full_time("Fan", 3000, 80000);

  }
}

And then, in the main function: 然后,在主要功能中:

public static void main(String[] args) {
    Tester t = new Tester();

    t.staff[i].name = "A Name";
}

However, instead of accessing member field directly it is suggested to use getter or setter function like: getStaff(i) and similar. 但是,建议不要直接访问成员字段,而建议使用getter或setter函数,例如: getStaff(i)和类似函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM