目录
进制🚪
System.out.println() 都是以十进制形式输出 | |||
---|---|---|---|
二进制 | 以 0b 开头 | 0b10 | 2 |
八进制 | 以 0 开头 | 010 | 8 |
十六进制 | 以 0x 开头 | 0x10 | 16 |
进制转换
二进制 ----> 八/十六
1 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 1 | 1 |
倒着来 每三位转成10进制形式 不够三位补齐0 八进制为 1243
转十六进制也是如此 每四位转成10进制形式 不够四位补齐 十六进制为 2a3
八/十六 ----> 二
把每一位 拆成三位/四位即可
java中的函数调用
十进制 -------> 二 八 十六 进制 | |
十 ----> 2 | String b = toBinaryString() |
十 ----> 8 | String b = toHexString() |
十 ----> 16 | String b = toOctalString() |
串
串查找
indexOf(str)
//首次出现下标
String s = "Hello World";
System.out.println(s.indexOf("llo"));
lastIndexOf(str)
//最后出现下标
String s = "Hello World";
System.out.println(s.lastIndexOf("o"));
获取指定位置的字符
String s = "Hello World";
System.out.println(s.charAt(4));
串的截取
substring(start,end)
// [)
String s = "Hello World";
System.out.println(s.substring(2,4));
去除首尾空格
trim()
String s = " Hello World ";
System.out.println(s.trim());
串的替换
replace(old,new)
//替换全部的"Hello"
String s = "Hello World";
System.out.println(s.replace("Hello","你好"));
判断串的开头和结尾
startsWith()
String s = "Hello World";
System.out.println(s.startsWith("Hello"));
endsWith()
String s = "Hello World";
System.out.println(s.endsWith("World"));
串是否相等
equals()
String s = "Hello World";
String a = "Hello World";
System.out.println(a.equals(s));
equalsIgnoreCase()
//不区分大小写
String s = "Hello World";
String a = "hello world";
System.out.println(a.equalsIgnoreCase(s));
大小写转换
toLowerCase()
String s = "Hello World";
System.out.println(s.toLowerCase());
toUpperCase()
String s = "Hello World";
System.out.println(s.toUpperCase());
数字其他不被影响
串的分割
split()
String s = "H e ll o Wo l d";
String[] a;
//分割次数
//a = s.split(" ",2);
a = s.split(" ");
for(String i :a){
System.out.println(i);
}
数组🚪
定义
int []a = new int[50];
int a[] = new int[50];
int a[] = new int[]{1,5,8,7};
int a[][] = new int[5][];//√
int a[][] = new int[][5];//×
String a[] = new String[50];//二维串
for (int i = 0; i < 3; i++) {
a[i] = cin.next();
}
for (int i = 0; i < 3; i++) {
for(int j = 0; j < a[i].length(); j++){
System.out.println(a[i].charAt(j));
}
}
int a[][] = new int[50][50];//二维int
.length()//长度
数组赋值
int[] a,b;
a = new int[]{1,2,3,4,5,6};
b=a;//只是修改了数组的首地址
for (int i = 0; i < b.length; i++)
System.out.println(b[i]);
可变个数形参
这种有多个相同类型的参数 在方法中可以用 ... 代替
他们会自动进去到一个数组中
public class Main {
public static void main(String[] args) {
Main test = new Main();
test.pp(1,2,3,4,5);
}
public void pp(int... x){
for(int i=0;i<x.length;i++) System.out.println(x[i]);
}
}
常见Arrays类
java.util.Arrays | |
boolean f = Arrays.equals(a,b) | 判断两个数组是否相等 |
Arrays.sort(b) | 对数组从小到大排序 |
Arrays.fill(c,50) | 将50填到数组中 |
String ss = Arrays.toString(a) | 输出数组信息 |
int d = Arrays.binarySearch(b,60) | 二分找60(必须有序) |
b = Arrays.copyOf(a,5) | 将数组a前5个给b |
面向对象🚪
类和对象
个人理解 这相当于C语言中的结构体
new 一个类 这个就被称为对象
public class Main {
public static void main(String[] args) {
student student1 = new student();//这里其实就是new 了一个对象
// student1就是一个对象
student1.name = "liming";
student1.doing();
student1.age = 60;
System.out.println(student1.age);
}
}
class student{
//属性
String name;
int age = 50;
//方法
public void doing () {
System.out.println(this.name + " 学会习");
}
}
此时 首先new了一个对象 然后 在这个对象上 又创建一个对象
这个时候 student1 student2这两个对象 共用一个对象 修改其中一个 两个都变
student student1 = new student();
student student2 = student1;
方法的重载
当你想让一个方法名有两个及两个以上 的含义时,可以考虑使用方法重载
根据方法的参数的个数、类型、顺序决定采用哪个方法。
public class Main {
public static void main(String[] args) {
math math = new math();
math.ad(5);
}
}
class math{
public void ad(int a,int b){
System.out.println(a + b);
}
public void ad(int a){
System.out.println(a);
}
}
封装
封装原则
将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问
成员变量private,提供对应的getXxx()/setXxx()方法设计思想
该暴露的暴露,该隐藏的隐藏,对于用户 他们只需要知道干嘛的就行了,不必要了解内部运行。
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
pack pack = new pack();
System.out.println(pack.getA());
pack.setA(60);
System.out.println(pack.getA());
}
}
class pack{
/**
* private 修饰下的变量 不允许直接调用
* 可以通过构造函数来控制变量
* alt + insert 快捷键
*/
private int a = 20;
private int b = 57;
public int getA() {
return a;
}
public void setA(int a){
this.a = a;
}
public void st(){
System.out.println("srthsthsrt");
}
}
权限修饰符
可以调用的前提 | ||||
修饰符 | 在类内部 | 在同一个包 | 在不同包的子类 | 在同一个工程 |
private | YES | |||
(不写) | YES | YES | ||
protected | YES | YES | YES | |
public | YES | YES | YES | YES |
**class只能用后两者修饰**
构造器
令方法名类名一致 便于初始化对象
当初始化对象时 不用构造器有如下:
public class Main {
public static void main(String[] args) {
glass g = new glass();
g.pp(50,"liming");
System.out.println(g.age + g.name);
}
}
class glass{
int age;
String name;
void pp(int a,String n){
this.age = a;
this.name = n;
}
}
使用构造器 有如下:
public class Main {
public static void main(String[] args) {
glass g = new glass(50,"liming");
System.out.println(g.age + g.name);
}
}
class glass{
int age;
String name;
public glass(int a,String n){
age = a;
name = n;
}
}
对比可知 当初始化对象时 使用构造器时 比较简单
public class Main {
public static void main(String[] args) {
glass p = new glass();
p.cc();
glass p1 = new glass(50);
p1.cc();
}
}
class glass{
int age;
String name;
//构造器(多个构造器彼此成为重载)
public glass(){
System.out.println("shsths");
}
public glass(int n){
this();//构造器调用构造器
this.age = n;
}
public void cc(){
System.out.println(age + name);
}
}
继承与多态🚪
继承
继承允许一个类成为另一个类的子类,子类继承了父类的所有特性,并且可以扩展出自己的特征 Objest是所有类的父类
public class Main {
public static void main(String[] args) {
son son = new son();
System.out.println(son.age);
}
}
class father {
int age = 83;
String name = "liming";
}
class son extends father{//子类继承父类
}
重写
子类中定义一个方法,并且这个方法的名字,返回类型,参数个数,和类型与从父类继承的方法完全相同。子类通过方法的重写可以隐藏继承的方法,进而再次使用时 ,调用的就是 重写后的方法
重写时 子类的权限修饰符/返回类型 必须 大于等于 父类的权限修饰符/返回类型
super
public class father {
int age = 83;
public void cc(){
System.out.println("cccccccccccc");
}
public void bb(){
System.out.println("haiahiahiahi");
}
String name = "liming";
}
public class son extends father{//子类继承父类
int age = 60;
public void bb(){
System.out.println("bbbbbbbbb");
}
public void aa(){
System.out.println(this.age);//调用自己
System.out.println(super.age);//调用父类
System.out.println(age);//默认调用自己
/**同理
* this.bb();
* super.bb();
* bb();
*/
cc();//子类中没有重写则三种都可以
}
}
引用构造器
this 和 super 引用构造器时,必须在子类构造器的第一行。
所以 this 和 super 二者只能选一个
public class Main {
public static void main(String[] args) {
son son = new son(50,"liming");
son.dd();
}
}
public class father {
int age;
String name;
public father(int age){
this.age = age;
}
public father(int age,String name){
this(age);//引用自己类中构造器的father(age)
this.name = name;
}
}
public class son extends father{
public son(int age,String name){
super(age,name);//引用父类中的构造器father(age,name) 且必须在第一行。
}
public void dd(){
System.out.println(age + "岁的" + name);
}
}
多态
定义
new一个子类 定义为父类
编译看左 运行看右
编译:只能引用父类中存在的方法。
运行:运行子类中的方法。
public class Main {
public static void main(String[] args) {
father son1 = new son();
son1.bb();
}
}
public class father {
public void aa(){
System.out.println("该吃饭了");
}
public void bb(){
System.out.println("liming");
}
}
public class son extends father{
public void aa(){
System.out.println("还没到吃饭时间");
}
}
有什么用
简单点说:“一个接口,多种实现”,就是同一种事物表现出的多种形态。
1、提高了代码的维护性(继承保证)
2、提高了代码的扩展性(由多态保证)
3、把不同的子类对象都当作父类来看,可以屏蔽不同子类对象之间的差异,写出通用的代码,做出通用的编程,以适应需求的不断变化。
使用多态:
public class Main {
public static void main(String[] args) {
Main f = new Main();
f.fun(new student());
f.fun(new liming());
f.fun(new xiaoli());
}
public void fun(student person){
person.age();
person.name();
}
}
class student{
public void name(){
System.out.println("liming");
}
public void age(){
System.out.println("今年 10 岁了");
}
}
class liming extends student{
public void name(){
System.out.println("liming");
}
public void age(){
System.out.println("今年 22 岁了");
}
}
class xiaoli extends student{
public void name(){
System.out.println("xiaoli");
}
public void age(){
System.out.println("今年 18岁了");
}
}
不用多态:
public class Main {
public static void main(String[] args) {
Main f = new Main();
f.fun(new student());
f.fun(new liming());
f.fun(new xiaoli());
}
public void fun(student person){
person.age();
person.name();
}
public void fun(liming person){
person.age();
person.name();
}
public void fun(xiaoli person){
person.age();
person.name();
}
}
class student{
public void name(){
System.out.println("liming");
}
public void age(){
System.out.println("今年 10 岁了");
}
}
class liming extends student{
public void name(){
System.out.println("liming");
}
public void age(){
System.out.println("今年 22 岁了");
}
}
class xiaoli extends student{
public void name(){
System.out.println("xiaoli");
}
public void age(){
System.out.println("今年 18岁了");
}
}
很明显 没有使用多态时 代码较多,且重复。
当仅引用子类在父类中存在的方法时
可以使用多态 ,只需要定义一次即可。
多态只适用于方法,不适用于属性(属性没有重写 编译运行都看左边)
public class Main {
public static void main(String[] args) {
one s1 = new two();
two s2 = new two();
System.out.println(s1.a);
System.out.println(s2.a);
}
}
class one{
int a = 50;
}
class two extends one{
int a = 60;
}
如何调用子类特有的方法
理解为类型 的 强制转换(向下转型)(子父类才能强转)
public class Main {
public static void main(String[] args) {
father a = new son();
son b = (son)a;
b.bb();
}
}
class father {
public void aa(){
System.out.println("haiahi");
}
}
class son extends father{
public void aa(){
System.out.println("lalalalalal");
}
public void bb(){
System.out.println("666666");
}
}
instanceof
强制转换时 可能出现ClassCastException的异常
于是出现instanceof关键字
a instancdof A : 判断a是否为A的实例 是返回true 否返回false
(如果 a = new A) return ture;
public class Main {
public static void main(String[] args) {
father a = new son();
// a.bb();无法调用自己的方法,于是有下面的方法
if(a instanceof son){
son b = (son)a;
b.bb();
}
}
}
class father {
public void aa(){
System.out.println("haiahi");
}
}
class son extends father{
public void bb(){
System.out.println("666666");
}
}
static
static表示“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,或者进行静态导包。static的特点:
1、随着类的加载而被加载;
2、优先于对象存在;
3、被所有对象共享。
public class Main {
static int a = 50;//static 对象可以直接调用 否则需要new
int b = 60;
public static void Eat(){
System.out.println("吃饭了");
}
public void Go(){
System.out.println("出去玩会");
}
public static void main(String[] args) {
System.out.println(a);//√
System.out.println(b);//× 无法从静态上下文中引用非静态 变量 b
System.out.println(new Main().b);
Eat();
new Main().Go();
}
}
被所有对象共享
public class Main {
public static void main(String[] args) {
Person.where = "湖北";
Person PersonOne = new Person();
Person PersonTwo = new Person();
System.out.println(PersonOne.where);
System.out.println(PersonTwo.where);
PersonOne.where = "北京";
System.out.println(PersonOne.where);
System.out.println(PersonTwo.where);
PersonTwo.where = "上海";
System.out.println(PersonOne.where);
System.out.println(PersonTwo.where);
}
}
class Person{
static String where;
}
/*输出
湖北
湖北
北京
北京
上海
上海
*/
抽象类
关键字:abstract
① 抽象类中 抽象方法可有可无
② 抽象方法 必须在抽象类中
③ 抽象类不能new对象
④ 如果一个非抽象类是某个抽象类的子类,那么他必须重写父类的抽象方法。
/*Main.java*/
public class Main {
public static void main(String[] args) {
son son = new son();
System.out.println(son.sum(2,5));
System.out.println(son.sub(5,8));
}
}
/*son.java*/
public class son extends father{
int sum(int a,int b){
return a+b;
}
}
/*father.java*/
public abstract class father {
abstract int sum(int a,int b);//子类必须重写
int sub(int a,int b){
return a-b;
}
}
一些理解:实现一些方法时,先在父类中实现大概轮廓(定义方法名)
子类中给出具体的实现方法。
例如 : 在设计地图时,首先考虑地图最重要的轮廓,不必去考虑诸如城市中的街道牌子等细节。细节应当由抽象类的非抽象子类去实现,这些子类可以给出具体的实例,来完成程序功能的具体实现
接口
只有 规范 自己无法写方法。
关键字: interface
一个类通过关键字:implements 声明自己实现一个或多个接口
基本用法
/*Main.java*/
public class Main {
public static void main(String[] args) {
son son = new son("李明");
System.out.println(son.MAX);
son.study();
son.eat();
son.play();
}
}
/*son.java*/
public class son implements father{
String name;
public son(String name){
this.name = name;
}
@Override
public void study() {
System.out.println("好好学习");
}
@Override
public void eat() {
System.out.println("吃点吧");
}
@Override
public void play() {
System.out.println("玩会吧");
}
}
/*father.java*/
public interface father{
//全局常量
public static final int MAX = 999;
//下面省略了 public static final
double PI = 3.14;
public abstract void study();
//下面两个方法都省略了 public abstract
void eat();
void play();
}
接口的多继承
public class Main {
public static void main(String[] args) {
D d = new D();
d.eat();
d.play();
d.study();
}
}
interface A{
void eat();
}
interface B{
void play();
}
interface C extends A,B{
void study();
}
class D implements C{
@Override
public void eat() {
System.out.println("gf");
}
@Override
public void play() {
System.out.println("rys");
}
@Override
public void study() {
System.out.println("sry");
}
}
继承 + 实现接口:
class A extends B implements C,D,E
接口与多态
public class Main{
public static void main(String[] args) {
A a = new B();
a.eat();
a.Test();//❌
}
}
interface A{
void eat();
}
class B implements A {
public void eat() {
System.out.println("多吃点吧!");
}
public void Test(){
System.out.println("hellow world!!");
}
}
异常 🚪
try —— catch —— finally
public class Main {
public static void main(String[] args) {
int[] a = new int[10];
try {
//以下内容 是否存在异常
// 没有异常则会正常执行
System.out.println("1111");//会执行
System.out.println(a[10]);
System.out.println("222222");//不会执行
} catch (ArrayIndexOutOfBoundsException e) {
//捕获 ArrayIndexOutOfBoundsException异常
System.out.println("数组下标越界");
}catch (Throwable E){//和if else一样 依次判断
System.out.println("存在异常");
}
finally{
//无论是否捕获到异常 都会执行
System.out.println("无论是否捕获到异常 都会执行我");
}
}
}
Throws
一般用在方法中 因为 try 中 总不能写一堆代码把 不美观😅😅😅
public class Main {
public static void main(String[] args) {
try{
aaa(4,0);
}catch (ArithmeticException e){//捕获异常ArrayIndexOutOfBoundsException
System.out.println("出现了异常 " + e);
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("数组越界");
}
}
//无法确定异常类型时,可抛出多个类型
public static void aaa(int a,int b) throws ArithmeticException ,ArrayIndexOutOfBoundsException {
System.out.println(a/b);
}
}
主动抛出异常
提前预判异常 主动抛出 配合Throws使用
主动预判异常类型
然后配合Throws向外抛出此异常
这时就不需要向 上面一样 同时抛出多个异常
public class Main {
public static void main(String[] args) {
try{
aaa(4,0);
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("出现了异常 " + e);
}
}
public static void aaa(int a,int b) throws ArithmeticException {
if(b == 0){
throw new ArithmeticException();
}
else System.out.println(a/b);
}
}
自定义异常
略
集合
List
import java.util.*;
public class main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
// 正常 访问 效率高
LinkedList<String> link = new LinkedList<>();
// 链表形式 插入 删除 效率高
list.add("Hello World");
list.add("he");
list.add("hhhhh");
list = new main().Add(list);
System.out.println(list);
// for(String i :list) sout(i)
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
public ArrayList<String> Add(ArrayList<String> list){
list.remove("he");
list.add(1,"666");
list.add("世界 你好");
return list;
}
}
Set
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class main {
public static void main(String args[]){
// 哈希表
Set<String> hashSet = new HashSet<>();
// 有序
Set<Integer> treeSet = new HashSet<>();
hashSet.add("Hello World");
hashSet.add("世界 你好");
hashSet.remove("Hello World");
System.out.println(hashSet);
for(int i=10;i>=5;i--){
treeSet.add(i);
}
System.out.println(treeSet);
// 迭代器输出
Iterator<String> i = hashSet.iterator();
while(i.hasNext())
System.out.println(i.next());
}
}
Map
import java.util.*;
public class main{
public static void main(String []args){
Map<String,Integer> hashMap = new HashMap<>();
Map<String,Integer> treeMap = new TreeMap<>();
// 同理
hashMap.put("Hello World",1);
hashMap.put("世界 你好",2);
System.out.println(hashMap);
// 获取key值
Set<String> getKey = hashMap.keySet();
System.out.println(getKey);
// 获取value值
Collection<Integer> getValue = hashMap.values();
System.out.println(getValue);
// 遍历一起输出
hashMap.forEach((key,value)->{
System.out.println(key + value);
});
// 最多使用
for(Map.Entry<String ,Integer> i: hashMap.entrySet()){
System.out.println(i.getKey() + i.getValue());
}
}
}
I/O
文件
public String getName(); 名字 public String getPath(); 路径 public String[] list(); 列出目录 public Boolean mkdir(); 创建目录 public Boolean exists(); 是否存在目录
public Boolean exists(); 存在 public Boolean ifFile();是否为文件 public String getName(); 名字 public String getPath(); 路径 public Long length(); 文件长度 public Boolean isHidden(); 隐藏
File
创建文件对象
- File file = new File("D:\\2.txt")
- File file = new File("D:\\","666.txt")
import java.io.File;
import java.io.IOException;
public class main{
public static void main(String[] args) {
File file = new File("D:\\2.txt");
if(file.exists()){
System.out.println("怎末会存在呀");
// 文件名字
String name = file.getName();
System.out.println(name);
// 文件长度
long length = file.length();
System.out.println(length);
// 文件是否隐藏
boolean flag = file.isHidden();
System.out.println(flag);
}
else{
try{
file.createNewFile();
System.out.println("ok");
}catch(Exception e){
e.printStackTrace();
}
}
}
}
FileStream
文件读取的基本操作
字节 字节数组
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class main{
public static void main(String[] args) {
File file = new File("D:\\2.txt");
try{
FileOutputStream out = new FileOutputStream(file);
// 将一个字符串转化为一个字节数组byte[]的方法
byte bus[] = "Hello World".getBytes();
out.write(bus);
out.close();
}catch(IOException e){
e.printStackTrace();
}
try{
FileInputStream in = new FileInputStream(file);
byte a[] = new byte[1024];
int len = in.read(a);
System.out.println(new String(a,0,len));
}catch(IOException e){
e.printStackTrace();
}
}
}
FileReader
更适合于中文
import java.io.File;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
//确保文件格式是utf-8
//更适合用于中文
public class main{
public static void main(String[] args) {
System.out.println("你好");
File file = new File("D:\\2.txt");
try{
FileWriter writer = new FileWriter(file);
String s = "世界 你好";
writer.write(s);
writer.close();
}catch(IOException e){
e.printStackTrace();
}
try{
FileReader read = new FileReader(file);
char a[] = new char[1024];
int len = read.read(a);
System.out.println("信息是:"+new String (a,0,len));
}catch(IOException e){
e.printStackTrace();
}
}
}
Lamda表达式
必须满足函数式接口 : 接口 只包含一个抽象类 如下:
interface man{
void hh();
}
public class main {
public static void main(String[] args) {
//man hh = ()-> System.out.println("hhh");
man hh = ()->{
System.out.println("hhh");
};
hh.hh();
}
}
interface man{
void hh();
}