package com.aaa.zxf.ajax.test; import java.io.Serializable; /** * java中的继承。 * * 一、数据模型类 * 数据模型类:用来存取数据库数据对应的java类 * 理解成:需要将先有的类完善成标准的数据模型类 * * * 二、访问控制修饰符 * * public : 公共的。 对所有类可见(在同一个工程中 任何类都可以调用)。使用对象:类、接口、成员变量、方法 * protected : (包权限和继承权限 )对同一包内的类或所有子类可见。使用对象:成员变量、方法。 注意:不能修饰类(外部类)。包权限指的使 即使没有关系但是在同一个包中 可以访问 继承权限指的是 即使不在同一个包中但是有继承关系 依然可以访问 * default (即缺省,什么也不写): 包权限 在同一包内可见,不使用任何修饰符。使用对象:类、接口、成员变量、方法。 注意 什么都不写并不代表没有权限修饰符 而是代表默认的权限修饰 * private : 在同一类内可见。使用对象:变量、方法。 注意:不能修饰类(外部类) * * 我们可以可以通过以下表来说明访问权限: * 访问控制 * 修饰符 当前类 同一包内 子孙类 其他包 * public Y Y Y Y * protected Y Y Y N * default Y Y N N * private Y N N N * */ public class JavaJiCheng { public static void main(String[] args) { People p = new People(); // p.age = -45; // age -45? 如何解决? 给age 设置 set方法 进行基本的判断。。 // // p.name = "haha"; // System.out.println("name="+p.name+" age="+p.age ); /** * 通过set 赋值 * get 获得 */ People p1 = new People(); p1.setAge(45); p1.setName("xixi"); int age = p1.getAge(); } } //1.创建一个数据模型类 class People{ private int age; private String name; /** * 我们希望 有了set方法之后 就不要使用 打点调用的形式了。 * 解决方案:将成员变量的权限修饰符设定为 私有的。 */ void setAge(int age){ // 三元运算符 不大于0 就将age 赋值为0 this.age = age>0 ? age :0; } void setName(String name){ this.name = name; } /** * 解决私有属性无法调用的问题 * * 建立一个 get方法 */ public int getAge(){ return age; } public String getName(){ return name; } } //数据模型类最终版 class Book implements 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 + '\'' + '}'; } }