Java对象之间的关系有:一对一,一对多,多对多~
一对一关系例子:
一个学生对应一本书
Book类
package Com; public class Book { private String StuID; private String BookName; private String dec; private Student student; public Book() { super(); // TODO Auto-generated constructor stub } public Book(String stuID, String bookName, String dec, Student student) { super(); StuID = stuID; BookName = bookName; this.dec = dec; this.student = student; } public String getStuID() { return StuID; } public void setStuID(String stuID) { StuID = stuID; } public String getBookName() { return BookName; } public void setBookName(String bookName) { BookName = bookName; } public String getDec() { return dec; } public void setDec(String dec) { this.dec = dec; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } }
package Com; public class Student { private String StuID; private String name; private int age; private Book book; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String stuID, String name, int age, Book book) { super(); StuID = stuID; this.name = name; this.age = age; this.book = book; } public String getStuID() { return StuID; } public void setStuID(String stuID) { StuID = stuID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int i) { this.age = i; } public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } }
main
package Com; public class main { /** * @param args */ public static void main(String[] args) { // 创建student对象zs Student zs=new Student(); zs.setStuID("1"); zs.setName("zhangsang"); zs.setAge(21); // 创建book 对象 thinkingJava Book thinkingJava=new Book(); thinkingJava.setBookName("TH001"); thinkingJava.setBookName("java编程思想"); thinkingJava.setDec("xxx"); thinkingJava.setStudent(zs); zs.setBook(thinkingJava); // 将书对象给学生zs } }
第二种关系为一对多,这个例子里就是一个学生可以拥有多本书,方法也很简单,只需要在Student类中把Book类声明改为List集合就可以了,不使用数组的优势是数量可以随时增减
package ComMuti; import java.util.List; public class Student { private String StuID; private String name; private int age; private List<Book> book; // 一个学生可以拥有多本书 建议使用集合 数量可以随时增减 public Student() { super(); // TODO Auto-generated constructor stub } public Student(String stuID, String name, int age, List<Book> book) { super(); StuID = stuID; this.name = name; this.age = age; this.book = book; } public String getStuID() { return StuID; } public void setStuID(String stuID) { StuID = stuID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int i) { this.age = i; } public List<Book> getBook() { return book; } public void setBook(List<Book> book) { this.book = book; } }
package ComMuti; import java.util.ArrayList; import java.util.List; // 面向对象 一对多的情况 // 一个学生可以拥有多本书 // 在学生类中声明书的集合 // 在main函数中将书的集合放入学生类的书集合中。 public class main { /** * @param args */ public static void main(String[] args) { // 创建student对象zs Student zs=new Student(); zs.setStuID("1"); zs.setName("zhangsang"); zs.setAge(21); // 创建book 对象 thinkingJava Book thinkingJava=new Book(); thinkingJava.setBookName("TH001"); thinkingJava.setBookName("java编程思想"); thinkingJava.setDec("xxx"); thinkingJava.setStudent(zs); // 创建book 对象 CoreJava Book CoreJava=new Book(); CoreJava.setBookName("TH002"); CoreJava.setBookName("java核心"); CoreJava.setDec("xxx"); CoreJava.setStudent(zs); // 创建book集合 List<Book> books=new ArrayList<Book>(); books.add(thinkingJava); books.add(CoreJava); zs.setBook(books); // 得到第一本书的名字 System.out.println(zs.getBook().get(0).getBookName()); } }
Java对象的关系写起来更多的是为了程序框架考虑。
相比C++,Java的方法调用感觉简单多了,没有复杂的指针乱指,不用担心内存泄露,用的还是比较顺手的。