文章目录
面向对象
- 类(class):具有相同特性的事物的抽象描述,是一组相关属性和行为的集合
- 对象(object):类的具体体现
举例
类:猫科动物
属性:姓名、年龄、颜色
行为:猎、吃、喝、跑
对象:美洲豹
对象属性:颜色 -> 黄底+斑点
对象行为:吃 -> 刺杀鳄鱼
成员变量
public class Hello {
public static void main(String[] args) {
System.out.println(A.classVariate);
System.out.println((new A()).variate);
}
}
class A {
static String classVariate = "静态成员变量";
String variate = "成员变量";
}
方法(method)
构造方法(constructor)
public class Cat {
public Cat() {
System.out.println("类似Python的__init__");
}
}
方法重载(overload)
public class Hello {
void method(String a) {
System.out.println(a);
}
void method(int b) {
System.out.println(b);
}
}
继承
class Animal {}
class Cat extends Animal {}
多态
public class Hello {
public static void main(String[] args) {
// 本态
Animal a = new Animal();
// 多态
Animal c = new Cat();
// 编译看左,运行看右
a.eat(); // 吃
c.eat(); // 吃鱼
//c.attack(); // 编译不能通过
// 类属性没有多态(初学建议:父子类属性不要重名)
System.out.println(c.name); // 动物
}
}
class Animal {
String name = "动物";
void eat() {System.out.println("吃");}
}
class Cat extends Animal {
String name = "猫";
void eat() {System.out.println("吃鱼");}
void attack() {System.out.println("喵爪");}
}
基础语法
public class Hello {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
代码注释(annotation)
/**
* 可以用来自动创建文档的注释
* javadoc -d directory_name A.java
*/
public class A {
// 单行注释
public static void main(String[] args) {
/*
多行注释
*/
System.out.println("Hello, world!");
}
}
关键字(keyword)
Java已定义好的单词,具有特殊含义;
特点:全是小写字母
命名规范
- 英文 数字
_
$
.
- 下划线 不独行
- 数字 不开头
- 区分大小写
- 不重名
- 禁用java关键词
命名规范 | 包 | 类&接口 | 方法&变量 | 常量 |
---|---|---|---|---|
单个单词 | 全小写 | 首字母大写 | 小写 | 全大写 |
多个单词 | 全小写,用. 分隔 | 大驼峰 | 驼峰 | 全大写,用_ 分隔 |
e.g. | user.name | UserName | userName | USER_NAME |
条件(if、switch)
if
…else if
…else
…
int n = 95;
if (n >= 90) {
System.out.print("优秀");
} else if (n >= 60) {
System.out.print("及格");
} else {
System.out.print("挂科");
switch
…case
…case
…
在switch语句中,如果case后面不写break,将出现穿透现象
int quarter1 = 3;
switch (quarter1) {
case 4:
System.out.print("冬<-");
case 3:
System.out.print("秋<-");
case 2:
System.out.print("夏<-");
case 1:
System.out.println("春");
}
// 打印结果:秋<-夏<-春
int quarter2 = 3;
switch (quarter2) {
case 4:
System.out.print("冬");
break;
case 3:
System.out.print("秋");
break;
case 2:
System.out.print("夏");
break;
case 1:
System.out.print("春");
break;
}
// 打印结果:秋
循环(while、for)
while
:先判断,后循环do while
:先循环,后判断
int sum = 0;
int n = 1;
do {
sum = sum + n;
n ++;
} while (n <= 100);
System.out.println(sum); // 5050
for
、foreach
String[] array = {"a", "d", "c", "a", "d", "c", "a", "d", "c"};
for (String i : array) {
System.out.print(i);
}
// foreach打印结果:adcadcadc
break
和continue
here:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.println("" + i + j);
if (i > 0 & j > 0) {
break here;
}
}
}
break打断外层循环,留意
here
,打印结果如下:00
01
02
10
11
here:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j > 0) {
continue here;
}
System.out.println("" + i + j);
}
}
continue继续外层循环,留意
here
,打印结果如下:00
10
20
30
40
异常
- 捕获异常:
try
、catch
、finally
- 抛出异常:
throw
- https://yellow520.blog.youkuaiyun.com/article/details/114661078
try {
int i = 9 / 0;
} catch (ArithmeticException e) {
System.out.println(e);
} finally {
System.out.println("finally");
}
数据相关
基本数据类型
名称 | 二进制位数 | 包装类 | MIN_VALUE | MAX_VALUE | 默认值 (5系下下都有) |
---|---|---|---|---|---|
byte | 8 | java.lang.Byte | -128 | 127 | 0 |
short | 16 | java.lang.Short | -32768 | 32767 | 0 |
int | 32 | java.lang.Integer | − 2 31 -2^{31} −231 | 2 31 2^{31} 231 | 0 |
long | 64 | java.lang.Long | − 2 63 -2^{63} −263 | − 2 63 -2^{63} −263 | 0L |
float | 32 | java.lang.Float | 1.4E-45 | 3.4028235E38 | 0.0f |
double | 64 | java.lang.Double | 4.9E-324 | 1.7976931348623157E308 | 0.0d |
char | 16 | java.lang.Character | 0 | 65535 | 'u0000' |
boolean | false |
1.4E-45
表示:
1.4
×
1
0
−
45
1.4 \times 10^{-45}
1.4×10−45
基本数据类型转换
- 自动类型转换(隐式类型转换)
自动类型转换图
// 自动升级为int
char c1 = '0';
char c2 = 'A';
System.out.println(c1 + c2); // 113
- 强制类型转换(显示类型转换)
//损失精度
int i = (int)3.14;
System.out.println(i); // 3
// 溢出
byte b = (byte)128;
System.out.println(b); // -128
- String相关转换
System.out.println("" + 1 + 23); // 123
运算符(Operator)优先级
优先级 | 类别 | 操作符 | 关联性 |
---|---|---|---|
高 | 后缀 | () [] . | 左到右 |
2 | 一元 | expr++ expr-- | 从左到右 |
3 | 一元 | ++expr --expr + - ~ ! | 从右到左 |
4 | 乘性 | * / % | 左到右 |
5 | 加性 | + - | 左到右 |
6 | 移位 | >> >>> << | 左到右 |
7 | 关系 | > >= < <= | 左到右 |
8 | 相等 | == != | 左到右 |
9 | 按位与 | & | 左到右 |
10 | 按位异或 | ^ | 左到右 |
11 | 按位或 | | | 左到右 |
12 | 逻辑与 | && | 左到右 |
13 | 逻辑或 | || | 左到右 |
14 | 条件 | ? : | 从右到左 |
15 | 赋值 | = += -= *= /= %= >>= <<= &= ^= |= | 从右到左 |
低 | 逗号 | , | 左到右 |
+ + 和 - -
double x = 1.5;
double y = ++x;
System.out.printf("%.2f", x); // 2.50
System.out.printf("%.2f", y); // 2.50
double x = 1.5;
double y = x--;
System.out.printf("%.2f", x); // 0.50
System.out.printf("%.2f", y); // 1.50
数组
public class a {
public static void main(String[] args) {
int[][] arr = {
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9},
};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j]);
}
}
}
}
0123456789
泛型
https://yellow520.blog.youkuaiyun.com/article/details/114996580
ArrayList(类似Python的list)
import java.util.ArrayList;
// 创建ArrayList对象
ArrayList<String> a = new ArrayList<>();
// 添加元素
a.add("2");
a.add("3");
System.out.println(a); // [2, 3]
// 查看
System.out.println(a.get(1)); // 3
// 大小(长度)
System.out.println(a.size()); // 2
// 按索引移除元素
a.remove(1);
// 遍历
for (String s : a) { System.out.println(s); } // 2
HashMap(类似Python的dict)
import java.util.HashMap;
// 创建
HashMap<String, Integer> map = new HashMap<>();
// 添加
map.put("剑圣", 8);
map.put("先知", 6);
map.put("巫妖", 5);
System.out.println(map); // {巫妖=5, 先知=6, 剑圣=8}
// 获取
System.out.println(map.get("剑圣")); // 8
System.out.println(map.get("大法师")); // null
// 移除
System.out.println(map.remove("先知")); // 6
System.out.println(map); // {巫妖=5, 剑圣=8}
// 获取全部键或值
System.out.println(map.keySet()); // [巫妖, 剑圣]
System.out.println(map.values()); // [5, 8]
// 遍历键值对
for (Map.Entry<String, Integer> stringIntegerEntry : map.entrySet()) {
System.out.println(stringIntegerEntry);
} // 巫妖=5 剑圣=8