moummm
2017-02-14 22:18
package com.collection;
import java.util.ArrayList;
import java.util.List;
public class ListTest {
public List BookToSelect;
public ListTest(){
this.BookToSelect=new ArrayList();
}
public void BookAdd(){
Book bt=new Book("1.","《三国演义》");//此处编译器报错
BookToSelect.add(bt);
Book temp=(Book) BookToSelect.get(0);
System.out.println("添加了第一本书:"+bt.id+bt.name);
}
public static void main(Sring[] args){
ListTest lt=new ListTest();
lt.BookAdd();
}
}
public class Book {
public String id;
public String name;
public void Book(String id,String name) {
this.id = id;
this.name = name;
}
}
//如果把Book类里面的属性设置成private,那在BookAdd()中应该怎么去添加书呢?
public void Book(String id,String name) {
this.id = id;
this.name = name;
}
把void去掉,构造方法中是不要返回的
后面的问题中:改成private起到的是封装的作用,减少了调用的途径,BookAdd()直接把对象放进去就添加了
System.out.println("添加了第一本书:"+bt.id+bt.name);
这里应该用temp.id temp.name 啊
public class ListTest {
public List BookToSelect;
public ListTest(){
this.BookToSelect=new ArrayList();
}
public void BookAdd(){
Book bt=new Book("1.","《三国演义》");
BookToSelect.add(bt);
Book temp=(Book) BookToSelect.get(0);
System.out.println("添加了第一本书:"+bt.getId()+":"+bt.getName());
}
public static void main(Sring[] args){
ListTest lt=new ListTest();
lt.BookAdd();
}
}
为啥这个类还是运行不了呢?
我试过了,把void 去掉的结果是不对的
Method 'main[String[]]' is never used
这句话是什么意思啊?
楼上说的对
Java入门第三季
409792 学习 · 4340 问题
相似问题