类的组合

基础类

String是Java API中的一个类,表示多个字符。它内部是一个char的数组,提供了若干方法用于方便操作字符串。

String可以用一个字符串常量初始化,必须是双引号。

​

Date是Java API中的一个类,表示日期和时间。它内部是一个long类型的值,提供了若干方法用于操作日期和时间。

1
Date now = new Date();

如上面代码,用无参的构造方法创建一个Date对象,这个对象就表示当前时间。

​

​

图形类

先扩展一下《类的定义》一章中的Point类,为它添加一个“计算改点到另一个点的距离”的方法。

1
2
3
4
public double distance(Point p) {
return Math.sqrt(Math.pow(x-p.getX(),2)+
Math.pow(y-p.getY(),2));
}

设计一个表示线 - Line的类,其中类的属性含有Point类。

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Line {
private Point start;
private Point end;

public Line(Point start,Point end) {
this.start = start;
this.end = end;
}

public double length() {
return start.distance(end);
}
}

Line由两个Point组成,在创建Line时需要两个Point。

使用这个类的代码如下:

1
2
3
4
5
6
7
public static void main(String[] args) {
Point start = new Point(2,3);
Point end = new Point(3,4);

Line line = new Line(start,end);
System.out.println(Line.length());
}

​

​

电商概念

尝试用类描述一下电商系统中的一些基本概念。

电商系统中最基本的有产品、用户和订单:

  • 产品:有产品唯一Id、名称、描述、图片、价格等属性。

  • 用户:有用户名、密码等属性。

  • 订单:有订单号、下单用户、选购产品列表及数量、下单时间、收货人、收货地址、联系电话、订单状态等属性。

产品Product类的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Product {
//唯一id
private String id;

//产品名称
private String name;

//产品图片链接
private String pictureUrl;

//产品描述
private String description;

//产品价格
private double price;
}

用户User类的代码如下:

1
2
3
4
public class User {
private String name;
private String password;
}

订单条目OrderItem类来描述单个产品及选购的数量,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class OrderItem {
//购买产品
private Product product;

//购买数量
private int quantity;

public OrderItem(Product product,int quantity) {
this.product = productl;
this.quantity = quantity;
}

//合计价格
public double computePrice() {
return product.getPrice()*quantity;
}
}

最后是包含全部属性的订单Order类,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Order {
//订单号
private String id;

//购买用户
private User user;

//购买产品列表及数量
private OrderItem[] items;

//下单时间
private Date createtime;

//收货人
private String receiver;

//收货地址
private String address;

//联系电话
private String phone;

//订单状态
private String status;

public double computeTotalPrice() {
double totalPrice = 0;
if(items != null) {
for(OrderItem item : items) {
totalPrice+=item.computePrice(); //计算总金额
}
}
}
}

Order类引用了User类,以及一个订单条目的数组items,定义了一个计算总价的方法。

​

​

引用自己的类

一个类定义中还可以引用它自己。

比如想要描述人以及人之间的血缘关系,用Person类表示一个人,它的实例成员包括其父母孩子,这些成员也都是Person类型。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Person {
//姓名
private String name;

//父亲
private Person father;

//母亲
private Person mother;

//孩子数组
private Person[] children;

public Person(String name) {
this.name = name;
}
}

使用代码如下:

1
2
3
4
5
6
7
8
9
public static void main(String[] args) {
Person daZhongMa = new Person("大仲马");
Person xiaoZhongMa = new Person("小仲马");

xiaoZhongMa.setFather(daZhongMa);
daZhongMa.setChildren(new Person[]{xiaoZhongMa});

System.out.println(xiaoZhongMa.getFather().getName());
}

上面代码先创建了大仲马,然后创建了小仲马,接着调用xiaozhongma的setFather()和dazhongma的setChildren(),设置了父子关系。

栈-地址 栈-内容 堆-地址 堆-内容 含义
0x8000 0x1000 0x1000 小仲马 xiaoZhongMa.name
0x1004 0x1010 xiaoZhongMa.father
0x1008 null xiaoZhongMa.mother
0x100C null xiaoZhongMa.children
0x8008 0x1010 0x1010 大仲马 daZhongMa.name
0x1014 null daZhongMa.father
0x1018 null daZhongMa.mother
0x101C 0x1000 daZhongMa.children

​

​

互相引用的类

定义两个类MyFile和MyFolder,分别表示文件和文件夹。

文件和文件夹都有名称、创建时间、父文件夹,根文件夹没有父文件夹,文件夹还有子文件列表和子文件夹列表。

文件MyFile类代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MyFile {
//文件名称
private String name;

//创建时间
private Date createtime;

//文件大小
private int size;

//上级目录
private MyFolder parent;

public int getSize() {
return size;
}
}

文件夹MyFolder类的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class MyFolder {
//文件夹名称
private String name;

//创建时间
private Date createtime;

//上级文件夹
private MyFolder parent;

//包含的文件
private MyFile[] files;

//包含的子文件夹
private MyFolder[] subFolders;

public int totalSize() {
int totalSize = 0;
if (files != null) {
for(MyFile file : files) {
totalSize+=file.getSize();
}
}
if(subFolders != null) {
for(MyFolder folder : subFolders) {
totalSize+=folder.totalSize(); //计算子文件夹的size
}
}

return totalSize;
}
}

MyFile引用了MyFolder,MyFolder也引用了MyFile,两者组合互相引用。

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2021 Silver Shaded
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信