如何从一个不同的类中检索一个数组到另一个类中

huangapple go评论76阅读模式
英文:

how to retrieve an array into one class from a different class

问题

我有一个类,在其中创建了一个行星数组。

private final static Planet[] planets = {
new Planet("Mercury"),
new Planet("Venus"),
new Planet("Earth"),
new Planet("Mars"),
new Planet("Jupiter"),
new Planet("Saturn"),
new Planet("Uranus"),
new Planet("Neptune")
};

然后我需要在另一个类中检索它。

public float getPlanetMass() {
// 在这里进行操作以获取行星质量
}

我应该如何实现这个操作呢?
英文:

I have a class where I made an array of the planets.

private final static Planet[]		
	{
		new Planet("Mercury"),
		new Planet("Venus"),
		new Planet("Earth"),
		new Planet("Mars"),
		new Planet("Jupiter"),
		new Planet("Saturn"),
		new Planet("Uranus"),
		new Planet("Neptune")
		
	};

and I need to retrieve it into another class.

public float getPlanetMass()
	{
		
		
	}

How exactly would I go about this?

答案1

得分: 1

你可以在类中拥有一个公共方法来返回行星,并且另外一个类将调用该方法以获取该列表。类似下面这样:

public class Test {

    private Planet[] getPlanets(){
        Planet[] planets = new Planet[8];
        int index = 0;
        for(String name : Arrays.asList("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune")) {
            planets[index++] = new Planet(name);
        }
        return planets;
    }
    
    static class PlanetClient{
        public static void main(String args[]) {
            Test test = new Test();
            Planet[] planets = test.getPlanets();
            System.out.println("Planets are " + Arrays.toString(planets));
        }
    }
    
    class Planet {
        String name;
        Planet(String name){
            this.name = name;
        }
        
        @Override
        public String toString() {
            return this.name;
        }
    }
}
英文:

You can have a public method in a class to return the planets and another class that will call that method to get the list. Something like below :

public class Test {

	private Planet[] getPlanets(){
		Planet[] planets = new Planet[8];
		int index = 0;
		for(String name : Arrays.asList("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune")) {
			planets[index++] = new Planet(name);
		}
		return planets;
    }
	
	static class PlanetClient{
		public static void main(String args[]) {
			Test test = new Test();
			Planet[] planets = test.getPlanets();
			System.out.println("Planets are " + Arrays.toString(planets));
		}
	}
	
	class Planet {
		String name;
		Planet(String name){
			this.name = name;
		}
		
		@Override
		public String toString() {
			return this.name;
		}
	}

}

</details>



# 答案2
**得分**: 0

有许多方法可以做到这一点,但与其创建一个数组,不如创建一个名为`Planet`的`Enum`,甚至是一个名为`StandardPlanet`或`TerrestrialPlanet`的枚举,它实现了一个名为`Plant`的接口。这样,枚举使用了多例模式,您可以公开引用这些元素,而且不会创建非私有可变对象。

```java
public interface Planet {

}
public enum TerrestrialPlanet implements Planet {
    EARTH,
    MARS,
    // 等等
}
Planet planet = TerrestrialPlanet.EARTH;
英文:

There are a lot of ways to do this but instead of creating an array you could create an Enum called Planet or even an enum called StandardPlanet or TerrestialPlanet that implements an interface called Plant. This way the enum uses the multiton pattern, you can reference these elements publicly, and you're not creating non-private mutable objects.

public interface Planet {

}
public enum TerrestialPlanet implements Plant {
    EARTH,
    MARS,
    // etc
}
Planet planet = TerrestialPlanet.EARTH;

huangapple
  • 本文由 发表于 2020年8月29日 07:49:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63642126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定