SpringBoot 学习
函数
$\textcolor{red}{}$ <font color="red" face="逐浪新宋" >Java</font>
Java 的所有变量和函数都要定义在类中。
函数或变量前加static 表示静态对象,类似于全局变量。
静态对象属于class ,而不属于class 的具体实例。
静态函数中只能调用静态函数和静态变量。
实例:
import java.util.Arrays;public class Main { public static void main (String[] args) { System.out.println(max(3 , 4 )); int [][] a = new int [3 ][4 ]; fill(a, 3 ); System.out.println(Arrays.deepToString(a)); int [][] b = getArray2d(2 , 3 , 5 ); System.out.println(Arrays.deepToString(b)); } private static int max (int a, int b) { if (a > b) return a; return b; } private static void fill (int [][] a, int val) { for (int i = 0 ; i < a.length; i ++ ) for (int j = 0 ; j < a[i].length; j ++ ) a[i][j] = val; } private static int [][] getArray2d(int row, int col, int val) { int [][] a = new int [row][col]; for (int i = 0 ; i < row; i ++ ) for (int j = 0 ; j < col; j ++ ) a[i][j] = val; return a; } }
package com.ys;import java.util.Scanner;import java.util.Arrays;public class Main { public static void main (String[] args) { Scanner sc = new Scanner (System.in); int a = sc.nextInt(), b = sc.nextInt(); System.out.println(max(a, b)); } private static int max (int a, int b) { return a > b ? a : b; } }
package com.ys;import java.lang.reflect.Array;import java.util.Scanner;import java.util.Arrays;public class Main { public static void main (String[] args) { Scanner sc = new Scanner (System.in); int [][] a = new int [3 ][4 ]; fill(a, 3 ); System.out.println(Arrays.deepToString(a)); } private static void fill (int [][] a, int val) { for (int i = 0 ; i < a.length; i ++){ for (int j = 0 ; j < a[i].length; j ++){ a[i][j] = val; } } } }
package com.ys;import java.lang.reflect.Array;import java.util.Scanner;import java.util.Arrays;public class Main { public static void main (String[] args) { int [][] a = getArray2d(3 , 4 , 10 ); System.out.println(Arrays.deepToString(a)); } private static int [][] getArray2d(int row, int col, int val){ int [][] res = new int [row][col]; for (int i = 0 ; i < row; i ++){ for (int j = 0 ; j < col; j ++){ res[i][j] = val; } } return res; } }
静态变量访问时用类名,静态函数访问时也是用类名
普通变量普通函数,用具体的实例来访问
package com.ys;import java.lang.reflect.Array;import java.util.Scanner;import java.util.Arrays;class Arguments { public static int x = 1 ; public final static int y = 0 ; } public class Main { public static void main (String[] args) { System.out.println(Arguments.x); System.out.println(Arguments.y); } }
所有未加static的函数绑定到的都是对象上
package com.ys;import java.util.Scanner;class Node { public void f () { System.out.println("function: F" ); } public static void g () { System.out.println("function: G" ); } } public class Main { public static void main (String[] args) { Node node = new Node (); node.f(); Node.g(); } }
类与接口
类
class 与C++ 、Python 类似。
源文件声明规则
一个源文件中只能有一个public 类。
一个源文件可以有多个非publicpublic 类。
每个源文件中,先写package 语句,再写import 语句,最后定义类
类的定义
public : 所有对象均可以访问
private : 只有自己可以访问
package com.ys;class Point { private int x; private int y; public Point (int x) { this .x = x; } public Point (int x, int y) { this .x = x; this .y = y; } public int getX () { return x; } public void setX (int x) { this .x = x; } public int getY () { return y; } public void setY (int y) { this .y = y; } public String toString () { return String.format("(%d, %d)" , x, y); } } public class Main { public static void main (String[] args) { Point p = new Point (3 ,5 ); System.out.printf("(%d, %d)\n" , p.getX(), p.getY()); p.setX(5 ); p.setY(10 ); System.out.println(p.toString()); } }
类的继承
每个类只能继承一个类
package com.ys;class Point { private int x; private int y; public Point (int x) { this .x = x; } public Point (int x, int y) { this .x = x; this .y = y; } public int getX () { return x; } public void setX (int x) { this .x = x; } public int getY () { return y; } public void setY (int y) { this .y = y; } public String toString () { return String.format("(%d, %d)" , x, y); } } class ColorPoint extends Point { private String color; public ColorPoint (int x, int y, String color) { super (x, y); this .color = color; } public String getColor () { return color; } public void setColor (String color) { this .color = color; } @Override public String toString () { return String.format("(%d, %d, %s)" , super .getX(), super .getY(),color); } } public class Main { public static void main (String[] args) { ColorPoint cp = new ColorPoint (3 ,5 , "red" ); System.out.println(cp.toString()); } }
类的多态
package com.ys;class Point { private int x; private int y; public Point (int x) { this .x = x; } public Point (int x, int y) { this .x = x; this .y = y; } public int getX () { return x; } public void setX (int x) { this .x = x; } public int getY () { return y; } public void setY (int y) { this .y = y; } public String toString () { return String.format("(%d, %d)" , x, y); } } class ColorPoint extends Point { private String color; public ColorPoint (int x, int y, String color) { super (x, y); this .color = color; } public String getColor () { return color; } public void setColor (String color) { this .color = color; } @Override public String toString () { return String.format("(%d, %d, %s)" , super .getX(), super .getY(),color); } } public class Main { public static void main (String[] args) { Point point = new Point (3 ,4 ); Point colorPoint = new ColorPoint (1 , 2 , "red" ); System.out.println(point.toString()); System.out.println(colorPoint.toString()); } }
接口
interface与class类似。主要用来定义类中所需包含的函数。
接口也可以继承其他接口,一个类可以实现多个接口。
接口的定义
interface Role { public void greet () ; public void move () ; public int getSpeed () ; }
接口的继承
interface Hero extends Role { public void attack () ; }
接口的实现
每个类可以实现多个接口
package com.ys;interface Role { public void greet () ; public void move () ; public int getSpeed () ; } interface Hero extends Role { public void attack () ; } class Zeus implements Hero { private final String name = "Zeus" ; public String getName () { return name; } @Override public void greet () { System.out.println(name + ": Hi!" ); } @Override public void move () { System.out.println(name +": Move" ); } @Override public int getSpeed () { return 10 ; } @Override public void attack () { System.out.println(name + ": Attack!" ); } } public class Main { public static void main (String[] args) { Zeus zeus = new Zeus (); zeus.greet(); zeus.attack(); zeus.move(); } }
接口的多态
多态,同一个类的实例,调用相同的函数,运行结果不同
class Athena implements Hero { private final String name = "Athena" ; public void attack () { System.out.println(name + ": Attack!" ); } public void greet () { System.out.println(name + ": Hi!" ); } public void move () { System.out.println(name + ": Move!" ); } public int getSpeed () { return 10 ; } } public class Main { public static void main (String[] args) { Hero[] heros = {new Zeus (), new Athena ()}; for (Hero hero: heros) { hero.greet(); } } }
泛型
类似于C++的template,Java的类和接口也可以定义泛型,即同一套函数可以作用于不同的对象类型。
泛型只能使用对象类型,不能使用基本变量类型。
定义一个链表
package com.ys;class Node { int val; Node next; public Node (int val) { this .val = val; } } public class Main { public static void main (String[] args) { Node head = new Node (1 ); head.next = new Node (2 ); head.next.next = new Node (3 ); for (Node p = head; p != null ; p = p.next){ System.out.println(p.val); } } }
public class Main { public static void main (String[] args) { Stack<Integer> stk = new Stack <>(); } }
git
存档
不同机器上的代码同步
git init
git status 是查看当前的状态,是否有修改
git add. 是添加所有修改的文件
git commit -m “first commit” 是提交修改
git push 是上传到远程仓库
git pull 是拉取远程仓库的修改
前后端分离
前后端分离是一种开发模式,前端负责页面的展示,后端负责数据的处理。
前后端不分离
前后端分离
MVC
MVC是一种设计模式,将程序分为三个部分:模型(Model)、视图(View)和控制器(Controller)。