Java中自定义对象数组的自定义方法

时间:2022-05-02 16:32:29

I am trying to create an array of type Library, where I can store many objects of type Library that I'll manage later. Before I get too deep, I am trying to make a print() method on the array so that I can simply call myLibrary.print() to print the array.

我正在尝试创建一个类型库的数组,我可以在其中存储我将在稍后管理的许多类型库的对象。在我变得太深之前,我试图在数组上创建一个print()方法,以便我可以简单地调用myLibrary.print()来打印数组。

public class Library {
    // Constructor

    public Library() {
    }

    public void print() {
        System.out.println("Library Sorted by Title");
    }
}

public class MediaManager {
    public static void main(String[] args) throws Exception {
        Library myLibrary[] = new Library[100];
        myLibrary.print();
    }
}

I am getting an error saying that there is no print() on Library[].

我收到一个错误,说库[]上没有print()。

How would I go about printing this array? Would I just loop through the array in the main file and call a separate print on each object? If this is the case, where would I write custom methods to sort the array?

我该如何打印这个阵列?我只是循环遍历主文件中的数组并在每个对象上调用单独的打印吗?如果是这种情况,我会在哪里编写自定义方法来对数组进行排序?

UPDATE

Requirements from my assignment: "Your program will use one array of type Library to store all information read from the input file."

我的任务要求:“你的程序将使用一个类型库数组来存储从输入文件中读取的所有信息。”

"At the top level you will have a class called Library. Library will have three subclasses: Music, Book, and Movie. Music will have two subclasses: Song and Album. Book will have two subclasses: Fiction and Nonfiction. Movie, Fiction, Nonfiction, Song, and Album will not have any subclasses."

“在顶层,你将有一个名为Library的类。图书馆将有三个子类:Music,Book和Movie。音乐将有两个子类:Song和Album。Book将有两个子类:Fiction和Nonfiction。电影,小说,非小说类,歌曲和专辑将没有任何子类。“

UPDATE 2

This is for a CS-101 course. I don't feel I should need to use Comparable.

这是CS-101课程。我觉得我不需要使用Comparable。

6 个解决方案

#1


4  

In addition to defining print() method for Library; You can try following:

除了为Library定义print()方法外;您可以尝试以下方法:

public void printLibrary (Library [] libraryArray) {
    for(Library library :  libraryArray) 
        library.print();
}

or if you overload toString() for Library you can simply use:

或者如果你为库重载toString(),你可以简单地使用:

public void printLibrary (Library [] libraryArray) {
    for(Library library :  libraryArray) 
        System.out.println(library);
}

And the calling will be done like:

呼叫将像:

Library myLibrary[] = new Library[100];
printLibrary(myLibrary);

EDIT:

To sort the array you should implement Comparable<Library> in Library class and simply use

要对数组进行排序,您应该在Library类中实现Comparable 并简单地使用

Arrays.sort(myLibrary);

to sort the array.

排序数组。

#2


0  

myLibrary is an array of type Library. It doesn't have the method print. You should try something like:

myLibrary是一个类型库的数组。它没有打印方法。你应该尝试类似的东西:

Library myLibrary[] = new Library[100];
myLibrary[0] = new Library();    
myLibrary[0].print();

#3


0  

You declare a print method in Library class, it means you can call that method in each Library object, but you point at an array of Library instead of each library, what you can to do to print all array's object is :

你在Library类中声明了一个print方法,这意味着你可以在每个Library对象中调用该方法,但是你指向一个Library数组而不是每个库,你可以做的就是打印所有数组的对象是:

Library myLibrary[] = new Library[100];
for(Library library : myLibrary){ 
    library.print();
}

#4


0  

From the discussions above, a more suited data model would look like this:

从上面的讨论中,更合适的数据模型如下所示:

public class Library {
   private Vector<Title> allTitles;

   public void print() {
     // Print all titles from the vector
     for(Title t : allTitles) {
        t.print();
     }
   }
}

public abstract class Title {
   public abstract void print();
}

public class Movie extends Title {
   public void print() {
      ... // print the Movie
   }
}

public class Music extends Title {
   public void print() {
      ... // print the Music
   }
}

public class Book extends Title {
   public void print() {
      ... // print the Book
   }
}

#5


0  

you are actually calling print method from Library type Array and you should call print method from Library type object(not Library type Array).

你实际上是从库类型数组调用print方法,你应该从库类型对象(而不是库类型数组)调用print方法。

public class Library {
    // Constructor

    public Library() {
    }

    public void print() {
        System.out.println("Library Sorted by Title");
    }
}

public class MediaManager {
    public static void main(String[] args) throws Exception {
        Library myLibrary[] = new Library[100];
        for (Library lb : myLibrary) {
            lb.print();
        }
    }
}

This will work.

这会奏效。

#6


0  

I am bound to add another answer post the question is updated twice from original question:

我必须添加另一个答案,问题是从原始问题更新两次:

UPDATE

Requirements from my assignment: "Your program will use one array of type Library to store all information read from the input file." "At the top level you will have a class called Library. Library will have three subclasses: Music, Book, and Movie. Music will have two subclasses: Song and Album. Book will have two subclasses: Fiction and Nonfiction. Movie, Fiction, Nonfiction, Song, and Album will not have any subclasses."

我的任务要求:“你的程序将使用一个类型库数组来存储从输入文件中读取的所有信息。” “在顶层,你将有一个名为Library的类。图书馆将有三个子类:Music,Book和Movie。音乐将有两个子类:Song和Album。Book将有两个子类:Fiction和Nonfiction。电影,小说,非小说类,歌曲和专辑将没有任何子类。“

UPDATE 2

This is for a CS-101 course. I don't feel I should need to use Comparable.

这是CS-101课程。我觉得我不需要使用Comparable。

The best solution seems:

最佳解决方案似乎:

A. Create class structure as defined in your problem.

A.创建问题中定义的类结构。

class Library {...}
class Music extends Library {...}
class Book extends Library {...}
class Movie extends Library {...}
class Song extends Music {...}
class Album extends Music {...}
class Fiction extends Book {...}
class NonFiction extends Book {...}

B. Override toString() method in each class.

B.在每个类中重写toString()方法。

C. Simply use a for-each loop and print the objects.

C.只需使用for-each循环并打印对象。

for(Library library :  libraryArray) 
    System.out.println(library);

D. You have to use Comparable or Comparator in case you want the Array to be ordered/sorted; there is no other way.

D.如果您想要对阵列进行排序/排序,您必须使用Comparable或Comparator;没有其他办法。

#1


4  

In addition to defining print() method for Library; You can try following:

除了为Library定义print()方法外;您可以尝试以下方法:

public void printLibrary (Library [] libraryArray) {
    for(Library library :  libraryArray) 
        library.print();
}

or if you overload toString() for Library you can simply use:

或者如果你为库重载toString(),你可以简单地使用:

public void printLibrary (Library [] libraryArray) {
    for(Library library :  libraryArray) 
        System.out.println(library);
}

And the calling will be done like:

呼叫将像:

Library myLibrary[] = new Library[100];
printLibrary(myLibrary);

EDIT:

To sort the array you should implement Comparable<Library> in Library class and simply use

要对数组进行排序,您应该在Library类中实现Comparable 并简单地使用

Arrays.sort(myLibrary);

to sort the array.

排序数组。

#2


0  

myLibrary is an array of type Library. It doesn't have the method print. You should try something like:

myLibrary是一个类型库的数组。它没有打印方法。你应该尝试类似的东西:

Library myLibrary[] = new Library[100];
myLibrary[0] = new Library();    
myLibrary[0].print();

#3


0  

You declare a print method in Library class, it means you can call that method in each Library object, but you point at an array of Library instead of each library, what you can to do to print all array's object is :

你在Library类中声明了一个print方法,这意味着你可以在每个Library对象中调用该方法,但是你指向一个Library数组而不是每个库,你可以做的就是打印所有数组的对象是:

Library myLibrary[] = new Library[100];
for(Library library : myLibrary){ 
    library.print();
}

#4


0  

From the discussions above, a more suited data model would look like this:

从上面的讨论中,更合适的数据模型如下所示:

public class Library {
   private Vector<Title> allTitles;

   public void print() {
     // Print all titles from the vector
     for(Title t : allTitles) {
        t.print();
     }
   }
}

public abstract class Title {
   public abstract void print();
}

public class Movie extends Title {
   public void print() {
      ... // print the Movie
   }
}

public class Music extends Title {
   public void print() {
      ... // print the Music
   }
}

public class Book extends Title {
   public void print() {
      ... // print the Book
   }
}

#5


0  

you are actually calling print method from Library type Array and you should call print method from Library type object(not Library type Array).

你实际上是从库类型数组调用print方法,你应该从库类型对象(而不是库类型数组)调用print方法。

public class Library {
    // Constructor

    public Library() {
    }

    public void print() {
        System.out.println("Library Sorted by Title");
    }
}

public class MediaManager {
    public static void main(String[] args) throws Exception {
        Library myLibrary[] = new Library[100];
        for (Library lb : myLibrary) {
            lb.print();
        }
    }
}

This will work.

这会奏效。

#6


0  

I am bound to add another answer post the question is updated twice from original question:

我必须添加另一个答案,问题是从原始问题更新两次:

UPDATE

Requirements from my assignment: "Your program will use one array of type Library to store all information read from the input file." "At the top level you will have a class called Library. Library will have three subclasses: Music, Book, and Movie. Music will have two subclasses: Song and Album. Book will have two subclasses: Fiction and Nonfiction. Movie, Fiction, Nonfiction, Song, and Album will not have any subclasses."

我的任务要求:“你的程序将使用一个类型库数组来存储从输入文件中读取的所有信息。” “在顶层,你将有一个名为Library的类。图书馆将有三个子类:Music,Book和Movie。音乐将有两个子类:Song和Album。Book将有两个子类:Fiction和Nonfiction。电影,小说,非小说类,歌曲和专辑将没有任何子类。“

UPDATE 2

This is for a CS-101 course. I don't feel I should need to use Comparable.

这是CS-101课程。我觉得我不需要使用Comparable。

The best solution seems:

最佳解决方案似乎:

A. Create class structure as defined in your problem.

A.创建问题中定义的类结构。

class Library {...}
class Music extends Library {...}
class Book extends Library {...}
class Movie extends Library {...}
class Song extends Music {...}
class Album extends Music {...}
class Fiction extends Book {...}
class NonFiction extends Book {...}

B. Override toString() method in each class.

B.在每个类中重写toString()方法。

C. Simply use a for-each loop and print the objects.

C.只需使用for-each循环并打印对象。

for(Library library :  libraryArray) 
    System.out.println(library);

D. You have to use Comparable or Comparator in case you want the Array to be ordered/sorted; there is no other way.

D.如果您想要对阵列进行排序/排序,您必须使用Comparable或Comparator;没有其他办法。