将值设置为对象数组 [英] Setting values to an Array of objects

查看:104
本文介绍了将值设置为对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修订Java书籍,以确保我对对象和Java基本知识有扎实的了解。我偶然发现正在阅读的书中的这段代码头首先:Java第二版(2005)

I'm revising my Java book to make sure i have solid understanding of objects and Java basics in general. I stumbled upon this piece of code from the book i was reading Head First: Java 2nd edition (2005)

 class Book {   

     String title;
     String author; 

 }

 class Main {

    public static void main(String args[]) {    

        int x = 0;      

        Book[] myBooks = new Book[3];
        myBooks[0] = new Book();
        myBooks[1] = new Book();
        myBooks[2] = new Book();
        myBooks[0].title = "Example title xx";
        myBooks[1].title = "Example title cc";
        myBooks[2].title = "Example title yy";
        myBooks[0].author = "Example author xx";
        myBooks[1].author = "Example author cc";
        myBooks[2].author = "Example author yy";    

        while (x < 3) {     

            System.out.print(myBooks[x].title);
            System.out.print(", author ");
            System.out.println(myBooks[x].author);
            x = x + 1;  

        }       
    }   
}

我不我不太了解 myBooks [0] .title =示例标题xx
的语法我承认我对数组及其工作原理并不熟悉但是,遍历数组并使用setter方法设置所有对象字段不是更好的做法吗?

I don't quite understand the syntax of myBooks[0].title = "Example title xx" I admit i'm not quite experienced with arrays and how they work yet, but isn't it a better practice to loop through the array and set all the object fields with setter methods?

我认为这里是这种情况

根据我的有限理解,这种为这些字段分配值的特定方法与这两个类的范围有关。用相同的方法使用静态方法的名称,而不是先创建它们各自类的对象,而是使用静态变量。

From my limited understanding, This particular method of assigning values to those fields is related to the scope of those two classes. The same way you'd use names of the static methods instead of first creating object of their respective classes, but with static variables instead.

这似乎很琐碎,但是对我来说,理解和把握这个想法非常重要。

It seems very trivial, but it's very important for me to understand and grasp the idea. I hope you can clear it up for me.

推荐答案

首先,让我们在不使用数组的情况下重写程序:

First, let's rewrite your program without an array:

Book myBooks0, myBooks1, myBooks2;
myBooks0 = new Book();
myBooks1 = new Book();
myBooks2 = new Book();
myBooks0.title = "Example title xx";
myBooks1.title = "Example title cc";
myBooks2.title = "Example title yy";
myBooks0.author = "Example author xx";
myBooks1.author = "Example author cc";
myBooks2.author = "Example author yy";  

如果您了解引用cusom类的Java变量,那么这根本就不是谜。

If you understand Java variables referencing cusom classes, this should be no mystery at all.

接下来,让我们回到您的数组:

Next, let's go back to your array:

Book[] myBooks = new Book[3];

它将上面的三个命名变量替换为一个数组变量,因此现在不再编写 myBooksX 我们编写 myBooks [X]

It replaces three named variables above with one array variable, so now instead of writing myBooksX we write myBooks[X].

这是两者之间最主要的区别他们俩。数组的优点是 X 可以是变量或整数表达式,其结果为0、1或2,而对于单个变量,该数字必须为硬编码

That's most of the difference between the two. The advantage of an array is that X can be a variable or an integer expression that evaluates to 0, 1, or 2, while with individual variables the number must be hard-coded.

这篇关于将值设置为对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆