一个关于JAVA的疑问
这是一个JAVA程序,主要练习继承的用法。不过,我在这想问的是一个读入多个参数的问题
下面是一个编写类
import java.util.ArrayList;
public class Readings {
private int pages;
private String name;
public ArrayList<String> keywords=new ArrayList<String>();
public Readings(String a,int b,String ... keywords){
name=a;
pages=b;
}
public String toString(){
String xinxi="书名:"+name+"\n页数:"+pages+"\n"+"关键词:";
int a=keywords.size();
String xinxi3="",xinxi2;
for(int num=0;num < a;num++)
{
xinxi2=keywords.get(num);
xinxi3=xinxi2+xinxi3;
}
String xinxi4=xinxi+xinxi3;
return xinxi4;
}
}
class Book extends Readings{
public Book(String a,int b,String ... keywords){
super(a,b);
}
}
class Novel extends Readings{
public Novel(String a,int b,String ... keywords){
super(a,b);
}
}
class Journal extends Readings{
public Journal(String a,int b,String ... keywords){
super(a,b);
}
}
class Magazine extends Readings{
public Magazine(String a,int b,String ... keywords){
super(a,b);
}
}
class Textbook extends Readings{
public Textbook(String a,int b,String ... keywords){
super(a,b);
}
}
下面这个是驱动类
public class Readingsmian {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Readings rea=new Readings("hhh",123,"hhh","zouzouzou","helloworld");
Novel nov=new Novel("钢铁是怎样炼成的",1236,"保尔","俄罗斯");
Magazine mag=new Magazine("电脑周刊",256,"电脑科技","电脑组装","硬件大全","电脑购买");
Textbook text =new Textbook("JAVA程序设计教程",505,"JAVA程序设计","编程","教学");
System.out.println(nov);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(mag);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(text);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(rea);
}
}
我想问的就是。为什么后面的多个参数读不进去,如何能读进去呢,求指教
------解决方案--------------------
原因是构造函数里没有对keywords赋值
Java code
public Readings(String a,int b,String ... keywords){
name=a;
pages=b;
[color=#000080]
for(String s:keywords){
this.keywords.add(s);
}
[/color]
}