Java Classes&对象,我可以更好地编码吗?

时间:2015-04-24 14:44:41

标签: java

我为Author和Book编写了两个类,然后在Book类中包含了Author。没有任何用户输入,只是让我围绕Java类的方式。在我的驱动程序中,我正在尝试创建书籍对象以显示详细信息。细节结合并按预期显示,但我觉得有更好的方法。你有什么建议吗?

3个文件:

BookDriver.java

import java.util.Scanner;
import java.util.*;
import java.io.*;

public class BookDriver{
public static void main(String[] args) {

    Scanner userKeyboardInput = new Scanner(System.in);
    System.out.println("");
    System.out.println("===========================");
    System.out.println("     Book Driver Output    ");
    System.out.println("          ******           ");  
    System.out.println("        genericCog         ");   
    System.out.println("===========================");
    System.out.println("");
    Book.CreateBook1();
    Book.ShowBook1();
    Book.CreateBook2();
    Book.ShowBook2();
    System.out.println("");
    System.out.println("===========================");
}//END main
}//END class

Book.java

public class Book {

static String   Title;
static String   Category;
static String   CoverType;
static int      YearPublished;
static int      NumberOfPages;
static String   FirstName;
static String   LastName;
static String   AuthorFullName;

// BOOK 1
static void CreateAuthor1(){
    Author Author1 = new Author();
    FirstName = "J. R. R.";
    LastName = "Tolkien";
    AuthorFullName = Author1.CombineAuthorName(FirstName, LastName);
}
static void CreateBook1() {
    CreateAuthor1();
    Title  = "The Hobbit; or, There and Back Again";
    YearPublished = 1937;
    NumberOfPages = 300;
    Category = "Fantasy";
    CoverType = "Paperback";
}

static void ShowBook1() {
    System.out.println("-----                  -----");
    System.out.println("       Book 1 Details       ");
    System.out.println("-----                  -----");
    System.out.println("Title:      " + Title);
    System.out.println("Author:     " + AuthorFullName);
    System.out.println("Published:  " + YearPublished);
    System.out.println("Pages:      " + NumberOfPages);
    System.out.println("Category:   " + Category);
    System.out.println("Cover Type: " + CoverType);
    System.out.println("");
    System.out.println("############################");
    System.out.println("");

}

// BOOK 2
static void CreateAuthor2(){
    Author Author2 = new Author();
    FirstName = "Christian";
    LastName = "Keur";
    AuthorFullName = Author2.CombineAuthorName(FirstName, LastName);
}

static void CreateBook2() {
    CreateAuthor2();
    Title  = "IOS Programming: The Big Nerd Ranch Guide";
    YearPublished = 2011;
    NumberOfPages = 563;
    Category = "Educational";
    CoverType = "Paperback";
}

static void ShowBook2() {
    System.out.println("-----                  -----");
    System.out.println("       Book 2 Details       ");
    System.out.println("-----                  -----");
    System.out.println("Title:      " + Title);
    System.out.println("Author:     " + AuthorFullName);
    System.out.println("Published:  " + YearPublished);
    System.out.println("Pages:      " + NumberOfPages);
    System.out.println("Category:   " + Category);
    System.out.println("Cover Type: " + CoverType);
    System.out.println("");
    System.out.println("############################");
    System.out.println("");
}
}//END class Book

Author.java

public class Author {
static String   FirstName;
static String   LastName;
static String   AuthorFullName;

    public String CombineAuthorName (String FirstName, String LastName){
        AuthorFullName = FirstName + " " + LastName;
        return AuthorFullName;
}
}//END class Author

1 个答案:

答案 0 :(得分:0)

我建议

  • 您使用Java coding conventions特别是使用camelcase代替TitleCase来获取变量和方法名称。
  • 当您使用实例字段时,请不要初始化静态字段。
  • 删除不使用的字段。例如,Author中不需要任何字段。
  • 使用构造函数初始化实例值,而不是重置静态变量的方法。尽量避免使用可变的静态字段。
  • 尽量不要重复自己。 ShowBook1ShowBook2几乎完全相同。您可以设置一个具有图书编号的字段。
  • 尽量避免让自己初始化太多的课程。您可以传入设置图书所需的所有数据,BookDriver类可以创建许多不同的图书,而无需更改图书代码。