Java官方笔记6继承

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 继承 Java只有单继承,最顶级的父类是Object。 子类会继承父类的fields和methods,而不会继承constructors,因为constructors不属于methods,但是子类可以通过super调用父类的constructor。 子类继承父类的范围是:public、protected、package-private 隐式转换,子类转父类(只有1个爸爸): Object obj = new MountainBike(); 显示转换,父类转子类(有多个子女,所以要明确指定): MountainBike myBike = (MountainBike)obj; 类有field,而接口没有,所以在多继承时就有问题:如果多个类有相同的field,那么子类将不知道用哪一个,而接口不存在这个问题。Java不支持继承多个类,但是可以实现多个接口。 重载 子类的方法跟父类有完全相同的签名和返回类型(也可以是子类),将会覆盖父类方法Override。 如果子类定义了1个static方法,跟父类完全相同,那么父类方法会被隐藏Hide。 Override和Hide是不同的: public class Animal { public static void testClassMethod() { System.out.println("The static method in Animal"); } public void testInstanceMethod() { System.out.println("The instance method in Animal"); } } public class Cat extends Animal { public static void testClassMethod() { System.out.println("The static method in Cat"); } public void testInstanceMethod() { System.out.println("The instance method in Cat"); } public static void main(String[] args) { Cat myCat = new Cat(); Animal myAnimal = myCat; // 隐式转换 Animal.testClassMethod(); // 调的父类 myAnimal.testInstanceMethod(); // 父类对象引用,还是调的子类 } } The static method in Animal The instance method in Cat Override只会调子类方法,而Hide取决于调用方是父还是子,比如这里的myCat,隐式转换为父类Animal后,会调父类的static方法,而调的实例方法却是子类的。(如果不是隐式转换,而是直接给父类实例化,那肯定还是调父类方法) ...

SpringBoot官方笔记6消息

The Spring Framework provides extensive support for integrating with messaging systems, from simplified use of the JMS API using JmsTemplate to a complete infrastructure to receive messages asynchro

JavaWeb开发基础Servlet事件和监听器

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 事件和监听器 事件是指在程序运行过程中发生的特定动作或情况。比如: 用户操作,如点击按钮、输入文本 系统操作,如文件读取完成、网络连接断开 应用程序内部的状态变化,如对象属性改变、方法调用 监听器是一个接口或类,用于定义在特定事件发生时要执行的操作。监听器会“监听”特定的事件,当事件发生时,监听器中的相应方法会被调用,从而执行预定义的操作。 事件和监听器是相辅相成的,它们的工作原理如下: 事件源:触发事件的来源。例如,按钮是事件源,点击按钮会触发点击事件 事件对象:当事件发生时,事件源会创建一个事件对象,包含有关事件的信息 注册监听器:监听器需要注册到事件源,以便监听特定的事件。注册后,事件源会在事件发生时通知监听器 事件处理:当事件发生时,事件源会调用监听器中的相应方法,传递事件对象,从而执行预定义的操作 Servlet Event Class 事件(Event)是一个类,封装了与特定事件相关的信息。 基本是构造方法和get方法: ServletRequestEvent public class ServletRequestEvent extends EventObject { private static final long serialVersionUID = 1L; private final ServletRequest request; public ServletRequestEvent(ServletContext sc, ServletRequest request) { super(sc); this.request = request; } public ServletRequest getServletRequest() { return this.request; } public ServletContext getServletContext() { return (ServletContext) super.getSource(); } } ServletRequestAttributeEvent public class ServletRequestAttributeEvent extends ServletRequestEvent { private static final long serialVersionUID = 1L; private final String name; private final Object value; public ServletRequestAttributeEvent(ServletContext sc, ServletRequest request, String name, Object value) { super(sc, request); this.name = name; this.value = value; } public String getName() { return this.name; } public Object getValue() { return this.value; } } ServletContextEvent public class ServletContextEvent extends EventObject { private static final long serialVersionUID = 1L; public ServletContextEvent(ServletContext source) { super(source); } public ServletContext getServletContext() { return (ServletContext) super.getSource(); } } ServletContextAttributeEvent public class ServletContextAttributeEvent extends ServletContextEvent { private static final long serialVersionUID = 1L; private final String name; private final Object value; public ServletContextAttributeEvent(ServletContext source, String name, Object value) { super(source); this.name = name; this.value = value; } public String getName() { return this.name; } public Object getValue() { return this.value; } } HttpSessionEvent public class HttpSessionEvent extends EventObject { private static final long serialVersionUID = 1L; public HttpSessionEvent(HttpSession source) { super(source); } public HttpSession getSession() { return (HttpSession) super.getSource(); } } HttpSessionBindingEvent public class HttpSessionBindingEvent extends HttpSessionEvent { private static final long serialVersionUID = 1L; private final String name; private final Object value; public HttpSessionBindingEvent(HttpSession session, String name, Object value) { super(session); this.name = name; this.value = value; } public String getName() { return this.name; } public Object getValue() { return this.value; } } ...

Java官方笔记5数字和字符串

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Numbers Number的子类: 另外还有BigDecimal和BigInteger,用于高精度计算,AtomicInteger和AtomicLong用于多线程应用。 我们有时候需要用包装类而非基本数据类型,理由如下: 方法入参类型为Object,只能传入对象 使用包装类提供的常量,比如MIN_VALUE和MAX_VALUE 使用包装类的方法来做类型转换 format import java.util.Calendar; import java.util.Locale; public class TestFormat { public static void main(String[] args) { long n = 461012; System.out.format("%d%n", n); // --> "461012" System.out.format("%08d%n", n); // --> "00461012" System.out.format("%+8d%n", n); // --> " +461012" System.out.format("%,8d%n", n); // --> " 461,012" System.out.format("%+,8d%n%n", n); // --> "+461,012" double pi = Math.PI; System.out.format("%f%n", pi); // --> "3.141593" System.out.format("%.3f%n", pi); // --> "3.142" System.out.format("%10.3f%n", pi); // --> " 3.142" System.out.format("%-10.3f%n", pi); // --> "3.142" System.out.format(Locale.FRANCE, "%-10.4f%n%n", pi); // --> "3,1416" Calendar c = Calendar.getInstance(); System.out.format("%tB %te, %tY%n", c, c, c); // --> "May 29, 2006" System.out.format("%tl:%tM %tp%n", c, c, c); // --> "2:34 am" System.out.format("%tD%n", c); // --> "05/29/06" } } DecimalFormat ...

SpringBoot官方笔记5Data

Spring Boot integrates with a number of data technologies, both SQL and NoSQL.

JavaWeb开发基础ServletSession

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Session是什么 在Web应用程序中,Session(会话)是用于在多个请求之间保持用户状态的一种机制。由于HTTP协议是无状态的,每个请求都是独立的,服务器无法直接知道两个请求是否来自同一个用户。Session通过在服务器端存储用户状态信息,并在客户端使用一个唯一的Session ID来标识用户,从而实现状态保持。 Session工作原理如下: 创建Session:当用户第一次访问Web应用程序时,服务器创建一个新的Session,并生成一个唯一的Session ID 发送Session ID:服务器将Session ID发送给客户端,通常通过HTTP响应头中的Set-Cookie字段 存储Session ID:客户端浏览器将Session ID存储在Cookie中,并在后续请求中将其发送回服务器 识别Session:服务器通过接收到的Session ID识别用户的会话,并从服务器端存储中获取相应的Session数据 四种主要技术 Cookies Hidden Form Field URL Rewriting HttpSession 1. Cookies(Cookie) Cookie是存储在客户端浏览器中的小数据片段。它们会随着每个HTTP请求一起发送到服务器,可以用来存储会话标识符。 工作原理 当用户第一次访问网站时,服务器生成一个唯一的会话ID,并将其作为Cookie发送到客户端浏览器 浏览器存储这个Cookie,并在后续请求中将其发送回服务器 服务器使用Cookie中的会话ID来检索用户的会话数据 优点 对用户透明 由浏览器自动管理 可以存储少量数据 缺点 用户可以在浏览器中禁用Cookie 存储容量有限(通常每个Cookie大约4KB) 如果处理不当,可能存在安全问题(例如Cookie盗窃) 示例 import javax.servlet.http.Cookie; // 在Servlet中设置Cookie Cookie sessionCookie = new Cookie("JSESSIONID", session.getId()); sessionCookie.setMaxAge(30 * 60); // 30分钟 response.addCookie(sessionCookie); // 在Servlet中检索Cookie Cookie[] cookies = request.getCookies(); // 从请求获取Cookie if (cookies != null) { for (Cookie cookie : cookies) { if ("JSESSIONID".equals(cookie.getName())) { String sessionId = cookie.getValue(); // 使用会话ID检索会话数据 } } } Cookie有两种类型: ...

Java官方笔记4类和对象

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 创建类 定义类Bicycle: public class Bicycle { // the Bicycle class has // three fields public int cadence; public int gear; public int speed; // the Bicycle class has // one constructor public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } // the Bicycle class has // four methods public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } } 创建子类MountainBike继承类Bicycle: ...

SpringBoot官方笔记4Web

Most web applications use the spring-boot-starter-web module to get up and running quickly. You can also choose to build reactive web applications by using the spring-boot-starter-webflux module.

JavaWeb开发基础ServletConfig与ServletContext

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 ServletConfig package javax.servlet; import java.util.Enumeration; public interface ServletConfig { String getServletName(); ServletContext getServletContext(); String getInitParameter(String var1); Enumeration<String> getInitParameterNames(); } ServletConfig用于Servlet初始化时从web.xml获取配置信息。主要有以下方法: public String getInitParameter(String name): Returns the parameter value for the specified parameter name. public Enumeration getInitParameterNames(): Returns an enumeration of all the initialization parameter names. public String getServletName(): Returns the name of the servlet. public ServletContext getServletContext(): Returns an object of ServletContext. 示例,web.xml ...

Java官方笔记3Java语言基础

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 变量 Instance Variables (Non-Static Fields) 实例变量(非静态变量) 一个类可以创造多个实例,实例中的变量叫做实例变量,相互独立。 Class Variables (Static Fields) 类变量(静态变量) 对比来看,类变量就是类的变量,一个类只能有一份,不能复制,使用static关键字来定义类变量。 代码: public class MyVar { static int classVar = 1; // 类变量(静态变量) int instanceVar = 2; // 实例变量 } 对比Python来看就很清楚: class MyVar: class_var = 1 def __int__(self): instance_var = 2 Local Variables 局部变量 局部变量是由代码位置决定的,前面的实例变量和类变量,是放到Field位置,而局部变量是放到Method里面。也就是说,局部变量是放在方法的花括号里面的变量,并且只能在方法内部访问,不能被其他地方访问,这就是所谓的局部。 public class MyVar { static int classVar = 1; // 类变量(静态变量) int instanceVar = 2; // 实例变量 void method() { int localVar = 3; // 局部变量 } } Parameters 参数 ...