基础
数组使用
import java.util.Scanner;
public class array {
public static void main(String[] args) {
int [] ws= {6,6,6};
for(int i = 0 ; i < 3 ; i++) System.out.println(i + " ws big sb");
int a[]= new int[5];
Scanner ms = new Scanner(System.in);
for(int i = 0 ; i < a.length; i++)
{
System.out.println("请输入" + (i+1) + "的数字");
a[i] = ms.nextInt();
}
for(int i = 0 ; i < a.length ; i++) System.out.println(a[i]);
int s[][] = new int [2][2];
Scanner xl = new Scanner(System.in);
for(int i = 0 ; i < 2 ; i++)
{
for(int j = 0 ; j < 2 ; j++)
{
s[i][j] = xl.nextInt();
System.out.println(s[i][j]);
}
}
}
}
类和对象
import java.util.Scanner;
public class array {
public static void main(String[] args) {
cat a = new cat();
a.name = "ws";
a.age = "20";
System.out.println(a.name + " sb ws " + a.age);
}
}
class cat
{
String name;
String age;
}
成员方法
访问修饰符 public protected 默认 private
可变参数
public class array {
public static void main(String[] args) {
Person a = new Person();
a.speak();
a.sum(1,2,3);
}
}
class Person
{
public void speak()
{
System.out.println("xlx handsome");
}
public int sum(int... nums)
{
System.out.println("接受的参数个数=" + nums.length);
return 0;
}
}
构造器
import java.util.Scanner;
public class array {
public static void main(String[] args) {
Person a = new Person("smith",80);
}
}
class Person
{
String name;
int age;
public Person(String pName,int pAge)
{
System.out.println("构造器被调用");
name = pName;
age = pAge;
}
}
访问修饰符
方法重写/覆盖
多态
在编译阶段,能调用哪些成员,是由编译类型决定的(编译类型看等号的左边)
运行类型看等号右边
public class PloyArray {
public static void main(String[] args) {
//2个student对象 和 两个Teacher对象 统一放在数组中,并调用每个对象say方法
Person[] persons = new Person[5];
persons[0] = new Person("xlx",21);
persons[1] = new Student("xbj",16,200);
persons[2] = new Student("ws",20,100);
persons[3] = new Teacher("ljx",21,20000);
persons[4] = new Teacher("lyj",18,15000);
//循环遍历多态数组,调用say
for(int i = 0 ; i < persons.length;i++){
//person[i] 编译类型 是Person,运行类型是Student或Teacher
System.out.println(persons[i].say());
}
}
}
public class Student extends Person{
private double score;
public Student(String name, int age, double score) {
super(name, age);
this.score = score;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
//重写父类say方法
@Override
public String say() {
return super.say() +" score = " + score;
}
}
import java.util.Scanner;
public class Person{
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String say(){ // 返回名字和年龄
return name + "\t" + age ;
}
public static void main(String[] args) {
}
}
==与equals的区别
只要满足两个对象的地址是相同的,==就是相同的
toString方法
finalize方法
package finalize;
public class car {
public static void main(String[] args) {
car1 car1 = new car1("宝马");
car1 = null;
System.out.println("over");
System.gc();//主动调用垃圾回收器
//这时 car对象就是一个垃圾,垃圾回收器就会回收(销毁)对象,在销毁对象前,会调用该对象的finalize方法
//这时程序员就可以在finalize中,写自己的业务代码逻辑(eg:释放资源:数据库连接,打开文件...)
//不重写就会默认处理调用Object类的finalize
}
}
class car1{
private String name;
public car1(String name) {
this.name = name;
}
@Override
protected void finalize() throws Throwable {
System.out.println("我们销毁汽车" + name);
System.out.println("释放了某些资源" );
}
}
高级部分
类变量和类方法
main方法
代码块
//代码块调用是优先于构造器的
package kuai;
public class kuai {
public static void main(String[] args) {
Mo x1 = new Mo("你好xlx");
}
}
class Mo{
private String name;
private double price;
private String director;
{
System.out.println("11");
System.out.println("22");
System.out.println("33");
}
public Mo(String name) {
this.name = name;
System.out.println("12121");
}
public Mo(String name, double price) {
System.out.println("^^^^^^");
this.name = name;
this.price = price;
}
public Mo(String name, double price, String director) {
this.name = name;
this.price = price;
this.director = director;
System.out.println("******");
}
}
package kuai;
public class kuai {
public static void main(String[] args) {
AA aa = new AA();
aa.say();
//BB bb = new BB();
}
}
class AA {
//静态代码块
static void say(){
System.out.println("xlx is handsome");
}
{
System.out.println("xbj is beautiful");
}
static {
System.out.println("AA的静态代码块1被执行");
}
}
class BB extends AA{
static {
System.out.println("BB的静态代码块1被执行");
}
}
final基本使用
抽象类
abstract 没有方法体
接口
如果IG继承了IH接口,而Teacher类实现了IG接口,实际上就相当于 Teacher类也实现了TH接口。
四种内部类
SpringMVC
spring
maven工具错了,不知道怎么回事,挂一下
找了半天,是java版本不够,spring6要java17,不想下环境现在
先看着,往后走了
基于注解方式管理Bean
jdbl 又是druid 歇逼了
滚去学狂神说了
狂神说 spring
其实还好,尚硅谷让我学会了maven,理解了xml和注解,紧接着学spring5,挺好
helloSpring
!!! spring5用的是jdk8,正好不用下载新的jdk了
maven build 1.8 !!! 记录第一次能正常运行,我天!!!
对比,简单易懂
快速补全代码,以及强转的快捷键 ???
不知道快捷键是什么,留个坐标 alt + enter
bean的构造方法
spring配置
别名
Bean的配置
import
一般用于团队开发使用,他可以将多个配置文件,导入合并成一个
依赖注入
构造器注入(已说过)
(set方式注入)重点
依赖:bean对象的创建依赖于容器
注入:bean对象中的所有属性,由容器来注入
bean
注解实现自动装配
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
当写了两行bean,id有两个,可通过Qualifier来确定自动注解哪一个bean对象
这四个注解功能是一样的,都是代表将某个类注册到Spring中,装配Bean
方法名就是bean的id
也是一个component,也会被Spring容器托管,注册到容器中
Configuration代表这是一个配置类,和之前beans.xml作用相同
代理模式(SpringAOP的底层)
分类:静态代理,动态代理
动态代理
这一段代码 是死的,记住就行了
通过反射做到的
AOP
使用Spring的API接口
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
自定义来实现AOP
使用注解实现
Tomcat
改端口,在webapps的index.jsp 的connector
Mysql
springMVC能够后台链接前端
ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘设置的密码’;
data\的\要删除
狂神说 springmvc
狂神说 springboot
进入网页了!!!!!!!!!!!!!!!(重新看spring的注解)
JavaWeb
这是为什么需要tomcat
静态网页
动态
JSP/Servlet
package com.xlx.servlet;
import jdk.nashorn.internal.ir.CallNode;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloServlet extends HttpServlet {
//由于get或者post只是请求实现的不同的方式,可以相互调用。业务逻辑都一样
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//ServletOutputStream outputStream = resp.getOutputStream();
PrintWriter writer = resp.getWriter(); //响应流
writer.print("Hello,Servlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
package com.xlx.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(is);
String user = prop.getProperty("username") ;
String pwd = prop.getProperty("password") ;
resp.getWriter().print(user +":" + pwd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
package com.xlx.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(is);
String user = prop.getProperty("username") ;
String pwd = prop.getProperty("password") ;
resp.getWriter().print(user +":" + pwd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
package com.xlx.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class getHello extends HelloServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
response和request (不会,以后如果用到再说吧,跳了)
下载文件
response 实现重定向
package com.xlx.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RedirectServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("/xlx.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Http