今天写作业遇到一个很基础的问题——传递数组参数。
如果向一个实例传递的参数为数组要怎么办呢?
例如我定义了一个类Book
class Book {
public string name;
public int price;
public Book(string p1,int p2) {
this.name = p1;
this.price = p2;
}
public string GetName() {
return name;
}
public int GetPrice() {
return price;
}
}
想把Book整个作为参数传递给BookCard方法
class BookCard
{
int rno;
string rname;
Book[] books = new Book[5];
//构造方法
public BookCard(int p1, string p2, Book[] p3)
{
this.rno = p1;
this.rname = p2;
this.books = p3;
}
}
在Main方法中需要实例化一个Book类型的数组再传递才可以哦~
class program1 {
static void Main() {
Book b = new Book("三国演义",20);
//实例化数组
Book[] books = new Book[]{b};
BookCard bc = new BookCard(001,"XXX",books);
}
}