record User(String name, int age){
public User {
callNumber++;
}
private static int callNumber;
static int getCallNumber(){
return callNumber;
}
}
调用:
1 2 3
var dur=new User("tea2",20);
var acs=new User("tea2",20);
System.out.println(User.getCallNumber());
Record的构造函数
构造函数可以写成两种方式.同时在构造函数中,进行各种验证操作.
在IDEA中可以通过快捷方式进行转换.如下图:
Java 14 Record简化构造函数转换为传统构造函数:
Java 14 Record传统构造函数转换为简化构造函数:
传统构造函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
record User(String name, int age){
public User(String name, int age) {
// 进行各种验证操作.
if(age<20){
throw new IllegalArgumentException("age小于20.");
}
callNumber++;
this.name = name;
this.age = age;
}
private static int callNumber;
static int getCallNumber(){
return callNumber;
}
}
简化构造函数
1 2 3 4 5 6 7 8 9 10 11 12 13
record User(String name, int age){
public User {
// 进行验证操作.
if(age<20){
throw new IllegalArgumentException("age小于20.");
}
callNumber++;
}
private static int callNumber;
static int getCallNumber(){
return callNumber;
}
}
static User read(String filePath) {
User user = null;
try (var oos = new ObjectInputStream(Files.newInputStream(Paths.get(filePath)))) {
// 此处使用了instanceof模式匹配.该特性为JDK 14新增加.
if (oos.readObject() instanceof User user1) {
user = user1;
}
static User read(String filePath) {
User user = null;
try (var oos = new ObjectInputStream(Files.newInputStream(Paths.get(filePath)))) {
// 此处使用了instanceof模式匹配.该特性为JDK 14新增加.
if (oos.readObject() instanceof User user1) {
user = user1;
}
public static void main(String[] args) {
var user = new User("测试用户", 20);
var filePath = "Java14Record";
write(user, filePath);
var readUser = read(filePath);
System.out.println(readUser.toString());
}
}
def updateStyle(style):
try:
cnx = mysql.connector.connect(user='root',
database='wordpress',host='127.0.0.1',password='123')
cursor = cnx.cursor()
result=cursor.execute("""update wp_options set option_value='{0}' where option_name='theme_mods_twentyfifteen' """.format(style))
print(result)
cnx.commit()
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cursor.close()
cnx.close()
print("处理完成!~")
if __name__ == '__main__':
if len(sys.argv)>1:
styleStr=sys.argv[1]
if styleStr=='blueStyle':
updateStyle(blueStyle)
elif styleStr=='darkStyle':
updateStyle(darkStyle)
elif styleStr=='yellowStyle':
updateStyle(yellowStyle)
elif styleStr=='pinkStyle':
updateStyle(pinkStyle)
elif styleStr=='purpleStyle':
updateStyle(purpleStyle)
Shell脚本(在Shell脚本中写下面这段代码,并保存文件):
python3 BckfCNUpdateTheme.py blueStyle
定时任务(第一条每天晚上20点执行,第二条每天早上8点执行.):
0 20 * * * sh /opt/style01.sh 0 8 * * * sh /opt/style02.sh