1.创建Book类
package J2eeTest; //包名 /** * @author wanjinYoung * */ public class Book { private String name;//书名 private String author;//作者 private String ISBN;//编码 //ISBN 为条形码 //带参数的构造函数 public Book(String name,String author,String ISBN){ //利用构造方法初始化 this.name = name; this.author = author; this.ISBN = ISBN; //this 是对当前对象的引用 } //获取书名 public String getName(){ return name; } //获取作者 public String getAuthor(){ return author; } //获取编号 public String getISBN(){ return ISBN; } /* * 同理有set方法 * *name 初始化 * public void setName(String name){ * this.name = name; * } * * author 初始化 * public void setAuthor(String author){ * this.author = author; * } * * ISBN 初始化 * public void setISBN(String ISBN){ * this.ISBN = ISBN; * } * */ }
2.创建Test类使用Book类
package J2eeTest; /* * 用于实验Book类 * author@wanjinYoung * @date */ public class Test { public static void main(String [] args){ //创建Book对象并设定对象的属性 Book book = new Book("This is my first book","Bob","78987986"); //输出书名 System.out.println("Book Name:" + book.getName()); //输出作者 System.out.println("Book Author:" + book.getAuthor()); //输出编码 System.out.println("ISBN:" + book.getISBN()); } }
注:两个类在同一个包中。
个人学习使用!