结合老师复习给我们说的必考知识点+代码,我对应整理出一份复习笔记。
1.java概述
1.面向对象程序设计语言 基本特征:继承、封装、多态
封装 (Encapsulation)
我们把属性设为 private,不让外部直接修改,而是通过 public 方法(Getter/Setter)来控制
class Animal {
private String name; // 私有属性,保护数据
public void setName(String name) {
if(name != null) { // 这里可以加入判断逻辑,防止乱赋值
this.name = name;
}
}
public String getName() { return name; }
}
封装起来了,只有有权限的人才能打开
2. 继承 (Inheritance)
Dog 类通过 extends 继承了 Animal,直接拥有了 name 属性和方法,不用再写一遍。
// Dog 类继承了 Animal 类
class Dog extends Animal {
public void bark() {
System.out.println(getName() + " 在汪汪叫!");
}
}
3. 多态 (Polymorphism)
我们给 Animal 定义一个通用的 makeSound 方法。
无论什么动物,只要它是 Animal 的子类(这就是刚刚说的继承),都能有对应的叫法。
abstract class Animal {
public abstract void makeSound(); // 抽象方法
}
class Dog extends Animal {
public void makeSound() { System.out.println("汪汪!"); }
}
class Cat extends Animal {
public void makeSound() { System.out.println("喵喵!"); }
}
public class Main {
public static void main(String[] args) {
// 多态:左边是父类类型,右边是具体的子类对象
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.makeSound(); // 输出:汪汪!
myCat.makeSound(); // 输出:喵喵!
// 核心:调用的是同一个方法名,但行为完全不同
}
}
2. jdk命令
javac (Java Compiler):负责把"人类语言"变成"机器/JVM 语言"。这就是 编译。
执行编译 :
javac Main.java(此时,文件夹里多出了一个Main.class文件,这就是编译的结果)执行运行 :
java Main(控制台输出:Hello, World!)
2.java语法基础
1. 修饰符
修饰符->用来规定权限
【刚刚有提到的(object-oriented 语言特征)封装就是用了修饰符】
-
访问控制:
修饰符 同一个类 同一个包 不同包的子类要继承它 其他包 (全局) public✅ ✅ ✅ ✅ protected✅ ✅ ✅ ❌ (默认/无修饰符) ✅ ✅ ❌ ❌ private✅ ❌ ❌ ❌ -
public:谁都能进(公共)。 -
private:仅自己能看(私有)。 -
protected:家族内可见(包与子类)。
-
-
非访问控制(行为):
-
static:属于类,大家共有。class Student { String name; // 普通变量(每个学生都有自己的名字) static String schoolName = "清华大学"; // 静态变量(大家共享同一个校名) public Student(String name) { this.name = name; } } public class Main { public static void main(String[] args) { // 创建两个学生对象 Student s1 = new Student("张三"); Student s2 = new Student("李四"); // 此时: // s1 有自己的 name="张三" // s2 有自己的 name="李四" // 但他们共享同一个 schoolName="清华大学" // 如果校名改了,所有人的校名都会变 Student.schoolName = "北京大学"; System.out.println(s1.name + "的学校是:" + s1.schoolName); // 输出:张三的学校是:北京大学 System.out.println(s2.name + "的学校是:" + s2.schoolName); // 输出:李四的学校是:北京大学 } } -
final:定死了,不可更改。 -
abstract:框架而已,没具体实现。
-
2.数据类型转换
强制类型转换
public class LXZH {
public static void main(String[] args) {
byte b; //占用 一字节=8位二进制
long data = 306L;
float f = 3.14F;
//加F是为了表示这是一个float(整个数字保留6-7位);不加表示double(整个数字保留15-16位)
int i,j; //占用 三字节=32位二进制
// ()运算符->强制类型转换
i = (int) data; //306L在int类型的取值范围内,不会丢失数据
j = (int) f; //3.14->3(小数点后面全部清除,直接截断)
b = (byte) i; //i被变成了306=256+50,转换成二进制(1 00110010)
// int转换成byte,直接取二进制后八位->00110010 = 50 ->b = 50
System.out.println(b);
}
}
3.算数运算符
import java.util.Scanner;
public class b1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("stt");
int sh = sc.nextInt();
int sm = sc.nextInt();
System.out.println("edt");
int eh = sc.nextInt();
int em = sc.nextInt();
int s = sh*60+sm;
int e = eh*60+em;
int diff = e-s;
int dh = diff/60;
int dm = diff%60;
System.out.println("时间差为: " + dh + "小时 " + dm + "分钟"); //println()只能接收一个参数;如果你想输出两个变量,需要使用(+)连接。
}
}
4.流程控制
public class b2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入成绩");
int g;
g = sc.nextInt();
char gg;
if(g>=90){
gg='A';
}else if(g>=80){
gg='B';
}else if(g>=70){
gg='C';
}else if(g>=60){
gg='D';
}else{
gg='E';
}
System.out.println(gg);
}
}
5.break语句
public class breakyuju {
public static void main(String[] args) {
int i,j;
outer:
//外层循环,使用了outer标签
for(int k=0;k<5;k++){
i = k+2;
j = 5*i;
for(int m=0;m<5;m++){
if(j%10 ==0){
System.out.println("i="+i+",j="+j);
break outer;//跳出的是outer标签所在的循环
}
}
}
}
}
5.方法
1. 方法的定义结构
public static int add(int a, int b) {
int sum = a + b;
return sum;
}
-
修饰符 :比如
public,static(我们刚学过),规定了谁能调用它。 -
返回值类型 :方法运行完后吐出来的数据类型(如
int,String,如果不需要返回则写void)。 -
方法名 :这台"机器"的名字,遵循"驼峰命名法"(如
calculateSum)。 -
参数列表:
-
方法体(Body) :在大括号
{}中,具体的执行逻辑。
import java.util.Scanner;
import static java.lang.Math.pow;
public class b3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入a,n");
int a,n;
a = sc.nextInt();
n = sc.nextInt();
System.out.println(sum(a,n));
}
//求 重叠数 和
public static double sum(int a,int n){
double res=0;
for(int i=1;i<=n;i++){
res+= get(a,i);
}
return res;
}
//构造 重叠数
public static double get(int a,int n){
double numb = 0;
for(int i=0;i<n;i++){
numb = numb*10 + a;
//numb += a * Math.pow(10,i);
}
return numb;
}
}
2. 方法重载
public class chongzai {
public static void main(String[] args) {
int m = 3,n = 10;
double x = 2.0,y = 8.0;
System.out.println(add(m,n));
System.out.println(add(m,y));
System.out.println(add(y,n));
System.out.println(add(x,y));
}
public static double add(int a,int b){
return a+b;
}
//重载add方法
public static double add(double a,int b){
return a+b;
}
public static double add(double a,double b){
return a+b;
}
public static double add(int a,double b){
return a+b;
}
}
6.数组
1.初始化
静态初始化
int[] data = {1,2,3};
String[] s = {"hello", "halo"};
int week = new int[]{1,2,3};
动态初始化
int[] arr = new int[50];
int arr[] = new int[50];
2.多维数组
public class b4 {
public static void main(String[] args) {
int[][] arr = {{81,72,93},{64,55,96,87,78},{79,100}};
for(int i = 0; i<arr.length; i++){
double sum=0.0;
for(int j=0;j<arr[i].length;j++){
sum+= arr[i][j];
}
double avg = sum/arr[i].length;
System.out.println("avg"+i+"="+avg);
}
System.out.println("===================");
int i=0;
for(int[] temp:arr){
double s=0;
for(int n:temp){
s+=n;
}
double a = s/temp.length;
System.out.println("avg"+(i++)+"="+a);
}
}
}
7.scanner
import java.util.Scanner;
public class scan {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
int w = sc.nextInt();
int area = getS(l,w);
System.out.println(area);
}
static int getS(int ll, int ww){
return ll*ww;
}
}
3.面向对象基础
1.this关键字
public class thisis {
public static void main(String[] args) {
Student s = new Student("a",18);
System.out.println(s.age+"and"+s.name);
}
}
class Student {
int age;
String name;
public Student(String n,int a){
this.age = a;
this.name = n;
}
}
4.面向对象高级技术
1.继承基础
public class inheritage {
public static void main(String[] args) {
Student2 s = new Student2();
s.name = "a";
s.age = 12;
s.grade = "1年";
s.info();
s.work();
s.study();
}
}
class Person{
String name;
int age;
public void work(){
System.out.println("go");
}
public void info(){
System.out.println("name:"+name+" ,age:"+age);
}
}
class Student2 extends Person{
String grade;
public void study(){
System.out.println("study");
}
}
2.接口
interface BasicAction extends Comparable {
void add(); // 只定标准,不管实现
void delete(); // 只定标准,不管实现
}
// 办事员:我签了合同,我现在来具体干活
class Task implements BasicAction {
public void add() {
System.out.println("我用数据库添加了记录"); // 这是具体实现
}
public void delete() {
System.out.println("我从硬盘里删除了文件"); // 这是具体实现
}
}
5.java API
1.字符串方法
public class StringMethodsDemo {
public static void main(String[] args) {
// 1. 初始化字符串
String s = "Hello Java World";
// 2. 长度计算 (length)
System.out.println("字符串长度: " + s.length()); // 16
// 3. 字符定位 (charAt) - 取出索引为 0 的字符
System.out.println("第 1 个字符: " + s.charAt(0)); // 'H'
// 4. 查找字符位置 (indexOf) - 查找 'J' 的位置
System.out.println("'J' 的下标位置: " + s.indexOf('J')); // 6
// 5. 裁剪字符串 (substring) - 取出 "Hello" (从0到5,不含5)
System.out.println("截取前 5 位: " + s.substring(0, 5)); // "Hello"
// 6. 比较内容 (equals) - 注意:不要用 ==
String s2 = "hello java world";
System.out.println("忽略大小写比较: " + s.equalsIgnoreCase(s2)); // true
System.out.println("严格大小写比较: " + s.equals(s2)); // false
// 7. 转换大小写
System.out.println("全大写: " + s.toUpperCase()); // HELLO JAVA WORLD
System.out.println("全小写: " + s.toLowerCase()); // hello java world
// 8. 字符串替换
System.out.println("替换字符: " + s.replace("Java", "World")); // Hello World World
// 9. 去除首尾空格
String s3 = " Java ";
System.out.println("去空格前: '" + s3 + "'");
System.out.println("去空格后: '" + s3.trim() + "'");
}
}
2.数值与随机数
public class b5 {
public static void main(String[] args) {
System.out.println("四位数字"+ncode());
System.out.println("四位数字"+scode());
}
public static String ncode(){
Random rd = new Random();
int num = rd.nextInt(10000);
while(true){
if(num<1000){
num = rd.nextInt(10000);
}else{
break;
}
}
return String.valueOf(num);
}
public static String scode(){
Random rd = new Random();
String s = "abcdefghijklmnopqrstuvwxyz0123456789";
String ss = "";
//StringBuilder sb = new StringBuilder("");
for(int i=0;i<4;i++){
int des = rd.nextInt(36);
ss+= s.charAt(des);
//int index = rd.nextInt(36);
//sb.append(s.charAt(index));
}
return String.valueOf(ss);
}
}
6.异常
public class ExceptionDemo {
public static void main(String[] args) {
int a = 10;
int b = 0;
try {
// 1. 试着做这件事
System.out.println("准备开始除法运算...");
int result = a / b; // 这里会报错,因为不能除以 0
System.out.println("结果是: " + result);
} catch (ArithmeticException e) {
// 2. 如果出错了,在这里处理
System.err.println("发生错误啦:不能除以 0!");
} finally {
// 3. 无论是否出错,这里一定会执行
System.out.println("无论如何,程序运行结束。");
}
}
}
7.java o/i流
1.. 字符流代码示例(读取文本文件)
这是读取 .txt 文件的标准写法。
Java
import java.io.*;
public class ReadTextFile {
public static void main(String[] args) {
// 使用 FileReader 读取文件
// try-with-resources 会自动关闭流,考试写这个最加分
try (FileReader fr = new FileReader("test.txt")) {
int ch;
// 一个一个字符地读,读到 -1 表示结束
while ((ch = fr.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException e) {
System.out.println("文件读取出错了!");
e.printStackTrace();
}
}
}
- 字节流代码示例(复制图片/视频)
这是处理"非文本"文件的标准写法,使用 byte[] 数组作为缓冲区,效率更高。
import java.io.*;
public class CopyFile {
public static void main(String[] args) {
// 字节流:InputStream 读取,OutputStream 写入
try (FileInputStream fis = new FileInputStream("source.jpg");
FileOutputStream fos = new FileOutputStream("copy.jpg")) {
// 定义一个缓冲区(搬运的“篮子”),一次搬 1024 个字节
byte[] buffer = new byte[1024];
int len;
// 循环读取直到没数据
while ((len = fis.read(buffer)) != -1) {
// 将读取到的内容写入目标文件
fos.write(buffer, 0, len);
}
System.out.println("文件复制成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
8.多线程
1.线程创建方法
1.继承 Thread 类
// 1. 继承 Thread
class MyThread extends Thread {
@Override
public void run() {
System.out.println("我是并行跑的一个任务!");
}
}
// 2. 启动
public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start(); // 必须调用 start(),不能直接调 run()
}
}
2.实现 Runnable 接口
// 1. 实现 Runnable
class MyTask implements Runnable {
@Override
public void run() {
System.out.println("这是另一种跑任务的方法!");
}
}
// 2. 启动
public class Main {
public static void main(String[] args) {
Thread t = new Thread(new MyTask());
t.start();
}
}
2.线程生命周期
| 状态 | 触发条件 | 关键点 |
|---|---|---|
| New (新建) | new Thread() |
刚创建,尚未调用 start() |
| Runnable (就绪) | 调用 start() |
已准备好,等待 CPU 分配时间片 |
| Running (运行) | CPU 选中 | 正在执行 run() 方法中的代码 |
| Blocked (阻塞) | sleep()、IO 操作、锁 |
线程被迫暂停,障碍消除后回到"就绪" |
| Dead (死亡) | run() 执行完毕 |
线程任务结束,不可逆(不能重开) |
启动规则 :只有 start() 才能开启线程。调用 run() 只是普通函数执行,无并行效果。
状态不可逆 :线程一旦进入 Dead 状态,再次调用 start() 会抛出 IllegalThreadStateException 异常。
CPU 的分配 :从 Runnable 到 Running 是由操作系统调度决定的,代码无法直接控制,这是考试理论题常考点。
阻塞与死亡 :sleep() 会进入 Blocked(阻塞),醒来后回到 Runnable,而不是直接跳回 Running。
9.java gui
Graphical User Interface,翻译过来就是"图形用户界面"。
Java GUI(Java 图形用户界面)是指使用 Java 语言开发的、以图形化方式与用户交互的程序界面。与传统的命令行界面(CLI)相比,GUI 通过窗口、按钮、标签、文本框等可视化元素,使用户能够更直观、友好地操作程序 CSDN博客。
