Java面向对象 类关联结构()关系

布满荆棘的人生 2023-01-05 15:28 285阅读 0赞
  • 一对一 person car
  • 一对多 person children (自身关联)

先写基本 关系后加

  1. package com.msc.example;
  2. class Car{
  3. private String cname ;
  4. private double price ;
  5. private Person person;
  6. public Car() { } ;
  7. public Car(String cname,double price){
  8. this.cname = cname ;
  9. this.price = price ;
  10. }
  11. public void setPerson(Person person){
  12. this.person = person ;
  13. }
  14. public Person getPerson(){
  15. return this.person ;
  16. }
  17. public void setCname(String cname){
  18. this.cname = cname ;
  19. }
  20. public void setPrice(double price){
  21. this.price = price ;
  22. }
  23. public String getCname(){
  24. return this.cname ;
  25. }
  26. public double getPrice(){
  27. return this.price ;
  28. }
  29. public String getInfo(){
  30. return "【汽车信息】名字 = "+this.cname+" 价格 = "+ this.price ;
  31. }
  32. }
  33. class Person{
  34. private String pname ;
  35. private int age ;
  36. private Car car ;
  37. private Person children[] ;
  38. public Person() { };
  39. public Person(String pname,int age) {
  40. this.pname = pname ;
  41. this.age = age ;
  42. };
  43. public void setChildren(Person[] children) {
  44. this.children = children;
  45. }
  46. public Person[] getChildren() {
  47. return this.children;
  48. }
  49. public void setCar(Car car) {
  50. this.car = car;
  51. }
  52. public Car getCar() {
  53. return this.car;
  54. }
  55. public void setPname(String pname) {
  56. this.pname = pname;
  57. }
  58. public void setAge(int age) {
  59. this.age = age;
  60. }
  61. public int getAge() {
  62. return this.age;
  63. }
  64. public String getPname() {
  65. return this.pname;
  66. }
  67. public String getInfo(){
  68. return "【个人信息】 姓名 = "+this.pname+" 年龄 = "+this.age ;
  69. }
  70. }
  71. public class JavaBegin {
  72. public static void main(String args[]){
  73. // 第一步声明对象并设置彼此的关系
  74. Person father = new Person("张三",45) ;
  75. Person mother = new Person("王五",46) ;
  76. Person son = new Person("小刘",45) ;
  77. Person daugher = new Person("小莉",45) ;
  78. Car carA = new Car("法拉利",100000) ;
  79. Car carB = new Car("兰博基尼",600000) ;
  80. Car carC = new Car("奥迪",15640) ;
  81. Car carD = new Car("奔驰",200) ;
  82. father.setCar(carA);
  83. carA.setPerson(father);
  84. mother.setCar(carB);
  85. carB.setPerson(mother);
  86. son.setCar(carC);
  87. carA.setPerson(son);
  88. daugher.setCar(carD);
  89. carD.setPerson(daugher);
  90. father.setChildren(new Person[]{ son,daugher}) ;
  91. mother.setChildren(new Person[]{ son,daugher}) ;
  92. // 第二步根据关系获取数据
  93. System.out.println(father.getInfo()+" and "+father.getCar().getInfo()); //人找车
  94. System.out.println(carB.getInfo()+" and "+carB.getPerson().getInfo()); //车找人
  95. for(Person temp : father.getChildren()){
  96. System.out.println(temp.getInfo());
  97. }
  98. for(int x = 0 ;x<mother.getChildren().length;x++){
  99. System.out.println(mother.getChildren()[x].getInfo());
  100. }
  101. }
  102. }

输出:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,285人围观)

还没有评论,来说两句吧...

相关阅读