java基础(复习整理)

8个基本数据类型(其他的均为引用类型

byte 占1个字节范围 -128~127

short 占2个字节:-32768~32767

int 占4个字节

long 占8个字节 例如:30L,后面加L

float 占4个字节,后面加F

double 占8个字节

char 占2个字节

boolean 占一个位,只有true跟false

位(bit)是计算机内部存储的最小单位

字节(byte)计算机中处理数据的基本单位,习惯用B表示

1B = 8bit

字符:指计算机使用的字母、数字、字和符号

1KB = 1024B

1MB = 1024KB

1024M=1G

编码 Unicode 2个字节 0-65536 Excel 2的16次方行

实例变量,从属于对象

类变量,static,随着类一起出来,一起销毁

除了基本类型,其余默认都是null

0,0.00 ,布尔类型,默认false

常量:大写+下划线

局部变量名:首字母小写+驼峰原则

类名:首字母大写+驼峰原则

方法名:首字母小写和驼峰原则

三目运算符

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        int score = 50 ;
        String s = score<60 ? "不及格":"及格";
        System.out.println(s);

    }
}
//不及格

for循环:

  • 最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个循环控制变量,也可是空语句

  • 然后,检测布尔表达式的值,如果为true,循环体被执行;如果为false,循环终止,开始执行循环体后面的语句

  • 再次检测布尔表达式。循环直行上面的过程

// 死循环
for(;;;){
    
    
    
}

构造器本质

构造器本质就是个方法

1.使用new关键字(实例化一个对象),本质是调用构造器

2.用来初始化值

类与对象

  • 类与对象

    ​ 类是一个模板,对象是一个具体的实例

  • 方法

    ​ 定义与调用

  • 对象的引用

    ​ 引用类型,基本类型(8)

    ​ 对象是通过引用来操作的:栈—>堆

  • 对象的属性

    ​ 字段,成员变量

    ​ 默认初始化:

    ​ 数字: 0 0.00

    ​ char :u0000

    ​ boolean : false

    ​ 引用: null

    ​ 修饰符,属性类型 属性名 = 属性值

  • 对象的创建和使用

    ​ 必须使用new 关键字创建对象,构造器

    ​ 对象的属性

私有的无法被继承

调用父类构造器,必须在子类构造器的第一行

  • super注意点:
    1. super调用父类构造方法,必须在构造方法的第一个
    2. super必须只能出现在子类的方法,或者构造方法中
    3. super和this不能同时调用构造方法

重写都是方法的重写,和属性无关

父类的引用指向子类,方法的调用只与定义的数据类型有关

必须非静态方法才能重写,必须是public方法才能重写

static 静态方法,属于类,不属于实例,不能重写

final方法不能重写,是常量

private方法不能重写

instance of

子类转换成父类可能丢失一些自己本来的方法

父类引用指向子类的对象

把子类转换成父类,向上转型

把父类转换成子类,向下转型,强制转型

方便方法的调用,减少重复的代码!

抽象:封装、继承、多态;抽象类、接口,

编程的思想、持续的学习,实践自己大脑中的想法

//抽象类
public abstract class Action {
    
    
//约束,有人帮我们实现
//    abstract,抽象方法,只有方法名字,没有方法实现
    public abstract void doSomething();

//    1.抽象类不能new,只能靠子类实现
//    2. 抽象类可以写普通方法
//    3.抽象方法必须在抽象类中
    
//    抽象类有构造器吗?
//    抽象类的意义何在?
}

接口作用:

  1. 约束
  2. 定义一些方法,让不同的人实现
  3. public abstract 抽象方法
  4. public static final 常量
  5. 接口不能被实例化,接口没有构造方法
  6. implements可以实现多个接口
  7. 必须要重写接口中的方法

Ctrl+Alt+T,选中,环绕添加,条件、捕获异常

  • 反射

    正常方式:引入“包类”名称 - > new实例化 -> 取得实例化对象

    反射方式:实例化对象 -> getClass()方法 -> 的到完整的“包类”名称

package com.example.demoDuxiaowei.model;

//测试Class类的创建方式有哪些
public class reflection {
    
    
    public static void main(String[] args) throws ClassNotFoundException {
    
    
        Person person = new Student();
        System.out.println("这个人是:"+person.name);

        //方式一:通过对象获得
        Class personClass = person.getClass();
        System.out.println(personClass.hashCode());

//        方式二:forname获得
        Class aClass = Class.forName("com.example.demoDuxiaowei.model.Student");
        System.out.println(aClass.hashCode());

//        方式三:通过类名.class获得
        Class studentClass = Student.class;
        System.out.println(studentClass.hashCode());

    }
}

class Person{
    
    
    public String name;

    public Person() {
    
    
    }

    public Person(String name) {
    
    
        this.name = name;
    }

    @Override
    public String toString() {
    
    
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

class Student extends Person{
    
    
    public Student(){
    
    
        this.name="学生";
    }

}

class Teacher extends Person{
    
    
    public Teacher(){
    
    
        this.name="老师";
    }
}
//这个人是:学生
//149928006
//149928006
//149928006

package com.example.demoDuxiaowei.model;


import lombok.Data;

import java.util.Objects;

//@Data
public class User {
    
    
    private Long id;
    private String name;
    private int age;

    public User() {
    
    
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public User(Long id, String name, int age) {
    
    
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public void setId(Long id) {
    
    
        this.id = id;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    public Long getId() {
    
    
        return id;
    }

    public String getName() {
    
    
        return name;
    }

    public int getAge() {
    
    
        return age;
    }
}

package com.example.demoDuxiaowei.model;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//abstract 抽象类,extends 单继承
public class A {
    
    
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
    
    
//        通过反射拿user类
        Class user = Class.forName("com.example.demoDuxiaowei.model.User");
//        调用无参构造器
        User wucanUser = (User)user.newInstance();
        System.out.println(wucanUser);

//       调用有参构造器
        Constructor user2 = user.getDeclaredConstructor(Long.class,String.class,int.class);
        Object youcanUser2 = user2.newInstance(1234L, "杜晓伟", 35);
        System.out.println(youcanUser2);

//      通过反射调用普通方法,通过反射获取一个方法
        Method setName = user.getDeclaredMethod("setName", String.class);
//        invoke:激活的意思
//        (对象,“方法的值”)
        setName.invoke(wucanUser,"狂神");
        System.out.println(wucanUser.getName());

//        通过反射操作属性
        User user4 = (User)user.newInstance();
        Field name = user.getDeclaredField("name");

//        不能直接操作私有属性,我们需要关闭程序的安全监测,
//        属性,方法setAccessible(true)
        name.setAccessible(true);
        name.set(user4,"狂神666");
        System.out.println(user4.getName());


    }

}

web

  • tomcat

    默认webapps

    <Host name="www.duxiaowei.com" appBase="webapps"
          unpackWARs="true" autoDeploy="true">
    

谈谈 网站是如何进行访问的!

  1. 输入域名,回撤
  2. 检查本机C:\Windows\System32\drivers\etc\HOSTS配置文件下有没有这个域名映射;
  3. 有:直接返回对应的ip地址,这个地址中有我们需要访问的web程序可以直接访问
  4. 没有:去DNS服务器找,找到就返回,招不到就返回找不到

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aZoTiGzq-1671025928313)(C:\Users\duxiaowei\AppData\Roaming\Typora\typora-user-images\image-20221125125442505.png)]

发布一个网站

–webapps:tomcat服务器的web目录

​ -ROOT

​ -kuangstudy:网站目录

​ -WEB-INF

​ -classes : java程序

​ -lib :web应用所依赖的jar包

​ -web.xml :网站的配置文件

​ -index.html 默认首页

​ -static

​ -css

​ -style.css

​ -js

​ -img

Meven

约定大于配置

maven,jar包地址 :https://mybatis.org/mybatis-3/zh/sqlmap-xml.html#cache

Cookie、Session

7.1会话

会话:用户打开一个浏览器,点击很多超链接,访问很多web资源,关闭浏览器,这个过程称为会话

  • 服务端给客户端一个信件,客户端下次访问服务端带上信件就可以了:cookie

  • 服务器登记你来过,下次你来的时候我来匹配你:session

    Cookie

7.2 Cookie

  1. 从请求中拿到cookie

  2. 服务器响应给客户端cookie

    public class CookieDemo01 extends HttpServlet {
          
          
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          
          
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
    
            PrintWriter out = resp.getWriter();
            //服务器从客户端获取 cookie
            Cookie[] cookies = req.getCookies();//返回数组说明有多个
            //判断Cookie是否存在
            if(cookies!=null){
          
          
                //如果存在怎么办
                out.write("您上一次访问的时间是:");
                for (Cookie cookie : cookies) {
          
          
                    if(cookie.getName().equals("lastLoginTime")){
          
          
                        long lastLoginTime = Long.parseLong(cookie.getValue());
                        Date date = new Date(lastLoginTime);
                        out.write(date.toString());
                    }
                }
    
            }else {
          
          
                out.write("这是您第一次访问本站:");
            }
            //服务器给客户端响应一个cookie
            Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
            resp.addCookie(cookie);
    
        }
    
    }
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                            http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0"
             metadata-complete="true">
    
        <servlet>
            <servlet-name>cookieTest</servlet-name>
            <servlet-class>com.du.servlet.CookieDemo01</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>cookieTest</servlet-name>
            <url-pattern>/c1</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    7.3 Session

    • 服务器会给每个用户(浏览器)创建一个Session对象

    • 一个Session独占一个浏览器,只要浏览器没有关闭,这个Session就存在

    • 用户登录后,整个网站他都可以访问!–>保存用户的信息,保存购物车的信息…

          <session-config>
              <!-- 15分钟后Session失效,一分钟为单位-->
              <session-timeout>15</session-timeout>
          </session-config>
      
    • 使用场景

      1. 保存一个登录用的信息
      2. 购物车信息
      3. 再整个网站中经常会使用的数据,我们将它保存在Session中
      //手动注销Session
      session.invalidate()
      

7.4 Session和Cookie区别

  • Cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个)

  • Session把用户的数据写到用户独占的Session中,服务器端保存(保存重要的信息,减少服务器资源的浪费)

  • Session对象由服务器创建

    //RestFul :风格

    @RequestMapping(“/add/{a}/{b}”,method=RequestMethod.GET)

    public String test1(@PathVariable int a ,@PathVariable int b,Model model){

    }

猜你喜欢

转载自blog.csdn.net/u013080870/article/details/128322691