本帖最后由 王虎 于 2012-10-14 14:28 编辑
下面这个练习题用了两种方式,虽然都能实现结果,但是技术含量却是不一样的,前者只是单纯的利用“名字”比较,如果在加个条件比“名字”+“作者”就有局限性了
而用了面向对象之后只需比较对象就可,因为属性都封装到对象里了
注意的是:面向对象之后的写法不要忽略 重写equals方法,不然就达不到想要的达到的效果了~
/*
List练习
有一个类 Book类 属性 bookName bookAuthor bookPrice
定义一个List集合,装入五本书.
从键盘输入一个书名,如果有这本书,将这本书的信息输出。
*/
面向对象之前-------------------------------------------
*/
import java.util.*;
class Test3
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Book b1=new Book("红高粱","莫言",14);
Book b2=new Book("碧血剑","金庸",84);
Book b3=new Book("小李飞刀","古龙",64);
Book b4=new Book("楚留香","古龙",32);
Book b5=new Book("Thinking in java","tom",100);
List list=new ArrayList();
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
list.add(b5);
System.out.println("请输入要查找的书名:");
String name=sc.nextLine();
Book book=BookTools.findBookByName(name,list);
if(book==null){
System.out.println("查无此书");
}else{
System.out.println(book);
}
}
}
class BookTools
{
//根据书名去查找书籍
public static Book findBookByName(String name,List list){
Book book=null;
for(int i=0;i<list.size();i++){
Book b=(Book)list.get(i);
if(name.trim().equals(b.getBookName().trim())){
book=b;
break;
}
}
return book;
}
}
//首先定义书类
class Book
{
private String bookName;
private String bookAuthor;
private double bookPrice;
public Book(String bookName,String bookAuthor,double bookPrice){
this.bookName=bookName;
this.bookAuthor=bookAuthor;
this.bookPrice=bookPrice;
}
public String getBookName(){
return bookName;
}
public String toString(){
//return "书名:"+bookName+" 作者:"+bookAuthor+" 价格:"+bookPrice;
StringBuilder builder=new StringBuilder();
builder.append("书名:").append(bookName).append(" 作者:").append(bookAuthor).append(" 价格:").append(bookPrice);
return builder.toString();
}
}
面向对象之后-------------------------------------------
import java.util.*;
class Test4
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Book b1=new Book("红高粱","莫言",14);
Book b2=new Book("碧血剑","金庸",84);
Book b3=new Book("小李飞刀","古龙",64);
Book b4=new Book("楚留香","古龙",32);
Book b5=new Book("Thinking in java","tom",100);
List list=new ArrayList();
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
list.add(b5);
System.out.println("请输入要查找的书名:");
String name=sc.nextLine();
//定义一个要查找的书籍
Book findBook=new Book(name);
Book book=BookTools.findBookByName(findBook,list);
if(book==null){
System.out.println("查无此书");
}else{
System.out.println(book);
}
}
}
class BookTools
{
//根据书名去查找书籍
public static Book findBookByName(Book findBook,List list){
Book book=null;
if(list.contains(findBook)){ //判断是否包含这本书
int index=list.indexOf(findBook);//得到第一次出现的索引
book=(Book)list.get(index);//根据索引得到这本书
}
/*
上面使用的contains与indexOf方法在内部都默认使用了 Book类的equals方法.
*/
return book;
}
}
//首先定义书类
class Book
{
private String bookName;
private String bookAuthor;
private double bookPrice;
public Book(String bookName,String bookAuthor,double bookPrice){
this.bookName=bookName;
this.bookAuthor=bookAuthor;
this.bookPrice=bookPrice;
}
public Book(String bookName){
this.bookName=bookName;
}
public String getBookName(){
return bookName;
}
public String toString(){
//return "书名:"+bookName+" 作者:"+bookAuthor+" 价格:"+bookPrice;
StringBuilder builder=new StringBuilder();
builder.append("书名:").append(bookName).append(" 作者:").append(bookAuthor).append(" 价格:").append(bookPrice);
return builder.toString();
}
/*
这个equals方法重写时只重写了bookName属性,
是因为在查找书籍时使用书名查找 .
*/
public boolean equals(Object obj){
if(obj instanceof Book){ //instanceOf 是判断obj指向的对象的类型是否是 Book
Book b=(Book)obj;
return b.bookName.equals(bookName);
}
return false;
}
}
|