Java 如何在类数组中赋值

2025-05-10 01:46:15
推荐回答(1个)
回答1:

public class JavaA
{
public static void main(String[] args)
{
Book[] a1 = new Book[20];
for(int i = 0; i < 20; i++)
{
a1[i] = new Book("a", "b", 1, "c", 3d);
}
for(int i = 0; i < 20; i++)
{
System.out.println(a1[i]);
}
}

static class Book
{
private String auther;

private String BookName;

private int number;

private String press;

private double prices;

public Book()
{}

public Book(String auther, String BookName, int number, String press, double prices)
{
this.auther = auther;
this.BookName = BookName;
this.number = number;
this.press = press;
this.prices = prices;
}

public String getAuther()
{
return auther;
}

public String getBookName()
{
return BookName;
}

public int getNumber()
{
return number;
}

public String getPress()
{
return press;
}

public double getPrices()
{
return prices;
}

public String toString()
{
String s = auther + " " + BookName + " " + number + " " + press + " " + prices;
return s;
}
}
}