文章目录
一、面向对象
1.面向对象的思想
- 面向过程:实现功能的每一步都自己写;
- 面向对象:调用库里的方法;
面向对象的特点:封装、继承、多态;
2.类和对象
类:由属性与行为组成;
- 成员变量:属性,即事物的状态信息;
- 成员方法:行为,即事物能做什么;
方法:类的实例
2.1类的定义格式
public class student {
//成员变量
String name;
int age;
//成员方法
public void qf(){
System.out.println("恰饭");
}
public void xx() {
System.out.println("学习");;
}
}
public class student { //成员变量 //成员方法 }
成员变量: 和定义普通变量一样,不过定义的位置有要求,只能在类中、方法外定义;
成员方法: 和普通方法的定义一样,但要去掉static,后面会讲为什么;
2.2对象的创建
- 通常情况下,一个类并不能直接使用,需要创建一个对象才能调用
- 导包:指出需要使用的类在哪个位置 import 包名称.类名称;
*在同一个包下面的两个类可以不用导入语句;- 创建:类名称 对象名 = new 类名称();
- 使用
*使用成员变量:对象名.成员变量名
*使用成员方法:对象名,成员方法名()
public class DemoStudent {
public static void main(String[] args) {
student stu = new student();
stu.age = 18;
System.out.println(stu.age);
stu.xx();
}
}
3.对象的内存图
3.1一个对象的内存图
- 堆中对象的成员方法保存的是方法区中成员方法的地址值;
- 调用→进栈 , 执行完成→出栈;
3.2两个对象使用同一个方法的内存
- 新New一个对象的话就正常运行,主要看两个引用指向同一个对象的情况;
- 思想和学数组时一样
4.使用对象类型作为方法的参
public class studentPa {
public static void main(String[] args) {
student one = new student();
one.age = 18;//我写的时候写出student.age了,记住必须是对象.XX ,不能直接类.XX
one.name = "fuck";
method(one);
}
public static void method(student param) {//在method方法里需要一个名为param的student类型参数;命名为param只是为了分配内存空间而已,在调用时就被具体实例替代;
System.out.println(param.age);//
}
}
5.使用对象类型作为方法的返回值
public class studentPa {
public static void main(String[] args) {
student two = method();//调用
System.out.println(two.age);
}
public static student method() {//定义一个返回值为student类型的方法
student one = new student();
one.age = 13;
one.name = "fuck";
return one;//返回该类型的一个实例;
}
}
6.局部变量和成员变量
- 定义的位置不同
*局部变量:在方法内部定义;
*成员变量:在方法外、类中定义;- 作用范围不同
*局部变量:方法内;
*成员变量:整个方法;- 默认值不同
*局部变量:必须赋值;
*成员变量:不赋值会有默认值;- 内存位置不同
*局部变量:栈中;
*成员变量:堆中;- 生命周期不同
*局部变量:随着方法进栈产生,随着方法出栈消失;
*成员变量:随着对象创建而产生,随着对象垃圾回收而消失;
7.面向对象三大特征之封装性
- 封装性在java中的体现:
- 方法就是一种封装;
- 关键字private也是一种封装
封装的思想:只管调用,不去关心具体如何使用的;
7.1关键字private
作用:对需要“保护”的成员变量进行修饰,一旦使用,则被修饰变量不能再被该类以外的操作直接访问;
间接访问方法:setter与getter
private int age;
public void setAge(int age) {//set变量首字母大写,且必须是void(无返回值类型)
this.age = age;
}
public int getAge() {//get变量首字母大写,此时变量是什么数据格式返回值也得是什么格式;
return age;
}
public class studentPa {
public static void main(String[] args) {
student two = method();
System.out.println(two.getAge());//用get获取
}
public static student method() {
student one = new student();
// one.age = 13;
one.setAge(20);
one.name = "fuck";
return one;
}
}
PS:对于boolean型数据,getXxx应该为isxxx;
7.2 关键字this
- 当方法的局部变量与类的成员变量重名时,根据就近原则,会优先使用局部变量;
- 如果需要访问类中的成员变量,则给重名变量前面加一个this.重名则表示成员变量;
- 谁在调用,则谁代表这个this;
8.构造方法
9.定义一个标准类
标准类的四个组成部分:
- 所有成员变量都要使用private关键字修饰;
- 为每一个成员变量编写一对setter\getter;
- 编写一个无参数的构造方法;
- 编写一个全参数的构造方法;
PS:code→generate 可以快捷生成2.3.4;快捷键:alt+insert
二、API概述与使用
API:带说明文档的库;
1. Scanner
使用步骤:
- 导包
import 包路径.类名称
- 需要使用的目标类和当前类在同一个包下时可以不需要导入;
- 只有java.lang下的类不需要调包;
- 创建
类名称 对象名 = new 类名称();
- 使用
对象名.成员方法名();
import java.util.Scanner;
public class getKeybord {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//System in 表示数据获取来源是键盘
System.out.println("请键入数字:");
int num = sc.nextInt();//nextInt表示要获取的是一个整型,next+Int
System.out.println("请键入字符串:");
String str = sc.next();//next表示不用转换为整型,因为我们要的正好就是字符串格式,而键盘输入的就是字符串
System.out.println("输入的数字是:" + num);
System.out.println("输入的字符串是:" + str);
}
}
PS:键盘输入的都是字符串格式
输入两个数并求和:
import java.util.Scanner;
public class AddPractice {
public static void main(String[] args) {
int sum = getSum();
}
public static int getSum() {
Scanner sz = new Scanner(System.in);
System.out.println("请键入两个要相加的数字并以回车分开:");
int a = sz.nextInt();
int b = sz.nextInt();
int sum = a + b;
System.out.println(a + "与" + b + "的和是:" +sum);
return sum;
}
}
输入三个数并求最大值:
import java.util.Scanner;
public class GetMAX {
public static void main(String[] args) {
Scanner sz=new Scanner(System.in);
int a= sz.nextInt();
int b= sz.nextInt();
int c= sz.nextInt();
int zdz=max(a,b,c);
System.out.println(zdz);
}
public static int max(int a, int b, int c) {
int temp = a>b?a:b;
int sum=temp>c?temp:c;
return sum;
}
}
2. 匿名对象
定义格式:
- new 对象名();
- new 对象名().成员变量=balabala;
- new 对象名().成员方法();
特点:只能使用一次,注意它前面的那个new,即每次都是不同的一个对象,仅在本条语句有效;
示例:int num = new Scanner(System.in).nextInt;
3.Random
作用:生成(伪)随机数;
import java.util.Random;
public class FirstRandom {
public static void main(String[] args) {
Random R = new Random();//生成实例
int a = R.nextInt();
System.out.println(a);
}
}
指定范围
import java.util.Random;
public class FirstRandom {
public static void main(String[] args) {
Random R = new Random();
for (int i = 0; i < 100; i++) {
int a = R.nextInt(100);//指定区间,左闭右开,即0~99;
System.out.println(a);
}
}
}
- 写一个猜数字小游戏:
import java.util.Random;
import java.util.Scanner;
public class GuessSz {
public static void main(String[] args) {
int answer = new Random().nextInt(5);//生成正确数字
System.out.println("请猜一个0~4的数字:");
int uGuess = new Scanner(System.in).nextInt();//猜一个数字;
int r = game(uGuess, answer);//第一次判断,若第一次就猜对了则不会进入循环;
while (r != 2) {//猜对了才能跳出循环;
if (r == 0) {
System.out.println("猜小了,再猜一次");
uGuess = new Scanner(System.in).nextInt();
r = game(uGuess, answer);
} else {
System.out.println("猜大了,再猜一次");
uGuess = new Scanner(System.in).nextInt();
r = game(uGuess, answer);
}
}
System.out.println("恭喜你猜对啦!答案就是:" + answer);
}
public static int game(int uGuess, int answer) {
int status;
if (uGuess < answer)
status = 0;
else if (uGuess > answer)
status = 1;
else status = 2;
return status;
}
}
- 看的课程的人写的猜数字小游戏代码:
4.对象数组
定义:用来存放对象的数组;
public class demoo1SArray {
public static void main(String[] args) {
Person[] array = new Person[3];
Person one = new Person("席得毛", 17);
Person two = new Person("席一帆", 0);
Person three = new Person("书记", 22);
array[0] = one;//放的都是 one two three的地址值;
array[1] = two;
array[2] = three;
System.out.println(array);//输出的都是地址;
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[2].getAge());//此时获得的是成员变量;
}
}
5.ArrayList
- 数组长度不可改变,但ArrayList的长度可以随意改变;
- 在使用时,应该ArrayList<引用类型> 名字 = new ArrayList<>();
- ArrayList集合内部元素应该类型一致,且不能是基本类似;
- 直接打印ArrayList得到的不是地址值,而是其中元素,如果没输入,则得到空的[];
- 表示泛型:即装在集合当中的所有元素必须是统一的类型,ArrayList的泛型必须是引用类型不能是基本类型;
5.1 ArrayList常用方法
- public boolean add(E e):向集合当中添加元素,参数的类型和泛型一致;
- public E get(int index):从集合当中获取元素,参数是索引编号,返回值就是对应位置的元素;
- public E remove(int index):从集合当中删除元素,有返回值;
- public int size():获取集合长度;
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
System.out.println(list);
list.add("傻逼");
System.out.println(list);
System.out.println("add方法是有一个返回值的");
boolean success=list.add("笨比");
System.out.println("返回值是:"+success);
}
PS:返回值表示add方法是否成功,ArrayList集合的add都会成功所以返回值可用可不用,但是之后会学习的其他集合的add方法却不一定,所以需要一个返回值来确认;
5.2 基本数据类型的包装类
- 因为集合当中存储的是元素的索引(地址值),而基本数据类型没有地址;
- 如果希望向集合ArrayList当中存储基本数据类型,则需要使用基本类型对应的包装类;
- int → Integer
- char → Character
- 其他的都是把首字母改大写就行
5.3 练习
5.3.1随机生成6个1~33之间的数并存入ArrayList数组后遍历
public static void main(String[] args) {
ArrayList<Integer> List1 = new ArrayList<>();
Random R = new Random();
for (int i = 0; i < 6; i++) {
List1.add(R.nextInt(33) + 1);
if (i == 5) {
System.out.println(List1);
}
}
for (int i = 0; i < 6; i++) {
System.out.println(List1.get(i));
}
}
5.3.2 定义四个Person对象,添加到集合,并遍历【有我重要知识误区】
public static void main(String[] args) {
ArrayList<Person> List1 = new ArrayList<>();
Person A = new Person("席得毛", 22);
Person B = new Person("张钰", 23);
Person C = new Person("张国荣", 00);
Person D = new Person("周星驰", 60);
List1.add(A);// add 的是地址值
List1.add(B);
List1.add(C);
List1.add(D);
System.out.println(List1);//打印出来也是地址值
for (int i = 0; i < 4; i++) {
System.out.println(List1.get(i));//打印出来还是地址值
Person P=List1.get(i);//需要通过地址值获取元素并将它赋值在实例上
System.out.println(P.getAge()+"岁的"+P.getName());//调用成员变量分别打印
}
}
5.3.3 在大集合存入30个随机数字,筛出其中的偶数并存入另一个集合
public class demo03ArrayList {
public static void main(String[] args) {
Random R = new Random();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 30; i++) {
list.add(R.nextInt(100));
}
System.out.println(list);
filEven(list);
}
public static void filEven(ArrayList<Integer> list) {
ArrayList<Integer> list1=new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (list.get(i)%2==0){
list1.add(list.get(i));
}else {
continue;
}
}
System.out.println(list1);
}
6.String 字符串
- String 类对象不需要new,只要是 “这个形式的” 都是String类的实例;
- String类对象一旦定义则永不可变;
- 字符串效果上相当于是cahr[ ]字符数组,但是底层原理是byte[ ]字节数组;
6.1 字符串的常用3+1种构造方法
- public String():创建一个空白字符串,不还任何内容;
- public String(char[] array):根据字符数组内容来创建对应的字符串;
- public String(byte[] array):根据字节数组内容来创建对应的字符串;
- 直接创建;
6.2 字符串常量池
- 字符串常量池:程序当中直接写上的双引号字符串,就在字符串常量池中,字符串常量池在堆中,如果你用直接定义的方式定义两个值相同的字符串,则它们得到常量池该字符串的同一个地址;
对应基本类型来说,==是进行数值的比较;
对于引用类型来说,==是进行地址的比较;
6.3 字符串的常用方法
6.3.1用来比较字符串的方法
- ==只能比较字符串的地址值,要比较字符串的内容,可以使用如下两个方法:
- public boolean equals(Object obj):参数可以是任何对象,只有参数是一个字符串并且内容相同的才会给ture,负责返回false;
PS: 任何对象都能用Object进行接受;str1.equals(str2) str2.equals(str1)//与上一句两者等价,对称; "字符串".equals(str2)//把字符串写在括号里也行,但推荐这么写;
- public boolean equalsIgnoreCase(String str):该种方法忽略大小写;
6.3.2 与获取相关的常用方法
- public int length():获取字符串中字符个数,得到字符串长度;
- public String concat(String str):将当前字符串和参数字符串连接成新字符串;
- public char charAt(int index):获取指定索引位置的单个字符;
- public int indexOf(String str):查找参数字符串在本字符串当中首次出现的索引位置(不存在则返回-1);
6.3.3 字符串的截取、转换、分割方法
截取:
- public String substring(int index):从参数位置开始一直到字符串末尾,返回新字符串;
- public String substring(int begin,int end):左闭右开截取;
转换:
- public char[] toCharArray():将当前字符串转换为字符数组;
- public byte[] getBytes():将当前字符串转换为字节数组;
- public String replace(CharSequence oldString,CharSequence newString):将当前字符串中的指定字符替换成想要的新字符;
PS:CharSequence是一个接口,表示可以接受字符串类型;
分割:
- public String[] split(String regex):按照 参数(正则表达式) 的 规则(在哪切) 将字符串切分成若干部分;
public static void main(String[] args) {
String str = "aa.bb.cc";
String[] strs=str.split("\\.");//因为split方法的参数是一个正则表达式,而“.”在正则表达式中有特殊含义,所以需要转义;
for (int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
}
}
public class demo02Practice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String str = sc.next();
System.out.println(str);
counterF(str);
}
public static void counterF(String str) {
char[] charArray = str.toCharArray();
int counterUpper = 0;
int counterLower = 0;
int counterNumber = 0;
int counterOther = 0;
for (int i = 0; i < charArray.length; i++) {
if ('A' <= charArray[i] && charArray[i] <= 'Z') {
counterUpper ++;
} else if ('a' <= charArray[i] && charArray[i] <= 'z') {
counterLower ++;
} else if ('0' <= charArray[i] && charArray[i] <= '9') {
counterNumber ++;
} else counterOther ++;
}
System.out.println("大写字母个数为:" + counterUpper);
System.out.println("小写字母个数为:" + counterLower);
System.out.println("数字个数为:" + counterNumber);
System.out.println("其它字符个数为:" + counterOther);
}
}
7.静态static关键字
7.1static修饰成员变量
- 如果一个成员变量使用static关键字,那么这个变量不再属于对象自己,而是属于所在的类。多个对象共享该数据;
7.2 static修饰成员方法
- public void 方法名():普通成员方法;
- public static void 方法名():静态成员方法;
静态方法不属于对象而是属于类,如果没有static的普通方法必须先创建对象,然后通过对象名来使用它,而静态方法可以直接使用类名来调用;
- 无论是成员变量还是成员方法,只要是静态的都推荐使用类名称来调用;
- 本类当中的静态方法可以省略类名称而直接调用;
PS:
- 静态方法不能直接访问非静态,因为在内存中先有的静态内容,后有的非静态内容;
- 静态方法中不能用this关键字;
7.3静态代码块
格式:
public class 类名称{ static{ 静态代码块内容 } }
- 特点:只有第一次调用本类时,静态代码块执行唯一的一次;
- 典型用途:用来一次性地堆静态成员变量进行赋值;
8.数组工具类Arrays与数学工具类Math
8.1 Arrays
- java.util.Arrays是一个与数组相关的工具类,提供了大量静态方法;
- public static String toString(数组):数组转为字符串;
- public static void sort(数组):排序;
如果是数值,则sort默认从小到大升序;
如果是字符串,sort默认按照字母升序;
如果是自定义类型,则这个自定义的类需要有Comparable或者Comparator接口的支持。
8.2 Math
- java.util.Math是一个与数学相关的类,提供大量静态方法;
- public static double abs(double num):取绝对值;
- public static double ceil(double num):向上取整;
- public static double floor(double num):向下取整;
- public static double round(double num):四舍五入