J2EE 类的创建

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());
			
		}
}

注:两个类在同一个包中。

个人学习使用!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值