Java——API学习

本文介绍了如何使用Java的SimpleDateFormat类处理日期时间格式,以及如何从字符串中解析和存储用户信息到Map中。此外,还详细讲解了this和super关键字的用法,包括它们在访问属性、方法、构造方法中的作用,并通过示例代码展示了this和super的结合使用。
摘要由CSDN通过智能技术生成
前言:本篇专治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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值