Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java——多个类_Java_Class_Methods - Fatal编程技术网

Java——多个类

Java——多个类,java,class,methods,Java,Class,Methods,因此,对于我的java编程课程,我想做一些练习。我做了一个关于行星的项目。我得到了4个行星,每个行星都包含信息。我制作了一个“构造器”类行星: public class Planet { String name; int number; String color; int width; public Planet(String name){ this.name = name; } public void Number

因此,对于我的java编程课程,我想做一些练习。我做了一个关于行星的项目。我得到了4个行星,每个行星都包含信息。我制作了一个“构造器”类行星:

public class Planet {

    String name;
    int number;
    String color;
    int width;


    public Planet(String name){
        this.name = name;
    }

    public void Number(int Number){
        number =  Number;
    }

    public void Color(String Color){
        color = Color;
    }

    public void Width(int Width){
        width = Width;
    }

    public void printPlanet(){
        System.out.println("Name:"+ name );
        System.out.println("Number:" + number );
        System.out.println("Color:" + color );
        System.out.println("Width:" + width);
    }
}
以及其他代表行星的这类行星(总数=4):

public class Earth {

    public static void main(String args[]){
        Planet earth = new Planet("Earth");

        earth.Number(2);
        earth.Color("Blue");
        earth.Width(47000);

    }
}

但是现在我想用简单的代码创建一个简单的文件,将所有行星的所有信息一起输出。我知道我可以把来自planets文件的所有代码放在一个文件中,但是太多了,我想要一个包含一个或两个方法/构造函数的简单文件,输出所有信息。谢谢

按照建议对行星类别进行更改

public class Planet {

    private String name;
    private int number;
    private String color;
    private int width;

    public Planet(String name, int number, String color, int width) {
        this.name = name;
        this.number = number;
        this.color = color;
        this.width = width;
    }

    public void Number(int Number) {
        number = Number;
    }

    public void Color(String Color) {
        color = Color;
    }

    public void Width(int Width) {
        width = Width;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Planet [name=" + name + ", number=" + number + ", color="
                + color + ", width=" + width + "]";
    }

}
初始化部分

public static void main(String[] args) {
    Planet earth = new Planet("Earth", 2, "Blue", 47000);
    Planet pluto = new Planet("Pluto", 9, "Blue", 47000);
    Planet mars = new Planet("Mars", 5, "Blue", 47000);
    Planet other = new Planet("Other", 1, "Blue", 47000);
    System.out.println(earth);
    System.out.println(pluto);
    System.out.println(mars);
    System.out.println(other);
}

按照建议对行星类别进行更改

public class Planet {

    private String name;
    private int number;
    private String color;
    private int width;

    public Planet(String name, int number, String color, int width) {
        this.name = name;
        this.number = number;
        this.color = color;
        this.width = width;
    }

    public void Number(int Number) {
        number = Number;
    }

    public void Color(String Color) {
        color = Color;
    }

    public void Width(int Width) {
        width = Width;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Planet [name=" + name + ", number=" + number + ", color="
                + color + ", width=" + width + "]";
    }

}
初始化部分

public static void main(String[] args) {
    Planet earth = new Planet("Earth", 2, "Blue", 47000);
    Planet pluto = new Planet("Pluto", 9, "Blue", 47000);
    Planet mars = new Planet("Mars", 5, "Blue", 47000);
    Planet other = new Planet("Other", 1, "Blue", 47000);
    System.out.println(earth);
    System.out.println(pluto);
    System.out.println(mars);
    System.out.println(other);
}

按照建议对行星类别进行更改

public class Planet {

    private String name;
    private int number;
    private String color;
    private int width;

    public Planet(String name, int number, String color, int width) {
        this.name = name;
        this.number = number;
        this.color = color;
        this.width = width;
    }

    public void Number(int Number) {
        number = Number;
    }

    public void Color(String Color) {
        color = Color;
    }

    public void Width(int Width) {
        width = Width;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Planet [name=" + name + ", number=" + number + ", color="
                + color + ", width=" + width + "]";
    }

}
初始化部分

public static void main(String[] args) {
    Planet earth = new Planet("Earth", 2, "Blue", 47000);
    Planet pluto = new Planet("Pluto", 9, "Blue", 47000);
    Planet mars = new Planet("Mars", 5, "Blue", 47000);
    Planet other = new Planet("Other", 1, "Blue", 47000);
    System.out.println(earth);
    System.out.println(pluto);
    System.out.println(mars);
    System.out.println(other);
}

按照建议对行星类别进行更改

public class Planet {

    private String name;
    private int number;
    private String color;
    private int width;

    public Planet(String name, int number, String color, int width) {
        this.name = name;
        this.number = number;
        this.color = color;
        this.width = width;
    }

    public void Number(int Number) {
        number = Number;
    }

    public void Color(String Color) {
        color = Color;
    }

    public void Width(int Width) {
        width = Width;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Planet [name=" + name + ", number=" + number + ", color="
                + color + ", width=" + width + "]";
    }

}
初始化部分

public static void main(String[] args) {
    Planet earth = new Planet("Earth", 2, "Blue", 47000);
    Planet pluto = new Planet("Pluto", 9, "Blue", 47000);
    Planet mars = new Planet("Mars", 5, "Blue", 47000);
    Planet other = new Planet("Other", 1, "Blue", 47000);
    System.out.println(earth);
    System.out.println(pluto);
    System.out.println(mars);
    System.out.println(other);
}

首先,你需要启动行星的实例。在您的代码中是这样的(使用上述给定的和更新类):

公共类testplants{
公共静态void main(字符串[]args){
行星地球=新行星(“地球”,2,“蓝色”,47000);
行星冥王星=新行星(“冥王星”,9,“蓝色”,47000);
火星=新行星(“火星”,5,“蓝色”,47000);
金星=新行星(“金星”,1,“蓝色”,47000);
//列一张行星清单:(称为行星)
ArrayList行星=新ArrayList();
添加(地球);
行星。加上(冥王星);
添加(火星);
行星。加上(金星);
//现在是魔术:
对于(int i=0;i

现在我希望您了解这是如何工作的;)Java文档和教程太棒了

首先,你需要开始行星的实例。在您的代码中是这样的(使用上述给定的和更新类):

公共类testplants{
公共静态void main(字符串[]args){
行星地球=新行星(“地球”,2,“蓝色”,47000);
行星冥王星=新行星(“冥王星”,9,“蓝色”,47000);
火星=新行星(“火星”,5,“蓝色”,47000);
金星=新行星(“金星”,1,“蓝色”,47000);
//列一张行星清单:(称为行星)
ArrayList行星=新ArrayList();
添加(地球);
行星。加上(冥王星);
添加(火星);
行星。加上(金星);
//现在是魔术:
对于(int i=0;i

现在我希望您了解这是如何工作的;)Java文档和教程太棒了

首先,你需要开始行星的实例。在您的代码中是这样的(使用上述给定的和更新类):

公共类testplants{
公共静态void main(字符串[]args){
行星地球=新行星(“地球”,2,“蓝色”,47000);
行星冥王星=新行星(“冥王星”,9,“蓝色”,47000);
火星=新行星(“火星”,5,“蓝色”,47000);
金星=新行星(“金星”,1,“蓝色”,47000);
//列一张行星清单:(称为行星)
ArrayList行星=新ArrayList();
添加(地球);
行星。加上(冥王星);
添加(火星);
行星。加上(金星);
//现在是魔术:
对于(int i=0;i

现在我希望您了解这是如何工作的;)Java文档和教程太棒了

首先,你需要开始行星的实例。在您的代码中是这样的(使用上述给定的和更新类):

公共类testplants{
公共静态void main(字符串[]args){
行星地球=新行星(“地球”,2,“蓝色”,47000);
行星冥王星=新行星(“冥王星”,9,“蓝色”,47000);
火星=新行星(“火星”,5,“蓝色”,47000);
金星=新行星(“金星”,1,“蓝色”,47000);
//列一张行星清单:(称为行星)
ArrayList行星=新ArrayList();
添加(地球);
行星。加上(冥王星);
添加(火星);
行星。加上(金星);
//现在是魔术:
对于(int i=0;i

现在我希望您了解这是如何工作的;)Java文档和教程太棒了

你不能“创造”信息,也不能拥有比行星更少的构造器。但是,您可以在
行星
内部创建一些
行星
静态
实例(是的,您可以这样做),并将所有现有行星保存在那里。(即静态行星水星、静态行星金星等)。然后只访问它们。您可以在类中创建静态Arraylist成员,构造函数将使用该成员存储每个创建的实例。然后你可以有一个静态的方法,它将写入Arraylist并为每个记录调用printPlanet。好的,我明白了,但是为了在新文件中使用它们,这些“静态”实例下应该有什么语句呢?交叉发布:你不能“创建”信息,而且构造函数比要讨论的Planet要少。但是,您可以在
行星
内部创建一些
行星
静态
实例(是的,您可以这样做),并将所有现有行星保存在那里。(即静态行星水星、静态行星金星等)。然后只访问它们。您可以在类中创建静态Arraylist成员,构造函数将使用该成员存储每个创建的实例。这个