日期:2014-05-20 浏览次数:20884 次
public class Book{//示例,只是示例
String title;
int pageNum;
int type;
public Book(int pageNum,int type){//这就是传说中的构造方法。
this.title ="计算机";
this.pageNum = pageNum;
this.type = type;
}
}
------解决方案--------------------
public class Test24 {
public static void main(String[] args) {
Book book1 = new Book(100, 1);
book1.detail();
Book book2 = new Book("java", 200, 2);
book2.detail();
}
}
class Book {
String title;
int pageNum;
int type;
public Book(int pageNum, int type) {
this.title = "计算机";
this.pageNum = pageNum;
this.type = type;
}
public Book(String title, int pageNum, int type){
this.title = title;
this.pageNum = pageNum;
this.type = type;
}
public void detail(){
System.out.println("名称:" + title + ", 页数:" + pageNum + ", 类型:" + type);
}
}
------解决方案--------------------
public class Book{
String title;
int pageNum;
String type;
public Book(String titile,int pageNum){
this.title=title;
this.pageNum=pageNum;
this.type="计算机";
}
public Book(String titile,int pageNum,String type){
this.title=title;
this.pageNum=pageNum;
this.type=type;
}
}
------解决方案--------------------
不好意思,main方法和detail方法应该写在Book类里面
public String detail(){
System.out.println(this.title + "---" + this.pageNum + "---" + this.type);
}
public static void main(String[] args){
Book book1 = new Book("book1",100);
Book book2 = new Book("book2",200,"文学");
book1.detail();
book2.detail();
}