系列文章目录
提示:阅读本章之前,请先阅读目录
文章目录
前言
方法 function
加个 static 可以直接调用
package com.smobell.www.study_func;
public class Demo01 {
public static void main(String[] args) {
System.out.println("这是main方法,要保持干净整洁");
// 调用add方法,进行加法运算
int num = add(1,2);
System.out.println(num);
}
public static int add(int a, int b) {
return a+b;
}
}
不加static,则需要通过对象的方法
package com.smobell.www.study_func;
public class Demo01 {
public static void main(String[] args) {
System.out.println("这是main方法,要保持干净整洁");
// 调用add方法,进行加法运算
Demo01 demo01 = new Demo01();
int num = demo01.add(1,2);
System.out.println(num);
}
public int add(int a, int b) {
return a+b;
}
}
方法的定义和调用
package com.smobell.www.study_func;
public class Demo02 {
public static void main(String[] args) {
System.out.println("计算大小,大的输出");
System.out.println("结果:" + max(2, 2));
}
/**
* 比较大小,输出大的
* @param a
* @param b
* @return
*/
public static int max(int a, int b) {
int c = 0;
// 进行拦截,如果是相等,则return 0
if (a == b) return 0;
if ((a == b) || (a > b)) {
c = a;
} else{
c = b;
}
return c;
}
}
运行结果
方法的重载
package com.smobell.www.study_func;
public class Demo03 {
public static void main(String[] args) {
// 调用
System.out.println(sum(1, 2));
System.out.println(sum(1, 2, 3));
System.out.println(sum(1.00, 2, 3));
System.out.println(sum(1.00, 2.00, 3));
}
public static int sum(int a, int b) {
return a+b;
}
public static int sum(int a, int b, int c) {
return a+b+c;
}
public static int sum(double a, int b, int c) {
return (int)a*b+c;
}
public static double sum(double a, double b, double c) {
return a+b*c;
}
}
命令行传递参数
package com.smobell.www.study_func;
public class Demo04 {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
}
要先通过javac,生成class文件
然后退到主类com目录,再调用
可变参数
package com.smobell.www.study_func;
public class Demo05 {
public static void main(String[] args) {
double testA = max(1, 2, 3);
double testB = max(new double[]{1.11, 1.22, 1.33, 500, 12, 600, 900, 520});
double testC = max();
System.out.println("第一个值:" + testA);
System.out.println("第二个值:" + testB);
System.out.println("第三个值:" + testC);
}
// 可变参数,不指定具体几个
public static double max(double... num) {
if (num.length == 0) {
System.out.println("Sorry, This list is 0 length .");
return 0;
}
double result = 0.00;
for (int i = 0; i < num.length; i++) {
if (num[i] > result) result = num[i];
}
System.out.println("The max number is " + result);
return result;
}
}
递归
package com.smobell.www.study_func;
public class Demo06 {
public static void main(String[] args) {
// 调用递归 3 * 2 * 1
System.out.println(digui(3));
}
public static int digui(int x) {
if (x == 1) {
return 1;
} else {
return x*digui(x-1);
}
}
}
数组声明
package com.smobell.www.list;
public class Demo01 {
public static void main(String[] args) {
// 声明
int[] num1 = new int[1];
num1[0] = 100;
System.out.println(num1[0]);
}
}
静态数组和动态数组
package com.smobell.www.list;
public class Demo02 {
public static void main(String[] args) {
// 静态数组
int[] num1 = {1,2,3,4,5};
System.out.println(num1[2]);
// 动态数组
int[] num2 = new int[10];
num2[0] = 1;
System.out.println(num2[0]);
}
}
数组的反转
package com.smobell.www.list;
public class Demo03 {
public static void main(String[] args) {
// 定义数组1
int[] num1 = {1,2,3,4,5,6,7,8,9};
printInt(num1);
// 反转数组
int[] num2 = reverse(num1);
printInt(num2);
}
/**
* 反转数组
* @param x
* @return
*/
public static int[] reverse(int[] x) {
int[] rev = new int[x.length];
for (int i = 0, j = x.length - 1; i < x.length; i++, j--) {
rev[i] = x[j];
}
return rev;
}
/**
* 打印数字
* @param x
*/
public static void printInt(int[] x) {
for (int str : x) {
System.out.print(str + " ");
}
System.out.println();
}
}
多维数组
package com.smobell.www.list;
public class Demo04 {
public static void main(String[] args) {
// 一维数组
int[] num1 = {1,2,3,4};
int num1Len = num1.length;
// 二维数组
int[][] num2 = {{2,2,3,4},{9,8,7,6,5}};
int num2Len = num2[0].length;
// 三维数组
int[][][] num3 = {{{3,4,2,3,4,5},{5,6,7,8}},{{9,8,7,6,5}}};
int num3Len = num3[0][0].length;
System.out.println(num1[0]);
System.out.println(num2[0][0]);
System.out.println(num3[0][0][0]);
}
}
Array类
package com.smobell.www.list;
import java.util.Arrays;
public class Demo05 {
public static void main(String[] args) {
// 填充
int[] num1 = {1,2,3,4,5,6,7,8,9};
Arrays.fill(num1, 0,9,10);
Arrays.fill(num1, 10);
System.out.println(Arrays.toString(num1));
// 排序
int[] num2 = {88,55,99,105,220,0,1,2,3,999,555,4152,999};
Arrays.sort(num2);
System.out.println(Arrays.toString(num2));
}
}
冒泡排序
package com.smobell.www.list;
import java.util.Arrays;
public class Demo06 {
public static void main(String[] args) {
int[] list1 = {2,3,1,9,8,10,666,521,220,1123,5521,11,236,9999,555,620,0,12,3,21,8,4,45,54,5,51,1,55,4};
int[] newSortList = sortList(list1);
System.out.println(Arrays.toString(newSortList));
}
public static int[] sortList(int[] arr) {
// 循环次数,按照数组长度
for (int i = 0; i < arr.length; i++) {
boolean status = false;
// 每一次循环前后两个位置
for (int j = 0; j < arr.length-1; j++) {
int temp = arr[j];
if (arr[j] > arr[j+1]) {
arr[j] = arr[j+1];
arr[j+1] = temp;
status = true;
}
}
// 如果后续都没有可排序的,直接结束,不用100%循环次数
if (!status) break;
}
return arr;
}
}
稀疏数组
package com.smobell.www.list;
import javax.swing.*;
import java.util.Arrays;
public class Demo07 {
public static void main(String[] args) {
System.out.println("==================1. 模拟出稀疏数组======================");
// 1. 先创造一个稀疏数组
int[][] list1 = new int[11][11];
// 2. 设置棋点
list1[0][3] = 1;
list1[1][1] = 1;
list1[2][0] = 1;
list1[3][5] = 1;
list1[7][8] = 1;
list1[8][10] = 1;
list1[10][3] = 1;
// 3. 打印稀疏数组
for (int i = 0; i < list1.length; i++) {
for (int j = 0; j < list1[i].length; j++) {
if (list1[i][j]!=0) {
System.out.print(list1[i][j] + " ");
} else {
System.out.print("0 ");
}
}
System.out.println();
}
// 4. 获取旗子数
int numQz = 0;
for (int i = 0; i < list1.length; i++) {
for (int j = 0; j < list1[i].length; j++) {
if (list1[i][j]!=0) {
numQz++;
}
}
}
// 5. 通过稀疏数组,压缩
int[][] list2 = new int[numQz+1][3];
// 6. 导入数据
list2[0][0] = 11;
list2[0][1] = 11;
list2[0][2] = numQz;
int count = 0;
// 7. 记录压缩数组
for (int i = 0; i < list1.length; i++) {
for (int j = 0; j < list1[i].length; j++) {
if (list1[i][j] != 0) {
count++;
list2[count][0] = i;
list2[count][1] = j;
list2[count][2] = list1[i][j];
}
}
}
System.out.println("==================2. 打印出压缩数组======================");
// 8. 打印压缩数组
for (int i = 0; i < list2.length; i++) {
for (int j = 0; j < list2[i].length; j++) {
System.out.print(list2[i][j] + " ");
}
System.out.println();
}
// 9. 再次生成稀疏数组
int[][] list3 = new int[list2[0][0]][list2[0][1]];
for (int i = 1; i < list2.length; i++) {
list3[list2[i][0]][list2[i][1]] = list2[i][2];
}
System.out.println("==================3. 打印出还原的稀疏数组======================");
// 10. 再次打印稀疏数组
for (int i = 0; i < list3.length; i++) {
for (int j = 0; j < list3[i].length; j++) {
if (list3[i][j]!=0) {
System.out.print(list3[i][j] + " ");
} else {
System.out.print("0 ");
}
}
System.out.println();
}
}
}
运行结果
==================1. 模拟出稀疏数组======================
0 0 0 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0
==================2. 打印出压缩数组======================
11 11 7
0 3 1
1 1 1
2 0 1
3 5 1
7 8 1
8 10 1
10 3 1
==================3. 打印出还原的稀疏数组======================
0 0 0 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0
值传递
package com.smobell.www.obj;
public class Demo01 {
/**
* Java 的值传递,演示案例
* @param args
*/
public static void main(String[] args) {
int x = 0;
Demo01.change(x);
System.out.println("看看变化了有没有" + x);
}
public static void change(int x) {
x = 100;
}
}
引用传递
package com.smobell.www.obj;
public class Demo02 {
/**
* Java 的引用传递
* @param args
*/
public static void main(String[] args) {
Person person = new Person();
System.out.println("名字:" + person.name);
Demo02.change(person);
System.out.println("名字:" + person.name);
}
public static void change(Person person) {
person.name = "我已经不是小明拉";
}
}
class Person {
String name = "小明";
}
对象的调用
package com.smobell.www.obj;
public class Demo03 {
public static void main(String[] args) {
}
/**
* a是可以调用b的,因为是已经都创建完了
*/
public void a() {
System.out.println("a");
b();
}
/**
* b是可以调用a的,因为都已经创建完了
*/
public void b() {
System.out.println("a");
a();
}
}
package com.smobell.www.obj;
public class Demo04 {
public static void main(String[] args) {
}
/**
* a是可以调用b的,因为是已经都创建完了
*/
public void a() {
System.out.println("a");
b();
}
/**
* b 是无法调用a的,因为static关键字,就是和class类一起创建的,所以,还没有创建a出来,所以无法调用a
*/
public static void b() {
System.out.println("a");
a();
}
}
类和对象的使用
主类 main方法
package com.smobell.www.obj;
public class Demo06 {
public static void main(String[] args) {
Demo07 demo07 = new Demo07();
demo07.name = "Xiaoming";
demo07.age = 28;
demo07.money = 1998.08;
demo07.speak();
}
}
其他类
package com.smobell.www.obj;
public class Demo07 {
String name;
int age;
double money;
public void speak() {
System.out.println("My name: " + this.name + ", age: " + this.age + ", money: " + this.money + "¥");
}
}
构造器
运行时,其实这个new对象,就是调用Demo08的无参构造器,我们可以通过class文件来看到,编译后的,自动加上了无参构造器
class文件,自动创建无参构造器
public Demo08() {
}
验证,如果没有无参构造器,调用new的时候,就会报错
把无参构造器加上,则不会报错
最后,我们使用有参构造器,来创建对象,其实就是init函数,初始化作用,在java中,是叫构造器
代码
package com.smobell.www.obj;
public class Demo08 {
String name;
public Demo08() {}
public Demo08(String name) {
this.name = name;
}
}
package com.smobell.www.obj;
public class Demo09 {
public static void main(String[] args) {
Demo08 demo08 = new Demo08("xiaoming");
System.out.println(demo08.name);
}
}
创建对象内存分析图
方法区==》栈==》堆
静态方法区,和类一起加载了
封装
package com.smobell.www.obj;
public class Demo10 {
public static void main(String[] args) {
Demo11 demo11 = new Demo11();
demo11.setAge(999);
System.out.println(demo11.getAge());
}
}
package com.smobell.www.obj;
public class Demo11 {
private String name;
private int age;
public String getName(){
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge(){
return this.age;
}
public void setAge(int age) {
if (age > 100 || age < 0) {
this.age = 0;
} else {
this.age = age;
}
}
}
继承
Person类
package com.smobell.www.obj;
public class Persons {
public int money = 10_0000_0000;
private String family = "God Mon D.";
public void say() {
System.out.println("Hello,I am from " + this.family);
}
public String getFamily() {
return this.family;
}
public void setFamily(String family) {
this.family = family;
}
}
Teacher
package com.smobell.www.obj;
public class Teacher extends Persons{
}
Student
package com.smobell.www.obj;
public class Student extends Persons {
}
main方法调用
package com.smobell.www.obj;
public class Demo12 {
public static void main(String[] args) {
Student student = new Student();
student.say();
Teacher teacher = new Teacher();
teacher.say();
System.out.println(student.money);
}
}
运行结果
Super
父类
package com.smobell.www.obj;
public class Demo14 {
public String name = "xiaobailian";
public void say() {
System.out.println("Say Hello !");
}
}
子类
package com.smobell.www.obj;
public class Demo13 extends Demo14 {
public String name = "xiaoheilian";
public void print() {
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
this.say();
say();
super.say();
}
public void say() {
System.out.println("Say Yes !");
}
}
调用
package com.smobell.www.obj;
public class Demo15 {
public static void main(String[] args) {
Demo13 demo13 = new Demo13();
demo13.print();
}
}
运行结果
关于无参构造,子类,自动调用super的无参构造
package com.smobell.www.obj;
public class Demo14 {
public String name = "xiaobailian";
public Demo14() {
super(); // 无参构造,默认会执行这一条,super(),是父类的无参构造
}
public void say() {
System.out.println("Say Hello !");
}
}`在这里插入代码片`
重写
父类
package com.smobell.www.obj;
public class Demo17 {
public void sayTest() {
System.out.println("I am demo17");
}
}
子类
package com.smobell.www.obj;
public class Demo16 extends Demo17{
public void sayTest() {
System.out.println("I am Demo16 !");
}
}
调用
package com.smobell.www.obj;
public class Demo18 {
public static void main(String[] args) {
Demo16 demo16 = new Demo16();
demo16.sayTest();
Demo17 demo17 = new Demo16();
demo17.sayTest();
}
}
多态
父类
package com.smobell.www.obj;
public class Persons {
public int money = 10_0000_0000;
private String family = "God Mon D.";
public void say() {
System.out.println("Hello,I am from " + this.family);
}
public String getFamily() {
return this.family;
}
public void setFamily(String family) {
this.family = family;
}
}
子类
package com.smobell.www.obj;
public class Student extends Persons {
public void say() {
System.out.println("Hello,I am from self,because i am son");
}
public void play() {
System.out.println("I like play");
}
}
调用
package com.smobell.www.obj;
public class Demo19 {
public static void main(String[] args) {
Student student = new Student();
Persons persons = new Student();
student.say();
student.play();
persons.say();
}
}
结果
instanceof 判断类之间的关联关系
父类
package com.smobell.www.obj;
public class Persons {
public int money = 10_0000_0000;
private String family = "God Mon D.";
public void say() {
System.out.println("Hello,I am from " + this.family);
}
public String getFamily() {
return this.family;
}
public void setFamily(String family) {
this.family = family;
}
}
子类
package com.smobell.www.obj;
public class Student extends Persons {
public void say() {
System.out.println("Hello,I am from self,because i am son");
}
public void play() {
System.out.println("I like play");
}
}
调用
package com.smobell.www.obj;
public class Demo20 {
public static void main(String[] args) {
// object > persons
// object > persons > student
// object > persons > teacher
Persons persons = new Student();
System.out.println(persons instanceof Student);// true
System.out.println(persons instanceof Teacher);// false
System.out.println(persons instanceof Persons);// true
System.out.println(persons instanceof Object);// true
Student student = new Student();
System.out.println(student instanceof Student);// true
// System.out.println(student instanceof Teacher);// false
System.out.println(student instanceof Persons);// true
System.out.println(student instanceof Object);// true
Object object = new Student();
System.out.println(object instanceof Student);// true
System.out.println(object instanceof Teacher);// false
System.out.println(object instanceof Persons);// true
System.out.println(object instanceof Object);// true
Persons persons1 = new Persons();
System.out.println(persons1 instanceof Student);// false
System.out.println(persons1 instanceof Teacher);// false
System.out.println(persons1 instanceof Persons);// true
System.out.println(persons1 instanceof Object);// true
// 类的,从低 student 转换到 高
Student stu1 = (Student) persons;
// 也可以采用简单写法
((Student) persons).play();
// 类的,从高 persons 转换到 低,不支持,因为可能会丢失一些方法
// Student stu2 = (Student) persons1;
}
}
匿名函数,静态函数,静态导入包
package com.smobell.www.obj;
// 静态导入包~
import static java.lang.Math.random;
public class Demo21 {
{
System.out.println("我是匿名函数,每个实例化类,都会执行一次,可以用做初始化赋值");
}
static {
System.out.println("我是静态函数,只会执行一次");
}
public Demo21() {
System.out.println("我是构造函数,相当于初始化函数");
}
public static void main(String[] args) {
Demo21 demo21 = new Demo21();
Demo21 demo22 = new Demo21();
System.out.println(random());
}
}
运行
抽象类
抽象类
package com.smobell.www.obj;
public abstract class Demo22 {
public abstract void hello();
public void xixi() {
// 可以有普通方法
// 但是有抽象方法,必须是一个抽象类abstract
}
}
子类集成抽象类,必须重写抽象类的抽象方法
package com.smobell.www.obj;
public class Demo23 extends Demo22{
@Override
public void hello() {
// 必须重写抽象类的方法
}
}
子子类重写
package com.smobell.www.obj;
public abstract class Demo23 extends Demo22{
// 不想重写,也只能用抽象类集成,然后用子子类去重写
}
接口
接口类1
package com.smobell.www.obj;
public interface Demo24 {
public abstract void add();
void update();
void delete();
void index();
}
接口类2
package com.smobell.www.obj;
public interface Demo26 {
void test();
}
实现类
package com.smobell.www.obj;
public class Demo25 implements Demo24, Demo26{
@Override
public void add() {
}
@Override
public void update() {
}
@Override
public void delete() {
}
@Override
public void index() {
}
@Override
public void test() {
}
}
内部类
package com.smobell.www.obj;
public class Demo28 {
private String name = "hello hell";
public void run() {
System.out.println("demo28 run");
}
public class test {
public void out() {
System.out.println("test out");
}
// 可以获得私有变量
public void getName() {
System.out.println("我的名字" + name);
}
}
}
package com.smobell.www.obj;
public class Demo27 {
public static void main(String[] args) {
Demo28 demo28 = new Demo28();
demo28.run();
// 实例化内部类
Demo28.test test = demo28.new test();
test.getName();
}
}
运行
匿名类
package com.smobell.www.obj;
public class Demo27 {
public static void main(String[] args) {
Demo28 demo28 = new Demo28();
demo28.run();
// 实例化内部类
Demo28.test test = demo28.new test();
test.getName();
// 匿名类
new Demo28().run();
// 高级玩法
Demo26 demo26 = new Demo26() {
@Override
public void test() {
System.out.println("hello");
}
};
}
}
try…catch…finally
package com.smobell.www.exception;
public class Demo01 {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {
System.out.println(a/b);
} catch (Exception e) {
System.out.println("Exception错误");
} catch (Error e) {
System.out.println("Error错误");
} catch (Throwable e) {
System.out.println("Throwable错误");
} finally {
System.out.println("每次都会执行");
}
}
}
运行结果
抛出异常
package com.smobell.www.exception;
public class Demo02 {
public static void main(String[] args) throws Exception {
try {
new Demo02().sum(1,0);
} catch (Exception e){
System.out.println("错误" + e);
} finally {
System.out.println("执行一次");
}
}
public void sum(int a, int b) throws Exception {
if (b == 0) {
throw new Exception();
}
System.out.println(a/b);
}
}
运行结果
自定义异常
自定义异常类
package com.smobell.www.exception;
public class Demo03 extends Exception {
private int detail;
public Demo03(int a) {
this.detail = a;
}
@Override
public String toString() {
return "My Exception: " + detail;
}
}
使用
package com.smobell.www.exception;
public class Demo04 {
public static void main(String[] args) {
try {
new Demo04().number(100);
} catch (Demo03 e) {
System.out.println("报错了" + e);
}
}
public void number(int x) throws Demo03 {
if (x > 10) {
throw new Demo03(x);
}
System.out.println("恭喜您,没有问题");
}
}
运行结果
更新日志
提示:将会持续优化更新