public class Book {
我們擁有十載網頁設計和網站建設經驗,從網站策劃到網站制作,我們的網頁設計師為您提供的解決方案。為企業(yè)提供成都做網站、成都網站設計、微信開發(fā)、小程序制作、手機網站制作、H5場景定制、等業(yè)務。無論您有什么樣的網站設計或者設計方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設計服務并滿足您的需求。
private String title;
private Date pdate;
private int words;
public double price(){
日期這里用一個類處理比較簡單,不知道你用沒用過
SimpleDateFormat sdf = new SimpleDateFormat("MM");
month = sdf.format(pdate);
double xs = 0d;
if (month = 6){
xs = 1.2;
} else {
xs = 1.17;
}
return words / (1000 * 4.5) * xs;
}
}
這里不好空格,你自己格式化一下吧。
get/set方法沒寫
自己定義個main方法測一下吧
public class Book {
private String title ;
private String author;
private int sell;
public Book(){
}
public Book(String title,String author,int sell){
this.title=title;
this.author=author;
this.sell=sell;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getSell() {
return sell;
}
public void setSell(int sell) {
this.sell = sell;
}
public String toString() {
return "D [title=" + title + ", author=" + author + ", sell=" + sell
+ "]";
}
}
import?java.util.ArrayList;
import?java.util.Collection;
import?java.util.Iterator;
public?class?HomeWork?{
public?static?void?main(String[]?args)?{
CollectionBook?c1?=?new?ArrayListBook();
CollectionBook?c2?=?new?ArrayListBook();
//~?c1?add?data?----------------------------------------------------------------
c1.add(new?Book("數(shù)據(jù)結構","高等教育"));
c1.add(new?Book("操作系統(tǒng)","電子工業(yè)出版社"));
c1.add(new?Book("Java","電子工業(yè)出版社"));
//~?c2?add?data?----------------------------------------------------------------
c2.add(new?Book("操作系統(tǒng)","電子工業(yè)出版社"));
c2.add(new?Book("數(shù)據(jù)庫系統(tǒng)","清華大學出版社"));
Book?searchBook?=?new?Book("Java","機械工業(yè)出版社");
System.out.println("c1中是否包含書名為Java,出版社為機械工業(yè)出版社的圖書信息:"+?(c1.contains(searchBook)?"包含":"不包含"));
c1.removeAll(c2);
System.out.println("差以后c1的元素個數(shù):"+c1.size());
for(IteratorBook?iterator?=?c1.iterator();iterator.hasNext();System.out.println(iterator.next()));
}
}
class?Book?{
private?String?name;//書名
private?String?publish;//出版社
public?Book(String?name,?String?publish)?{
this.name?=?name;
this.publish?=?publish;
}
//~?Setter??Getter?Method?----------------------------------------------------------------
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?String?getPublish()?{
return?publish;
}
public?void?setPublish(String?publish)?{
this.publish?=?publish;
}
//~?Override?equals?Method?----------------------------------------------------------------
@Override
public?boolean?equals(Object?obj)?{
if?(this?==?obj)
return?true;
if?(obj?==?null)
return?false;
if?(getClass()?!=?obj.getClass())
return?false;
Book?other?=?(Book)?obj;
if?(name?==?null)?{
if?(other.name?!=?null)
return?false;
}
else?if?(!name.equals(other.name))
return?false;
if?(publish?==?null)?{
if?(other.publish?!=?null)
return?false;
}
else?if?(!publish.equals(other.publish))
return?false;
return?true;
}
//~?Override?toString?Method?----------------------------------------------------------------
@Override
public?String?toString()?{
return?"Book?[name="?+?name?+?",?publish="?+?publish?+?"]";
}
}
我寫了一個,方法可能有點麻煩,但是要求實現(xiàn)了
book類:
public class Book {
private String name;
private String publish;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public Book(String name, String publish) {
super();
this.name = name;
this.publish = publish;
}
public Book() {
}
}
測試類:
public class TestBook {
public static void main(String[] args) {
Book book1=new Book("數(shù)據(jù)結構", "高等教育");
Book book2=new Book("操作系統(tǒng)", "電子工業(yè)出版社");
Book book3=new Book("Java", "電子工業(yè)出版社");
Book book4=new Book("數(shù)據(jù)結構", "清華大學出版社");
CollectionBook c1=new ArrayList();
CollectionBook c2=new ArrayList();
CollectionBook c3=new ArrayList();
c1.add(book1);
c1.add(book2);
c1.add(book3);
c2.add(book2);
c2.add(book4);
boolean cont=false;
for(Book book:c1){
if(book.getName().equals("Java")book.getPublish().equals("機械工業(yè)出版社")){
cont=true;
}
}
System.out.println(cont);
for(Book book02:c2){
boolean a=true;
for(Book book01:c1){
if(book01.getName().equals(book02.getName())book01.getPublish().equals(book02.getPublish())){
a=false;
}
}
if(a){
c3.add(book02);
}
}
for(Book b:c3){
c1.add(b);
}
IteratorBook iterator=c1.iterator();
while(iterator.hasNext()){
Book b=iterator.next();
System.out.println("書名:"+b.getName());
System.out.println("出版社:"+b.getPublish());
}
}
}
文章標題:book類java代碼 java代碼教程
鏈接URL:http://jinyejixie.com/article6/doseoog.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供移動網站建設、品牌網站制作、虛擬主機、搜索引擎優(yōu)化、App設計、網站營銷
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)