前言:本篇专治Java_API中遇到的各种疑难杂症。
一、练习篇
1.1.练习
1.1.1 如何获取当前时间,并得到不同形式的输出。
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test16 {
public static void main(String[] args) {
SimpleDateFormat time=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String show=time.format(new Date());
SimpleDateFormat time1=new SimpleDateFormat("yyyy.MM.dd. HH:mm:ss");
String show1=time1.format(new Date());
System.out.println(show);
System.out.println(show1);
}
}
1.1.2、给定一个字符串,存储N个用户信息(账号和密码)。
字符串为:“zhangsan=aaaa;lisi=1234;wuwu=123456”
对字符串进行处理,将用户的信息存储到User中,多个用户如何存?将所有的信息存储完成后,在输出所有的用户信息。
public class Test11 {
public static void main(String[] args) {
String userInfo = "zhangsan=aaaa;lisi=1234;wuwu=123456";
Map<String, String> userMap = new HashMap<>();
String[] userArray = userInfo.split(";");
for (String user : userArray) {
String[] userInfoArray = user.split("=");
if (userInfoArray.length == 2) {
String username = userInfoArray[0];
String password = userInfoArray[1];
userMap.put(username, password);
}
}
for (Map.Entry<String, String> entry : userMap.entrySet()) {
String username = entry.getKey();
String password = entry.getValue();
System.out.println("Username: " + username + ", Password: " + password);
}
}
}
1.2 关键字this和super的使用说明
this类型:哪个对象调用就是哪个对象的应用类型。
this的三种用法:
(1)this.data; //访问属性
(2)this.funcation(); //访问方法
(3)this(); //调用本类中其他构造方法
super的作用意义:super主要是在继承关系下 存在于子类方法中,用于指向子类对象中父类对象。
super的三种用法:
(1)指向父类对象;
(2)调用父类的方法;
(3)super() 可以调用父类的构造方法。
下面是this和super结合使用的代码:
class Superserver{
public Superserver(){
System.out.println("all");
}
public Superserver(int y){
this();//调用本类中其他构造方法
System.out.println("good");
}
}
public class Test8 extends Superserver{
public Test8(){
super(11);//调用父类的构造方法
System.out.println("come");
}
public Test8(int y){
System.out.println("things");
}
public static void main(String[] args){
Test8 ts=new Test8();
}
}
输出结果:(四个输出,仔细看输出结果和顺序,明白this和super的作用)
all
good
come