Java创建对象的2种方法

这是最常用的一种。如:

Book book = new Book();

示例如下:


package test;


import java.io.Serializable;

import java.util.List;


/**

 * @author wangmengjun

 *

 */

public class Book implements Serializable{


    private static final long serialVersionUID = -6212470156629515269L;


    /**书名*/

    private String name;


    /**作者*/

    private List<String> authors;


    /**ISBN*/

    private String isbn;


    /**价格*/

    private float price;


    public Book() {

    }


    /**

     * @param name

     * @param authors

     * @param isbn

     * @param price

     */

    public Book(String name, List<String> authors, String isbn, float price) {

        this.name = name;

        this.authors = authors;

        this.isbn = isbn;

        this.price = price;

    }


    /**

     * @return the name

     */

    public String getName() {

        return name;

    }


    /**

     * @param name the name to set

     */

    public void setName(String name) {

        this.name = name;

    }


    /**

     * @return the authors

     */

    public List<String> getAuthors() {

        return authors;

    }


    /**

     * @param authors the authors to set

     */

    public void setAuthors(List<String> authors) {

        this.authors = authors;

    }


    /**

     * @return the isbn

     */

    public String getIsbn() {

        return isbn;

    }


    /**

     * @param isbn the isbn to set

     */

    public void setIsbn(String isbn) {

        this.isbn = isbn;

    }


    /**

     * @return the price

     */

    public float getPrice() {

        return price;

    }


    /**

     * @param price the price to set

     */

    public void setPrice(float price) {

        this.price = price;

    }


    /* (non-Javadoc)

     * @see java.lang.Object#toString()

     */

    @Override

    public String toString() {

        return "Book [name=" + name + ", authors=" + authors + ", isbn=" + isbn + ", price="

                + price + "]";

    }


}


使用object.clone()


如果要调用clone方法,那么该object需要实现Cloneable接口,并重写clone()方法。


修改后的Book类如下:


package test;


package test;


import java.io.Serializable;

import java.util.List;


/**

 * @author wangmengjun

 *

 */

public class Book implements Serializable, Cloneable {


    private static final long serialVersionUID = -6212470156629515269L;


    /**书名*/

    private String name;


    /**作者*/

    private List<String> authors;


    /**ISBN*/

    private String isbn;


    /**价格*/

    private float price;


    public Book() {

    }


    /**

     * @param name

     * @param authors

     * @param isbn

     * @param price

     */

    public Book(String name, List<String> authors, String isbn, float price) {

        this.name = name;

        this.authors = authors;

        this.isbn = isbn;

        this.price = price;

    }


    /**

     * @return the name

     */

    public String getName() {

        return name;

    }


    /**

     * @param name the name to set

     */

    public void setName(String name) {

        this.name = name;

    }


    /**

     * @return the authors

     */

    public List<String> getAuthors() {

        return authors;

    }


    /**

     * @param authors the authors to set

     */

    public void setAuthors(List<String> authors) {

        this.authors = authors;

    }


    /**

     * @return the isbn

     */

    public String getIsbn() {

        return isbn;

    }


    /**

     * @param isbn the isbn to set

     */

    public void setIsbn(String isbn) {

        this.isbn = isbn;

    }


    /**

     * @return the price

     */

    public float getPrice() {

        return price;

    }


    /**

     * @param price the price to set

     */

    public void setPrice(float price) {

        this.price = price;

    }


    /* (non-Javadoc)

     * @see java.lang.Object#toString()

     */

    @Override

    public String toString() {

        return "Book [name=" + name + ", authors=" + authors + ", isbn=" + isbn + ", price="

                + price + "]";

    }


    @Override

    protected Object clone() throws CloneNotSupportedException {

        return (Book) super.clone();

    }


}


评论

© 牛耳教育|长沙java培训|长沙java培训学校|长沙软件培 | Powered by LOFTER