目录
文章目录[隐藏]
自增
1 2 3 4 5 6 7 8 9 10 11 |
public class AutoIncrement { public static void main(String[] args) { int i = 1; i = i++; int j = i++; int k = i + ++i * i++; System.out.println("i = " + i); System.out.println("j = " + j); System.out.println("k = " + k); } } |
问:输出结果
单例模式
问: 什么是单例,有什么用。
问:单例的要点
问:单例有几种常见形式。
问:手写单例
[/collapse]
类初始化和实例初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class Father { private int i = test(); private static int j = method(); static{ System.out.print("(1)"); } Father(){ System.out.print("(2)"); } { System.out.print("(3)"); } public int test(){ System.out.print("(4)"); return 1; } public static int method(){ System.out.print("(5)"); return 1; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class Son extends Father{ private int i = test(); private static int j = method(); static{ System.out.print("(6)"); } Son(){ System.out.print("(7)"); } { System.out.print("(8)"); } public int test(){ System.out.print("(9)"); return 1; } public static int method(){ System.out.print("(10)"); return 1; } public static void main(String[] args) { Son s1 = new Son(); System.out.println(); Son s2 = new Son(); } } |
问:输出的结果
方法的参数传递机制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.Arrays; public class PassValue { public static void main(String[] args) { int i = 1; String str = "hello"; Integer num = 200; int[] arr = {1,2,3,4,5}; MyData my = new MyData(); change(i,str,num,arr,my); System.out.println("i = " + i); System.out.println("str = " + str); System.out.println("num = " + num); System.out.println("arr = " + Arrays.toString(arr)); System.out.println("my.a = " + my.a); } public static void change(int j, String s, Integer n, int[] a,MyData m){ j += 1; s += "world"; n += 1; a[0] += 1; m.a += 1; } } class MyData{ int a = 10; } |
问:输出是什么?
递归与迭代
编程题:有n步台阶,一次只能上1步或2步,共有多少种走法?
成员变量与局部变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class Variable { static int s; int i; int j; { int i = 1; i++; j++; s++; } public void test(int j){ j++; i++; s++; } public static void main(String[] args) { Variable obj1 = new Variable(); Variable obj2 = new Variable(); obj1.test(10); obj1.test(20); obj2.test(30); System.out.println(obj1.i + "," + obj1.j + "," + obj1.s); System.out.println(obj2.i + "," + obj2.j + "," + obj2.s); } } |
问:输出结果?
Spring Bean的作用域之间有什么区别
com.binz.spring.beans.Book
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
public class Book { private Integer id; private String title; private String author; private double price; private Integer sales; public Book() { System.out.println("Book对象被创建了"); } public Book(Integer id, String title, String author, double price, Integer sales) { super(); this.id = id; this.title = title; this.author = author; this.price = price; this.sales = sales; System.out.println("全参的构造器被调用"); } public Book(Integer id, String title, String author, double price) { super(); this.id = id; this.title = title; this.author = author; this.price = price; System.out.println("不含销量的构造器被调用"); } public Book(Integer id, String title, String author, Integer sales) { super(); this.id = id; this.title = title; this.author = author; this.sales = sales; System.out.println("不含价格的构造器被调用"); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Integer getSales() { return sales; } public void setSales(Integer sales) { this.sales = sales; } @Override public String toString() { return "Book [id=" + id + ", title=" + title + ", author=" + author + ", price=" + price + ", sales=" + sales + "]"; } } |
com.binz.spring.test.SpringTest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class SpringTest { //01.Spring Bean的作用域之间有什么区别 //创建IOC容器对象 ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml"); @Test void testBook() { Book book = (Book) ioc.getBean("book"); Book book2 = (Book) ioc.getBean("book"); System.out.println(book==book2); } } |
config.beans.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!-- ★bean的作用域 可以通过scope属性来指定bean的作用域 -singleton:默认值。当IOC容器一创建就会创建bean的实例,而且是单例的,每次得到的都是同一个 -prototype:原型的。当IOC容器一创建不再实例化该bean,每次调用getBean方法时再实例化该bean,而且每调用一次创建一个对象 -request:每次请求实例化一个bean -session:在一次会话中共享一个bean --> <bean id="book" class="com.binz.spring.beans.Book" scope="singleton"> <property name="id" value="8"></property> <property name="title" value="红高粱"></property> <property name="author" value="莫言"></property> <property name="price" value="10.00"></property> <property name="sales" value="800"></property> </bean> |
Spring支持的常用数据库事务传播属性和事务隔离级别
SpringMVC中如何解决POST请求中文乱码问题GET的又如何处理呢
SpringMVC的工作流程
MyBatis中当实体类中的属性名和表中的字段名不一样怎么办
Git 分支相关命令