The data model class java

Package Penalty for com.aaa.zxf.ajax.test; 

Import the java.io.Serializable; 

/ ** 
 The * java inheritance. 
 * 
 * A data model classes 
 * Data model classes: database for accessing data corresponding java class 
 * understood: the need to improve the conventional type to a standard data model class 
 * 
 * 
 * Second, access control modifiers 
 * 
 * public: public. Visible to all classes (in the same project in any class can call). Use objects: classes, interfaces, fields, methods 
 * protected: (package permissions and inheritance rights) for all classes or subclasses in the same package visibility. Used by: member variables and methods. Note: You can not modify the class (outside class). Package even without permission refers to the relationship but can be accessed in the same package to inherit permissions means that even if not in the same package but inheritance can still access 
 * default (which is the default, nothing to write): Package authority in the same package visibility, do not use any modifier. Use objects: classes, interfaces, fields, methods. Note that does not mean nothing to write but do not have permission modifier modified to represent the default permissions 
 * private: visible within the same class. Use objects: variables and methods. Note: You can not modify classes (external class) 
 * 
 * we can access can be illustrated by the following table: 
 * Access Control
 * Modifier within the same package as the current class descendent class of other packages
 Public YYYY * 
 * protected YYYN 
 * default YYNN 
 * Private Ynnn 
 * 
  
         * set by assigning* / 
public  class JavaJiCheng {
     public  static  void main (String [] args) { 

        People P = new new People ();
 //         p.age = -45;         // Age -45? How to solve? Age setting method to set basic judgment. .
// 
//         p.name = "haha";
 //         System.out.println ( "name =" + p.name + "Age =" + p.age); 

        / ** 
         * GET obtain 
         * / 
        People p1 = new new People (); 
        p1.setAge ( 45 ); 
        p1.setName ( "xixi" ); 

        int Age = p1.getAge (); 

    } 
} 

// 1. Create a data model class 
class   People {
     Private      int Age;
     Private      String name; 

    / ** 
     * we do not want to use in the form of a call after the RBI has set method. 
     * Solution: Permissions member variable modifier is set to private. 
     * / 
        Void the setAge ( int age) {
             // ternary operator 0 is not larger than 0 is assigned will age 
            the this .age age => 0 age: 0? ; 
        } 

        VoidsetName (String name) {
             the this .name = name; 
        } 


    / ** 
     * private property can not be invoked to solve the problem of 
     * 
     * the establishment of a get method 
     * / 
    public  int getAge () {
         return   Age; 
    } 

    public String getName () {
         return   name ; 
    } 

} 


// data model class fINAL 

class   Book the implements the Serializable { 

    Private   int BookID;
     Private   String bookname;
     Private   Double . price;
     Private   String Autor;

    public Book() {
    }

    public Book(int bookid, String bookname, double price, String autor) {
        this.bookid = bookid;
        this.bookname = bookname;
        this.price = price;
        this.autor = autor;
    }

    public int getBookid() {
        return bookid;
    }

    public void setBookid(int bookid) {
        this.bookid = bookid;
    }

    public String getBookname() {
        return bookname;
    }

    public void setBookname(String bookname) {
        this.bookname = bookname;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getAutor() {
        return autor;
    }

    public void setAutor(String autor) {
        this.autor = autor;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;

        if (bookid != book.bookid) return false;
        if (Double.compare(book.price, price) != 0) return false;
        if (bookname != null ? !bookname.equals(book.bookname) : book.bookname != null) return false;
        return autor != null ? autor.equals(book.autor) : book.autor == null;

    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = bookid;
        result = 31 * result + (bookname != null ? bookname.hashCode() : 0);
        temp = Double.doubleToLongBits(price);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + (autor != null ? autor.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookid=" + bookid +
                ", bookname='" + bookname + '\'' +
                ", price=" + price +
                ", autor='" + autor + '\'' +
                '}';
    }
}

 

Guess you like

Origin www.cnblogs.com/ZXF6/p/11531613.html