03_java基础(八)之static关键字与代码块
20\21.static关键字
/**
* static关键字
* 1.用static修饰后的方法,称为静态方法.
* 2.静态的方法特点,可以使用 类名.方法名称 调用方法
* 3.静态方法只能调用静态方法/字段,非静态方法,既可以调用静态方法/字段,也可以调用非静态方法/字段
* 4.被static 修饰后的字段是共享的
*/
堆栈简单分析:
代码:
1 package com.day01.station.staticDemo; 2 3 /** 4 * Created by Administrator on 2018/3/23. 5 * 6 * 类: 方法 (普通方法 构造方法) 7 * 字段/属性 8 * 代码块 9 */ 10 public class StaticObj { 11 private String name; 12 private Integer age; 13 private static String conuntry; 14 15 16 /** 17 * static关键字 18 * 1.用static修饰后的方法,称为静态方法. 19 * 2.静态的方法特点,可以使用 类名.方法名称 调用方法 20 * 3.静态方法只能调用静态方法/字段,非静态方法,既可以调用静态方法/字段,也可以调用非静态方法/字段 21 * 4.被static 修饰后的字段是共享的 22 */ 23 public static void say(){//修饰的是放方法 24 //System.out.println("-----我是一个方法---------"+name); 25 say3(); 26 } 27 28 public void say2(){ 29 System.out.println("-----我是一个方法2---------"+name+age); 30 say(); 31 } 32 33 public static void say3(){ 34 System.out.println("-----我是一个方法3---------"); 35 } 36 37 public String getName() { 38 return name; 39 } 40 41 public void setName(String name) { 42 this.name = name; 43 } 44 45 public Integer getAge() { 46 return age; 47 } 48 49 public void setAge(Integer age) { 50 this.age = age; 51 } 52 53 public String getConuntry() { 54 return conuntry; 55 } 56 57 public void setConuntry(String conuntry) { 58 StaticObj.conuntry = conuntry; 59 } 60 }
测试
1 package com.day01.station.staticDemo; 2 3 import org.junit.Test; 4 5 /** 6 * Created by Administrator on 2018/3/23. 7 */ 8 public class TestStaticObj { 9 /** 10 * 测试目的: 验证加了static修饰的字段只用一份,是共享的 11 * 创建两个对象 第一个设置国家为中国 第一个设置国家为美国,如果后面的可以覆盖前面的,那么确实是共享的 12 */ 13 @Test 14 public void test3(){ 15 // 创建两个对象 16 StaticObj staticObj1 = new StaticObj(); 17 StaticObj staticObj2 = new StaticObj(); 18 19 staticObj1.setConuntry("中国"); 20 21 22 staticObj2.setConuntry("美国"); 23 String conuntry2 = staticObj2.getConuntry(); 24 System.out.println("conuntry2= "+conuntry2); 25 26 27 28 String conuntry1 = staticObj1.getConuntry(); 29 System.out.println("conuntry1= "+conuntry1); 30 31 // System.out.println(" staticObj1= "+staticObj1); 32 // System.out.println(" staticObj2= "+staticObj2); 33 } 34 @Test 35 public void test2(){ 36 StaticObj.say(); 37 } 38 @Test 39 public void test(){ 40 //以前 41 StaticObj staticObj = new StaticObj(); 42 staticObj.say(); 43 44 45 } 46 }
22.构造代码块与静态代码块
代码:
1 package com.day01.station.model; 2 3 /** 4 * Created by lidongping on 2018/5/2. 5 */ 6 public class LoginUser2 { 7 private Integer id; 8 private String userName; 9 10 /** 11 * 静态代码块 作用:初始化对象 12 * 1.只执行一次,不论创建多少个对象 13 */ 14 static { 15 System.out.println("------我是静态代码块-----"); 16 } 17 18 /** 19 * 构造代码块 20 * 1.每次创建对象都执行一次 21 * 2.可以有多个构造代码块,但是生产上一般只有一个 22 */ 23 { 24 System.out.println("-------我是构造代码块1------"); 25 } 26 27 /** 28 * 构造方法 29 */ 30 public LoginUser2() { 31 System.out.println("-------我是构造方法-----"); 32 } 33 34 public Integer getId() { 35 return id; 36 } 37 38 public void setId(Integer id) { 39 this.id = id; 40 } 41 42 public String getUserName() { 43 return userName; 44 } 45 46 public void setUserName(String userName) { 47 this.userName = userName; 48 } 49 }
测试代码
1 package com.day01.station.testDao; 2 3 import com.day01.station.model.LoginUser2; 4 import org.junit.Test; 5 6 /** 7 * Created by lidongping on 2018/5/2. 8 */ 9 public class TestLoginUser2 { 10 //测试构造代码块 11 @Test 12 public void test(){ 13 LoginUser2 loginUser2 = new LoginUser2(); 14 System.out.println("loginUser2="+loginUser2); 15 16 LoginUser2 loginUser3 = new LoginUser2(); 17 System.out.println("loginUser2="+loginUser3); 18 } 19 }
23.接口简单使用
接口
1 package com.day01.station.dao; 2 3 import com.day01.station.model.Product; 4 5 public interface IProductDao { 6 //增 7 public void save(String productName, int salePrice); 8 //删 9 public void delete(int id); 10 //改 11 public void update(int id,int salePrice,int costPrice); 12 //查 13 public Product query(int id); 14 }
实现
1 package com.day01.station.dao.impl; 2 3 import com.day01.station.dao.IProductDao; 4 import com.day01.station.model.Product; 5 6 import java.sql.Connection; 7 import java.sql.DriverManager; 8 import java.sql.PreparedStatement; 9 import java.sql.ResultSet; 10 11 12 public class ProductDao implements IProductDao { 13 @Override 14 public void save(String productName, int salePrice) { 15 System.out.println("--------我是增加方法---------"); 16 System.out.println(" productName = " + productName + " , salePrice =" + salePrice); 17 try { 18 //1. 加载 19 Class.forName("com.mysql.jdbc.Driver"); 20 // * 2. 连接 21 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_web", "root", "admin"); 22 // PreparedStatement prepareStatement(String sql) 23 // 创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。 24 String sql = "INSERT INTO product (product_name,sale_price) VALUES (?,?)"; 25 //3.创建 预编译语句 26 PreparedStatement preparedStatement = connection.prepareStatement(sql); 27 // void setString(int parameterIndex, String x)将指定参数设置为给定 Java String 值。 28 preparedStatement.setString(1, productName); 29 preparedStatement.setInt(2, salePrice); 30 //添加参数 31 //preparedStatement.set 32 //4.执行 33 preparedStatement.executeUpdate();//重要提醒 不需要传入sql语句 34 //5.释放资源 35 preparedStatement.close(); 36 connection.close(); 37 38 39 } catch (Exception e) { 40 e.printStackTrace(); 41 } 42 } 43 44 @Override 45 public void delete(int id) { 46 System.out.println(" --------我是删除方法--------- "); 47 System.out.println("---id=" + id); 48 49 try { 50 //1.加载 51 Class.forName("com.mysql.jdbc.Driver"); 52 //2.连接 53 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin"); 54 //3.创建 预 编译语句 55 String sql = "DELETE FROM product WHERE id=?"; 56 PreparedStatement preparedStatement = connection.prepareStatement(sql); 57 //设定参数 58 preparedStatement.setInt(1, id); 59 //4.执行语句 60 preparedStatement.executeUpdate(); 61 //5.释放资源 62 preparedStatement.close(); 63 connection.close(); 64 65 } catch (Exception e) { 66 e.printStackTrace(); 67 } 68 } 69 70 @Override 71 public void update(int id, int salePrice, int costPrice) { 72 System.out.println("--------我是修改方法---------"); 73 try { 74 //1.加载 75 Class.forName("com.mysql.jdbc.Driver"); 76 //2.连接 77 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin"); 78 //3.创建编译语句 79 String sql = "UPDATE product SET sale_price=?,cost_price=? WHERE id= ? "; 80 PreparedStatement preparedStatement = connection.prepareStatement(sql); 81 preparedStatement.setInt(1, salePrice); 82 preparedStatement.setInt(2, costPrice); 83 preparedStatement.setInt(3, id); 84 //4.执行语句 85 preparedStatement.executeUpdate(); 86 //5.释放资源 87 preparedStatement.close(); 88 connection.close(); 89 90 } catch (Exception e) { 91 e.printStackTrace(); 92 } 93 } 94 95 @Override 96 public Product query(int id) { 97 System.out.println("------我是查询方法----------"); 98 Product product = new Product();//袋子 99 try { 100 //1.加载 101 Class.forName("com.mysql.jdbc.Driver"); 102 //2.连接 103 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin"); 104 //3.创建编译语句 105 String sql = "SELECT id,product_name,sale_price FROM product WHERE id=?"; 106 PreparedStatement preparedStatement = connection.prepareStatement(sql); 107 preparedStatement.setInt(1, id); 108 //4.执行语句 109 ResultSet resultSet = preparedStatement.executeQuery(); 110 //解析结果 111 while (resultSet.next()) {//如果有在执行里面 112 int id1 = resultSet.getInt("id"); 113 String productName = resultSet.getString("product_name"); 114 int salePrice = resultSet.getInt("sale_price"); 115 //封装 袋子 116 //装 117 product.setId(id1); 118 product.setProductName(productName); 119 product.setSalePrice(salePrice); 120 } 121 //5.释放资源 122 resultSet.close(); 123 preparedStatement.close(); 124 connection.close(); 125 } catch (Exception e) { 126 e.printStackTrace(); 127 } 128 return product; 129 } 130 }
测试
1 package com.day01.station.testDao; 2 3 import com.day01.station.dao.impl.ProductDao; 4 import org.junit.Test; 5 6 /** 7 * Created by lidongping on 2018/5/2. 8 */ 9 public class TestDao { 10 @Test 11 public void test(){ 12 //1.创建dao对象 13 ProductDao productDao = new ProductDao(); 14 //2.调用dao的方法 15 productDao.save("苹果10",8000); 16 17 } 18 }
24.业务层实现
业务层接口代码
1 package com.day01.station.service; 2 3 /** 4 * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html 5 * 疑问咨询wx:851298348 6 */ 7 8 import com.day01.station.model.Product; 9 10 /** 11 * 业务层接口命名规则: I+模型名称+Service 例如:IProductService 12 */ 13 public interface IProductService { 14 //增 15 public void save(Product product); 16 //删 根据id删除 17 public void delete(Integer id); 18 //改 19 public void update(Product product); 20 //查 根据id查询产品 21 public Product query(Integer id); 22 }
业务层实现代码
1 package com.day01.station.service.impl; 2 3 import com.day01.station.dao.impl.ProductDao; 4 import com.day01.station.model.Product; 5 import com.day01.station.service.IProductService; 6 7 /** 8 * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html 9 * 疑问咨询wx:851298348 10 */ 11 public class ProductService implements IProductService{ 12 ProductDao productDao = new ProductDao(); 13 14 @Override 15 public void save(Product product) { 16 //1.创建dao对象 17 18 //调用持久层保存方法 19 String productName = product.getProductName(); 20 Integer salePrice = product.getSalePrice(); 21 productDao.save(productName,salePrice); 22 23 } 24 25 @Override 26 public void delete(Integer id) { 27 productDao.delete(id); 28 } 29 30 @Override 31 public void update(Product product) { 32 productDao.update(product.getId(),product.getSalePrice(),product.getCostPrice()); 33 } 34 35 @Override 36 public Product query(Integer id) { 37 Product prduct = productDao.query(id); 38 39 return prduct; 40 } 41 }