You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

## 面向对象编程 Object
### 类和对象概述:
Class & Object
类泛指一大类抽象事物,对象特指这类事物中单一个体
类:属性,方法
#### 设计类
```java
public class Phone{
//属性
String name;
double price;
//方法
public void call(){
System.out.println("打电话");
}
}
```
#### 创建对象
```java
Phone p1 = new Phone;
p1.name = "华为";
p1.call();
```
#### 方法:
##### 方法的重载:
同一类中允许存在同一名的不同方法,只要他们的参数列表不同
##### 可变个数形参的方法:
eg:
```java
public void print(int ... nums){
System.out.println("");
}
```
##### 方法的值传递机制
##### 递归
#### package和import的使用
##### package
用于指定当前类存在在哪一个包
##### import
导包
## 封装
隐藏数据。只暴露接口。私有化
特性:高内聚,低耦合
##### 实现
| 修饰符 | 本类内部 | 本包内 | 其他包的子类 | 其他包的非子类 |
| --------- | -------- | ------ | ------------ | -------------- |
| private | √ | × | × | × |
| 缺省 | √ | √ | × | × |
| protected | √ | √ | √ | × |
| public | √ | √ | √ | √ |
### 类的构造器
作用1搭配new关键字创建类的对象
作用2在创建对象时给相关属性赋值
没有显示提供构造器时,会默认生成一个空参的构造器。权限和类的权限相同
一个类中可以有多个构造器,他们之间构成重载
### 属性赋值过程
1. 默认赋值
即给出的默认值
2. 显式赋值
在类中定义时赋值
3. 构造器赋值
4. 通过 对象.方法 赋值
5. 通过 对象.属性 赋值
先后顺序:
1 - 2 - 3 - 4 / 5
### JavaBean
指一个满足以下特征的类
1. 公共的
2. 有一个无参的公共的构造器
3. 有属性又对应的set和get方法
### UML类图
看懂uml类图