Cirry's Blog

类之间的相互继承

2019-09-24
技术
java
最后更新:2024-04-05
3分钟
411字

类之间的相互引用

1
class Car {
2
private String name;
3
private double price;
4
private Person person;
5
6
public Car (String name , double price){
7
this.name = name ;
8
this.price = price;
9
}
10
11
public void setPerson(Person p){
12
this.person = p;
13
}
14
15
public Person getPerson(){
42 collapsed lines
16
return this.person;
17
}
18
19
public String getInfo(){
20
return "car name :" +this.name +", car price: " + this.price;
21
}
22
}
23
24
class Person{
25
private String name;
26
private int age;
27
private Car car;
28
29
public Person(String name , int age){
30
this.name= name ;
31
this.age = age;
32
}
33
34
public void setCar(Car car){
35
this.car = car;
36
}
37
38
public Car getCar(){
39
return this.car;
40
}
41
42
public String getInfo(){
43
return "person name :" +this.name+", person age:" +this.age;
44
}
45
}
46
public class ArrayDemo{
47
public static void main(String args []){
48
49
Person p1 =new Person("cirry", 18);
50
Car c1 =new Car("audi", 400000);
51
p1.setCar(c1);
52
c1.setPerson(p1);
53
54
System.out.println(p1.getCar().getInfo());
55
System.out.println(c1.getPerson().getInfo());
56
}
57
}

两个类的互相引用和配置

1
class Dept{
2
private String name;
3
private String loc;
4
private Emp emps []; // 所有员工
5
6
public Dept(String name , String loc){
7
this.name = name ;
8
this.loc = loc;
9
}
10
11
public void setEmps(Emp [] emps){
12
this.emps = emps;
13
}
14
15
public Emp [] getEmps(){
60 collapsed lines
16
return this.emps;
17
}
18
19
20
public String getInfo(){
21
return "dept name:"+this.name+", dept loc"+this.loc;
22
}
23
}
24
25
class Emp{
26
private String name;
27
private int age;
28
private Dept dept; //所属部门
29
private Emp mgr; //所属领导
30
31
public Emp(String name , int age){
32
this.name = name ;
33
this.age = age;
34
}
35
36
public void setDept(Dept dept){
37
this.dept =dept;
38
}
39
public void setMgr(Emp mgr){
40
this.mgr = mgr;
41
}
42
43
public Dept getDept(){
44
return this.dept;
45
}
46
47
public Emp getMgr(){
48
return this.mgr;
49
}
50
51
public String getInfo(){
52
return "雇员姓名 :" +this.name+",雇员年龄 :"+this.age;
53
}
54
}
55
public class ArrayDemo{
56
public static void main(String args []){
57
Dept dept = new Dept("财务部","合肥");
58
Emp empA = new Emp("菜菜",18);
59
Emp empB = new Emp("学学",18);
60
Emp empC = new Emp("瑞瑞",18);
61
empA.setDept(dept);
62
empB.setDept(dept);
63
empC.setDept(dept);
64
empA.setMgr(empB);
65
empB.setMgr(empC);
66
dept.setEmps(new Emp []{empA,empB,empC});
67
68
System.out.println(dept.getInfo());
69
for(int x = 0 ; x < dept.getEmps().length; x++){
70
System.out.println("\t|-"+dept.getEmps()[x].getInfo());
71
if(dept.getEmps()[x].getMgr()!= null)
72
System.out.println("\t\t|-"+dept.getEmps()[x].getMgr().getInfo());
73
}
74
}
75
}

子类覆写

1
class Person{
2
private String name;
3
private int age;
4
public Person(String name , int age){
5
this.name = name;
6
this.age = age;
7
}
8
@Override
9
public String toString(){
10
return "name:"+this.name+"age:"+this.age;
11
}
12
}
13
14
public class duotai{
15
public static void main(String args []){
6 collapsed lines
16
Person per = new Person("dog", 18);
17
System.out.println(per);
18
System.out.println(per.toString());
19
20
}
21
}
本文标题:类之间的相互继承
文章作者:Cirry
发布时间:2019-09-24
版权声明:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
感谢大佬送来的咖啡☕
alipayQRCode
wechatQRCode