table中,表头固定,body滚动的方式。也就是使用:css Table布局-display:table
两种类型的表格布局 你有两种方式使用表格布局 -HTML Table(<table>标签)和CSS Table(display:table 等相关属性)。 HTML Table是指使用原生的<table>标签,而CSS Table是指用CSS属性模仿HTML 表格的模型。 在W3C关于<table>相关标签的文档中我们可以找到,HTML 4中<table>相关标签的默认样式表: js 代码: table { display: table } tr { display: table-row } thead { display: table-header-group } tbody { display: table-row-group } tfoot { display: table-footer-group } col { display: table-column } colgroup { display: table-column-group } td, th { display: table-cell } caption { display: table-caption } 显而易见HTML Table使用标签<table>、<tr>、<td>等标签,就是使用CSS Table的相关属性来实现的。从上面HTML4的默认样式表中可以看出他们的使用对于CSS属性的情况: table:指定对象作为块元素级的表格。类同于html标签<table>(CSS2) inline-table:指定对象作为内联元素级的表格。类同于html标签<table>(CSS2) table-caption:指定对象作为表格标题。类同于html标签<caption>(CSS2) table-cell:指定对象作为表格单元格。类同于html标签<td>(CSS2) table-row:指定对象作为表格行。类同于html标签<tr>(CSS2) table-row-group:指定对象作为表格行组。类同于html标签<tbody>(CSS2) table-column:指定对象作为表格列。类同于html标签<col>(CSS2) table-column-group:指定对象作为表格列组显示。类同于html标签<colgroup>(CSS2) table-header-group:指定对象作为表格标题组。类同于html标签<thead>(CSS2) table-footer-group:指定对象作为表格脚注组。类同于html标签<tfoot>(CSS2) 下面是一些 display:table 示例,你可能会发现它很有用: · 动态垂直居中对齐
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 |
Html: <button>添加多行</button> <div class="box"> <p>Double Line</p> <p>Double Line</p> </div> LESS body { color: @beige; background: @green; display: table; width: 100%; height: 100%; } .box { display:table-cell; vertical-align: middle; text-align: center; } /*====== Ignore section below ======*/ @orange: #BD4932; @yellow: #FFD34E; @green: #105B63; @beige: #FFFAD5; /* Basic Style*/ * { margin:0; padding:0;} html, body { height: 100%; } button { padding: 5px 10px;position:absolute;bottom: 20px;left:20px;display: block; -webkit-appearance: none;background: @orange; outline: none; border: 2px solid #DB9E36; color: @yellow; border-radius: 10px; box-shadow: 0 2px 3px rgba(0,0,0,0.5);cursor: pointer;} button:active {border-color: @beige; color:@beige;} JS document.querySelector("button").addEventListener("click", function(){ var element = document.createElement("p"); element.innerText = "额外添加的行"; document.querySelector(".box").appendChild(element); |
这也许是使用display:table最常见的例子了。对于动态高度的元素,有了它,就可以实现真正的垂直(居中)对齐。 还有另一个不用display:table实现的垂直居中的简单方式,您可能感兴趣: table表格,让thead固定,tbody有滚动条,关键是都对齐的纯css写法 找了好久才找到一篇可以简单粗暴就能用的,所以拿过来算是收藏了。里面有一个css2里的命令是我没用过的也是这里面关键的:table-layout:fixed; 原理很简单,有爱研究的童鞋可以去css官网看看说明文档。 直接贴代码:
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>转载自·威易网CSS教程</title> <style> table tbody { display:block; height:195px; overflow-y:scroll; } table thead, tbody tr { display:table; width:100%; table-layout:fixed; } table thead { width: calc( 100% - 1em ) } table thead th{ background:#ccc;} </style> </head> <body> <table width="80%" border="1"> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>出生年月</th> <th>手机号码</th> <th>单位</th> </tr> </thead> <tbody> <tr> <td>张三</td> <td>18</td> <td>1990-9-9</td> <td>13682299090</td> <td>阿里巴巴</td> </tr> <tr> <td>张三封</td> <td>18</td> <td>1990-9-9</td> <td>13682299090</td> <td>阿里巴巴与四十大盗</td> </tr> <tr> <td>张小三</td> <td>18</td> <td>1990-9-9</td> <td>13682299090</td> <td>腾讯科技</td> </tr> <tr> <td>张三</td> <td>18</td> <td>1990-9-9</td> <td>13682299090</td> <td>浏阳河就业</td> </tr> <tr> <td>张三疯子</td> <td>18</td> <td>1990-9-9</td> <td>13682299090</td> <td>阿里巴巴</td> </tr> <tr> <td>张三</td> <td>18</td> <td>1990-9-9</td> <td>13682299090</td> <td>阿里巴巴</td> </tr> <tr> <td>张大三</td> <td>18</td> <td>1990-9-9</td> <td>13682299090</td> <td>阿里巴巴</td> </tr> <tr> <td>张三五</td> <td>18</td> <td>1990-9-9</td> <td>13682299090</td> <td>阿里巴巴</td> </tr> <tr> <td>张刘三</td> <td>18</td> <td>1990-9-9</td> <td>13682299090</td> <td>阿里巴巴</td> </tr> <tr> <td>张三</td> <td>18</td> <td>1990-9-9</td> <td>13682299090</td> <td>阿里巴巴</td> </tr> </tbody> </table> </body> </html> |
from:https://www.cnblogs.com/susan-home/p/8761574.html
View DetailsASP.NET core 3.1中Method ‘get_Info’ in type ‘MySql.Data.EntityFrameworkCore.Infraestructure.MySQLOptionsExtension’…解决方案
问题 笔者在项目中迁移数据库时出现Method ‘get_Info’ in type ‘MySql.Data.EntityFrameworkCore.Infraestructure.MySQLOptionsExtension’ from assembly ‘MySql.Data.EntityFrameworkCore, Version=8.0.19.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d’ does not have an implementation.异常,导致数据库迁移失败。 解决 笔者开发环境如下: ASP.NET Core 3.1.1 EF Core 3.1.1 MySql.Data.EntityFrameworkCore 8.0.19 经排查得,上述问题因Oracle提供的MySql.Data.EntityFrameworkCore 8.0.19暂未完全支持ASP.NET Core 3.1.1和EF Core 3.1.1导致。故采用Pomelo.EntityFrameworkCore.MySql(3.1.1)替换MySql.Data.EntityFrameworkCore(8.0.19),问题得以解决。 错误详细堆栈信息如下: stem.TypeLoadException: Method ‘get_Info’ in type ‘MySql.Data.EntityFrameworkCore.Infraestructure.MySQLOptionsExtension’ from assembly ‘MySql.Data.EntityFrameworkCore, Version=8.0.19.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d’ does not have an implementation. at Microsoft.EntityFrameworkCore.MySQLDbContextOptionsExtensions.UseMySQL at GX.ManageSystem.Portal.Startup.<ConfigureServices>b__4_0(DbContextOptionsBuilder options) in C:\Users\mediv\source\repos\GXManageSystem\Code\GX.ManageSystem.Portal\Startup.cs:line 31 at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass1_02.<AddDbContext>b__0(IServiceProvider p, DbContextOptionsBuilder b) at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.CreateDbContextOptions[TContext](IServiceProvider applicationServiceProvider, Action2 optionsAction) at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass10_01.<AddCoreServices>b__0(IServiceProvider p) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope […]
View DetailsASP.NET Core使用EF Core操作MySql数据库
ASP.NET Core操作MySql数据库, 这样整套环境都可以布署在Linux上 使用微软的 Microsoft.EntityFrameworkCore(2.1.4) 和MySql出的 MySql.Data.EntityFrameworkCore(8.0.13) 软件版本 Asp.net Core:2.1 MySql:5.6 项目结构 Snai.Mysql 是 Asp.net core 2.0 Api网站,Database 下的是MySql建库建表脚本 项目实现 一、MySql 建库建表 使用 Database下的 mysql 建库 表 主键 索引.sql 脚本建库建表,脚本如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
CREATE DATABASE alan CHARACTER SET utf8 COLLATE utf8_general_ci; USE alan; CREATE TABLE student( id INT AUTO_INCREMENT PRIMARY KEY, -- 自增列需为主键 `name` NVARCHAR(32) NOT NULL DEFAULT '', sex TINYINT NOT NULL DEFAULT 1, -- 0 男生,1 女生,2 保密 age INT NOT NULL DEFAULT 0 ); ALTER TABLE student ADD INDEX ix_student_name(`name`) -- UNIQUE INDEX 唯一索引 |
建库时加上 CHARACTER SET utf8 COLLATE utf8_general_ci 代码,设置数据库字符集为 utf8,配合程序的数据库连接串加上 CharSet=utf8,防止中文保存时乱码 如果建库时不是utf8,就把字符集改为utf8 二、EF Core 连接操作 MySql 数据库 1、新建项目,添加EF Core 和 MySql驱动依赖项 新建 asp.net core api 网站程序,NuGet 添加依赖项 Microsoft.EntityFrameworkCore.Tools(2.1.4) 和 MySql.Data.EntityFrameworkCore(8.0.13) 包 2、添加实体类Student和数据库上下文 新建 Entities 目录,在,根据表及字段,在目录下新建 Student 实体类,在类上加 [Table("student")] 表名、属性上加[Column("id")] 字段名等与表对应,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; namespace Snai.Mysql.Entities { [Table("student")] public class Student { [Column("id")] public int ID { get; set; } [Column("name")] public string Name { get; set; } [Column("sex")] public byte Sex { get; set; } [Column("age")] public int Age { get; set; } } } |
在根目录下加上 DataAccess 目录做为数据库操作目录,在该目录下加上 Base 目录做数据库上下文目录 在 Base 目录下新建 AlanContext 上下文类,继承 DbContext 类,通过构造函数注入数据库连接,添加 DbSet<Student> 实体属性,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using Microsoft.EntityFrameworkCore; using Snai.Mysql.Entities; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Snai.Mysql.DataAccess.Base { public class AlanContext:DbContext { public AlanContext(DbContextOptions<AlanContext> options) : base(options) { } public DbSet<Student> Student { get; set; } } } |
3、添、删、改、查 数据库记录 在 […]
View DetailsSpringboot调用soap webservice(Client)
1.使用jdk自带的webservice工具wsimport生成相关类 测试wsdl http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl
1 |
wsimport -s d:\wsdl -p com.example.demo.request -encoding utf-8 http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl |
-s 存储目录; -p 包名; -encoding 文件编码,默认会采用操作系统编码,中文为gbk,建议使用utf-8; 2.构建相关的配置类及测试方法
1 2 3 4 5 6 7 8 |
@Configuration public class IpConfig { @Bean public IpAddressSearchWebServiceSoap webService(){ return new IpAddressSearchWebService().getIpAddressSearchWebServiceSoap(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@SpringBootApplication @RestController @RequestMapping("/soap") public class DemoApplication { @Autowired private IpAddressSearchWebServiceSoap soap; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @RequestMapping("/{ip}") public ArrayOfString searchIp(@PathVariable("ip") String ip) { ArrayOfString response = soap.getCountryCityByIp(ip); return response; } } |
from:https://blog.csdn.net/VitaminZH/article/details/81123571
View Detailsvue 子组件主动获取父组件的数据和方法
子组件主动获取父组件的数据和方法: this.$parent.数据 this.$parent.方法 在子组件Header.vue里面
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 |
<template> <div> <h2>我是头部组件</h2> <button @click="getParentData()">获取子组件的数据和方法</button> </div> </template> ------------------------------------------------------------------------ <script> export default{ data(){ return{ msg:'子组件的msg' } }, methods:{ run(){ alert('我是子组件的run方法') }, getParentData(){ /* 子组件主动获取父组件的数据和方法: this.$parent.数据 this.$parent.方法 */ alert(this.$parent.msg);//数据 this.$parent.run(); //方法 } } } </script> |
from:https://www.cnblogs.com/sulanlan/p/9932986.html
View Detailsc# dynamic动态类型和匿名类
dynamic类型 简单示例 dynamic expando = new System.Dynamic.ExpandoObject(); //动态类型字段 可读可写 expando.Id = 1; expando.Name = "Test"; string json = Utils.ConvertJson.JsonEncode(expando); //输出{Id:1,Name:’Test'} //动态添加字段 List<string> fieldList = new List<string>() { "Name","Age","Sex"}; //From config or db dynamic dobj = new System.Dynamic.ExpandoObject(); var dic = (IDictionary<string, object>)dobj; foreach (var fieldItem in fieldList) { dic[fieldItem] = "set "+fieldItem+" value"; /*实现类似js里的 动态添加属性的功能 var obj={}; var field="Id"; eval("obj."+field+"=1"); […]
View DetailsUncaught (in promise) 的解决方法
在微信小程序开发过程中,使用了promise,但是在使用的过程中报Uncaught (in promise)错误,第一次遇到这种错误,所以在此记录下,方便以后解决问题
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 |
getImage: function(url) { return new Promise((resolve, reject) => { wx.getImageInfo({ src: url, success: function(res) { resolve(res) }, fail: function() { reject("") } }) }); }, //原来 //修改后 getImage: function(url) { return new Promise((resolve, reject) => { wx.getImageInfo({ src: url, success: function(res) { resolve(res) }, fail: function() { reject("") } }) }).catch((e) => {}); }, |
只要在后面加上.catch((e) => {}),就不会报错了 from:https://blog.csdn.net/dwb123456123456/article/details/89083464
View Details恕我直言,我怀疑你没怎么用过枚举
我们是否一样? 估计很多小伙伴(也包括我自己)都有这种情况,在自学Java语言看书时,关于枚举enum这一块的知识点可能都有点 “轻敌” ,觉得这块内容非常简单,一带而过,而且在实际写代码过程中也不注意运用。 是的,我也是这样!直到有一天我提的代码审核没过,被技术总监一顿批,我才重新拿起了《Java编程思想》,把枚举这块的知识点重新又审视了一遍。 为什么需要枚举 常量定义它不香吗?为啥非得用枚举? 举个栗子,就以B站上传视频为例,视频一般有三个状态:草稿、审核和发布,我们可以将其定义为静态常量:
1 2 3 4 5 6 7 8 |
public class VideoStatus { public static final int Draft = 1; //草稿 public static final int Review = 2; //审核 public static final int Published = 3; //发布 } |
对于这种单值类型的静态常量定义,本身也没错,主要是在使用的地方没有一个明确性的约束而已,比如:
1 2 3 4 5 |
void judgeVideoStatus( int status ) { ... } |
比如这里的 judgeVideoStatus 函数的本意是传入 VideoStatus 的三种静态常量之一,但由于没有类型上的约束,因此传入任意一个int值都是可以的,编译器也不会提出任何警告。 但是在枚举类型出现之后,上面这种情况就可以用枚举严谨地去约束,比如用枚举去定义视频状态就非常简洁了:
1 2 3 |
public enum VideoStatus { Draft, Review, Published } |
而且主要是在用枚举的地方会有更强的类型约束:
1 2 3 4 5 6 |
// 入参就有明确类型约束 void judgeVideoStatus( VideoStatus status ) { ... } |
这样在使用 judgeVideoStatus 函数时,入参类型就会受到明确的类型约束,一旦传入无效值,编译器就会帮我们检查,从而规避潜在问题。 除此之外,枚举在扩展性方面比普常量更方便、也更优雅。 重新系统认识一下枚举 还是拿前文《答应我,别再if/else走天下了可以吗》中的那个例子来说:比如,在后台管理系统中,肯定有用户角色一说,而且角色一般都是固定的,适合定义成一个枚举:
1 2 3 4 5 6 7 8 |
public enum UserRole { ROLE_ROOT_ADMIN, // 系统管理员 ROLE_ORDER_ADMIN, // 订单管理员 ROLE_NORMAL // 普通用户 } |
接下来我们就用这个UserRole为例来说明枚举的所有基本用法:
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 |
UserRole role1 = UserRole.ROLE_ROOT_ADMIN; UserRole role2 = UserRole.ROLE_ORDER_ADMIN; UserRole role3 = UserRole.ROLE_NORMAL; // values()方法:返回所有枚举常量的数组集合 for ( UserRole role : UserRole.values() ) { System.out.println(role); } // 打印: // ROLE_ROOT_ADMIN // ROLE_ORDER_ADMIN // ROLE_NORMAL // ordinal()方法:返回枚举常量的序数,注意从0开始 System.out.println( role1.ordinal() ); // 打印0 System.out.println( role2.ordinal() ); // 打印1 System.out.println( role3.ordinal() ); // 打印2 // compareTo()方法:枚举常量间的比较 System.out.println( role1.compareTo(role2) ); //打印-1 System.out.println( role2.compareTo(role3) ); //打印-2 System.out.println( role1.compareTo(role3) ); //打印-2 // name()方法:获得枚举常量的名称 System.out.println( role1.name() ); // 打印ROLE_ROOT_ADMIN System.out.println( role2.name() ); // 打印ROLE_ORDER_ADMIN System.out.println( role3.name() ); // 打印ROLE_NORMAL // valueOf()方法:返回指定名称的枚举常量 System.out.println( UserRole.valueOf( "ROLE_ROOT_ADMIN" ) ); System.out.println( UserRole.valueOf( "ROLE_ORDER_ADMIN" ) ); System.out.println( UserRole.valueOf( "ROLE_NORMAL" ) ); |
除此之外,枚举还可以用于switch语句中,而且意义更加明确:
1 2 3 4 5 6 7 8 9 10 11 12 |
UserRole userRole = UserRole.ROLE_ORDER_ADMIN; switch (userRole) { case ROLE_ROOT_ADMIN: // 比如此处的意义就非常清晰了,比1,2,3这种数字好! System.out.println("这是系统管理员角色"); break; case ROLE_ORDER_ADMIN: System.out.println("这是订单管理员角色"); break; case ROLE_NORMAL: System.out.println("这是普通用户角色"); break; } |
自定义扩充枚举 上面展示的枚举例子非常简单,仅仅是单值的情形,而实际项目中用枚举往往是多值用法。 比如,我想扩充一下上面的UserRole枚举,在里面加入 角色名 — 角色编码 的对应关系,这也是实际项目中常用的用法。 这时候我们可以在枚举里自定义各种属性、构造函数、甚至各种方法:
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 |
public enum UserRole { ROLE_ROOT_ADMIN( "系统管理员", 000000 ), ROLE_ORDER_ADMIN( "订单管理员", 100000 ), ROLE_NORMAL( "普通用户", 200000 ), ; // 以下为自定义属性 private final String roleName; //角色名称 private final Integer roleCode; //角色编码 // 以下为自定义构造函数 UserRole( String roleName, Integer roleCode ) { this.roleName = roleName; this.roleCode = roleCode; } // 以下为自定义方法 public String getRoleName() { return this.roleName; } public Integer getRoleCode() { return this.roleCode; } public static Integer getRoleCodeByRoleName( String roleName ) { for( UserRole enums : UserRole.values() ) { if( enums.getRoleName().equals( roleName ) ) { return enums.getRoleCode(); } } return null; } } |
从上述代码可知,在enum枚举类中完全可以像在普通Class里一样声明属性、构造函数以及成员方法。 枚举 + 接口 = ? 比如在我的前文《答应我,别再if/else走天下了可以吗》中讲烦人的if/else消除时,就讲过如何通过让枚举去实现接口来方便的完成。 这地方不妨再回顾一遍: 什么角色能干什么事,这很明显有一个对应关系,所以我们首先定义一个公用的接口RoleOperation,表示不同角色所能做的操作:
1 2 3 |
public interface RoleOperation { String op(); // 表示某个角色可以做哪些op操作 } |
接下来我们将不同角色的情况全部交由枚举类来做,定义一个枚举类RoleEnum,并让它去实现RoleOperation接口:
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 enum RoleEnum implements RoleOperation { // 系统管理员(有A操作权限) ROLE_ROOT_ADMIN { @Override public String op() { return "ROLE_ROOT_ADMIN:" + " has AAA permission"; } }, // 订单管理员(有B操作权限) ROLE_ORDER_ADMIN { @Override public String op() { return "ROLE_ORDER_ADMIN:" + " has BBB permission"; } }, // 普通用户(有C操作权限) ROLE_NORMAL { @Override public String op() { return "ROLE_NORMAL:" + " has CCC permission"; } }; } |
这样,在调用处就变得异常简单了,一行代码就行了,根本不需要什么if/else:
1 2 3 4 5 6 |
public class JudgeRole { public String judge( String roleName ) { // 一行代码搞定!之前的if/else灰飞烟灭 return RoleEnum.valueOf(roleName).op(); } } |
而且这样一来,以后假如我想扩充条件,只需要去枚举类中加代码即可,而不用改任何老代码,非常符合开闭原则! 枚举与设计模式 什么?枚举还能实现设计模式? 是的!不仅能而且还能实现好几种! 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 27 28 |
public class Singleton { // 构造函数私有化,避免外部创建实例 private Singleton() { } //定义一个内部枚举 public enum SingletonEnum{ SEED; // 唯一一个枚举对象,我们称它为“种子选手”! private Singleton singleton; SingletonEnum(){ singleton = new Singleton(); //真正的对象创建隐蔽在此! } public Singleton getInstnce(){ return singleton; } } // 故意外露的对象获取方法,也是外面获取实例的唯一入口 public static Singleton getInstance(){ return SingletonEnum.SEED.getInstnce(); // 通过枚举的种子选手来完成 } } |
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
public class Test { public enum Calculator { ADDITION { public Double execute( Double x, Double y ) { return x + y; // 加法 } }, SUBTRACTION { public Double execute( Double x, Double y ) { return x - y; // 减法 } }, MULTIPLICATION { public Double execute( Double x, Double y ) { return x * y; // 乘法 } }, DIVISION { public Double execute( Double x, Double y ) { return x/y; // 除法 } }; public abstract Double execute(Double x, Double y); } public static void main(String[] args) { System.out.println( Calculator.ADDITION.execute( 4.0, 2.0 ) ); // 打印 6.0 System.out.println( Calculator.SUBTRACTION.execute( 4.0, 2.0 ) ); // 打印 2.0 System.out.println( Calculator.MULTIPLICATION.execute( 4.0, 2.0 ) ); // 打印 8.0 System.out.println( Calculator.DIVISION.execute( 4.0, 2.0 ) ); // 打印 2.0 } } |
专门用于枚举的集合类 我们平常一般习惯于使用诸如:HashMap 和 HashSet等集合来盛放元素,而对于枚举,有它专门的集合类:EnumSet和EnumMap 1、EnumSet EnumSet 是专门为盛放枚举类型所设计的 Set 类型。 还是举例来说,就以文中开头定义的角色枚举为例:
1 2 3 4 5 6 7 8 |
public enum UserRole { ROLE_ROOT_ADMIN, // 系统管理员 ROLE_ORDER_ADMIN, // 订单管理员 ROLE_NORMAL // 普通用户 } |
比如系统里来了一批人,我们需要查看他是不是某个角色中的一个:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// 定义一个管理员角色的专属集合 EnumSet<UserRole> userRolesForAdmin = EnumSet.of( UserRole.ROLE_ROOT_ADMIN, UserRole.ROLE_ORDER_ADMIN ); // 判断某个进来的用户是不是管理员 Boolean isAdmin( User user ) { if( userRoles.contains( user.getUserRole() ) ) return true; return false; } |
2、EnumMap 同样,EnumMap 则是用来专门盛放枚举类型为key的 Map 类型。 比如,系统里来了一批人,我们需要统计不同的角色到底有多少人这种的话:
1 2 3 4 5 6 7 8 9 10 |
Map<UserRole,Integer> userStatisticMap = new EnumMap<>(UserRole.class); for ( User user : userList ) { Integer num = userStatisticMap.get( user.getUserRole() ); if( null != num ) { userStatisticMap.put( user.getUserRole(), num+1 ); } else { userStatisticMap.put( user.getUserRole(), 1 ); } } |
用EnumMap可以说非常方便了。 总 结 小小的枚举就玩出这么多的花样,不过好在探索和总结的过程还挺有意思的,也复习了很多知识,慢慢来吧。 from:https://my.oschina.net/hansonwang99/blog/3196498
View DetailsEvent filter with query "SELECT * FROM __InstanceModi
问题描述: Details -Event filter with query "SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA "Win32_Processor" AND TargetInstance.LoadPercentage > 99" could not be reactivated in namespace "//./root/CIMV2" because of error 0x80041003. Events cannot be delivered through this filter until the problem is corrected. 解决方案: http://go.microsoft.com/?linkid=9775756 http://download.microsoft.com/download/3/9/D/39DF4B56-9D3A-4645-AC11-803043ED4214/MicrosoftFixit50688.msi 方案详解: http://support.microsoft.com/kb/2545227/en-us#FixItForMeAlways from:https://www.cnblogs.com/heweijian/p/6781382.html
View Detailswin10怎么更新到1809正式版 升级windows10方法
win10 怎么更新? 升级win10 1809须知: 1、必须保证C盘有10G以上的可用空间,是剩余可用的硬盘容量,如果不足,需要自己清理c盘垃圾 2、必须保证一直通电,如果是笔记本电脑,建议不要用电池,主要怕你电量不够,直接连接电源升级 方法一:windows更新 1、首先保证你的win10是最新版的win10 1803,如果不是可能需要先更新达到1803,才能获取win10 1809推送; 2、如果已经是1803,则依次点击开始—设置—windows更新—检查更新,就会显示有更新可用,针对windows 10的功能更新,版本1809,之后就会自动下载、安装,后面就是漫长的更新配置过程。 方法二:windows10易升 1、直接下载windows10易升最新版,双击打开; 2、自动检测电脑环境以及当前系统版本,提示最新版是win10 1809,点击【立即更新】,后面就是漫长的下载、更新、配置过程。 方法三:win10 1809镜像升级 直接下载win10 1809 iso镜像,手动升级安装 镜像下载地址及安装方法详看:win10 1809正式版iso镜像下载 以上就是win10更新1809正式版的三种方法,如果你没有接收到win10 1809更新推送,会议参考本文提供的方法来升级win10 1809。 from:https://blog.51cto.com/rainy0426/2406685
View Details