-
德摩根定律
! (a && b) = !a || !b
! (a || b) = !a && !b
-
else
就近原则嵌套使用时,
else if
和else
遵循就近原则,和它上面最近的if
语句匹配 -
type casting 类型转换
-
automatic / implicit casting 隐式类型转换
double x = 2.0; int n = 5; x = n;
-
explicit casting 显式类型转换
double x = 2.0; int n = 5; n = (int)x;
-
除法
被除数、除数中有一个
double
类型就不是整除
-
-
round-off error 舍入误差
由于计算机有效数位的限制,在计算过程中产生的误差
0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 == 0.1 * 6 // false
-
error
- compile-time error 编译错误
- runtime error 运行错误
-
declare arrays
int a[], b, c[], d; // a、c是数组 int []e, f, g, h; // 都是数组 int i[][] = new int[3][2]; // 二维数组 int i[][] = {{1,2},{3,6},{7,8}};
When the brackets come before the first variable, they apply to all variables in the list.
-
数组开辟新的空间
int[] myArray = new int[10]; myArray = new int[500]; // 合法,因为数组的大小是值的一部分,而不是类型 double[] dub = new int[10]; // 不合法
-
for each 循环
for (int num:a) { System.out.print(num + " "); }
- 无需知道数组的个数,只需知道类型即可,但是无法获取索引
- 不可修改基本类型数组
- 可以对引用类型数组(除
String
外)中的元素赋值,因为 for each 循环传递的是内存地址;但不可以删除或添加数组元素,也不可以让数组元素指向新地址
-
generic 泛型类
举例:
ArrayList<E>
-
class 类
-
instance 实例
-
instance variable 实例变量
-
access specifier
-
public
: 全局都能使用 -
private
: 只能在当前类里使用
-
-
数组的
length
是一个变量,所以a.length
是不加括号的 -
带
this
的是实例变量的名称,不带的是参数名。但是不加this
也能通过编译public void setName(String name) { name = name; // 合法,但是可读性差 this.name = name; // 合法,可读性好 }
-
-
static variable 静态变量
-
final
-
只能被赋值一次,赋值后不能更改
final int []y = new int[10]; y[0] = 100; // 合法 y = new int[5]; // 不合法
-
-
static
- 被所有实例共享
- 静态方法同理
-
-
method 方法
-
ArrayList
的size()
是个方法,所以list.size()
是加括号的 -
constructors 构造方法
- 初始化实例
- 函数名与类名相同,不用定义返回值类型,不写
return
- 可以有参,可以无参
-
允许方法重载
public account() { password = "123456"; balance = 0; } public account(String password, double balance) { this.password = password; this.balance = balance; }
-
-
method overloading 方法重载
同名不同参(个数、类型、顺序),返回类型随意
-
mutators 修改方法
会修改变量值的方法
-
accessors 访问方法
只获取而不修改变量值的方法
toString()
- 可以让你在客户程序中,通过调用实例,返回提前设置好的句子(如一些参数)
- 不设置,默认输出:类名@一串数字
-
Inheritance 继承:superclass & subclass 超类(父类)与子类
-
子类类名 +
extends
+ 父类类名 -
子类继承父类的属性和方法,除了构造方法和
private
-
super
- 可以调用父类的方法
- 若调用构造方法,必须在子类构造方法的第一行使用,可以省略
-
overriding 方法重写
-
is-a relationship
Person p = new Student(); // 合法 ((Student)(p)).getId(); // 父类型变量p不能直接使用子类的东西,必须强制类型转换 Student s = new Person(); // 不合法
-
Object
-
所有类的父类
-
equals()
public boolean equals(Object obj) { return (this == obj); // 只能比较地址,等价于==,因此一般重写本方法 }
-
toString()
public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashcode()); // 一般重写 }
-
-
-
Polymorphism 多态
- 条件:继承、方法重写、父类引用指向子类对象
- 调用子类重写后的方法
-
client program / class 客户程序
调用其他类的程序
-
abstract class 抽象类
-
可以有抽象方法和具体(concrete)方法
-
不能创建对象,但可以定义变量
Shape a = new Shape(); // 不合法 Shape c = new Circle(); // 合法
-
必须作为其他类的父类,并且子类要全部实现父类的抽象方法,否则也要声明为抽象类
-
-
interface 接口
-
支持多重继承,即一个类有多个父类
public class subclass extends superclass implements interface1, interface2, interface3 {}
-
定义:
interface name { // write something }
-
所有方法都是
abstract
public
的,且不需提供关键字 -
没有构造方法
-
不能创建对象,但可以定义变量
-
抽象类可以继承接口,但接口不能继承类
-
-
-
数据类型
-
primitive data types 基本数据类型
- 包括
boolean
、Integer
、double
等 - 传递时,拷贝变量的值给目标变量
==
比较的是值
- 包括
-
reference data types 引用数据类型
- 包括类、接口、数组等
- 传递时,地址传达给目标变量,之后改变互相影响。一旦某变量重新指向新地址(被重新
new
),则两变量不再相关 - 初值是
null
==
比较的是指向的内存地址
-
-
wrapper 包装类
基本数据类型 包装类 boolean Boolean int Integer double Double - 整型最大最小值
System.out.println(Integer.MAX_VALUE); // 2147483647 System.out.println(Integer.MIN_VALUE); // -2147483648
-
String
-
length()
字符串的长度
-
equals()
判断两个字符串是否相等
-
compareTo()
逐位对 ASCII 码求差的绝对值,返回绝对值之和
-
indexOf()
查找特定字符(串)在原字符串中的起始位置,若不存在返回 -1
-
substring(x)
原字符串的 [x, length) 子串
-
substring(x, y)
原字符串的 [x, y) 子串
-
-
Math
-
abs()
-
pow(base, exp)
- base > 0 或 base = 0 and exp > 0 或 base < 0 and exp 是整数
-
sqrt()
-
random()
-
产生 [0,1] 之间的随机数
-
[a, b) 随机数
(b-a)*Math.random()+a;
-
[a, b] 随机整数
(int)((b-a+1)*Math.random())+a;
-
-
-
二分查找
- 查找所需次数:大于log2 n的最小整数
AP计算机科学A 笔记
最新推荐文章于 2024-06-26 18:33:25 发布