【0基础学java】教学日志:javaSE-常用类

本章概述:本章主要讲了Date日期类,还有构造方法与重载,String类,Vehicle类

目录

本章概述:

一、Date日期类的使用

1、DateDemo

二、构造方法与重载

1、构造方法与重载在Java基础中的使用

2、Person类

3、TestCircle


本章概述:

一、Date日期类的使用

1、DateDemo

package commanclass.class4;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Auther: Yu Panpan
 * @Date: 2022/3/15 - 03 - 15 - 17:02
 * @Description: commanclass.class4
 * @version: 1.0
 */
public class DateDemo {

    public static void main(String[] args) throws ParseException {
        //日期类
        Date date = new Date();
        System.out.println(date);
        System.out.println(date.getTime());
        System.out.println(date.getYear());

        //日期格式化类
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        System.out.println(format);

        String newDate = "2008-08-08 08:08:08";
        Date parse = dateFormat.parse(newDate);
        System.out.println(parse);

        //日历类
//        Calendar calendar = Calendar.getInstance();
        Calendar calendar = new GregorianCalendar();
        System.out.println(calendar.getTime());
        calendar.setTime(parse);
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH) + 1);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
        System.out.println(calendar.get(Calendar.MINUTE));
        System.out.println(calendar.get(Calendar.SECOND));
    }
}

二、构造方法与重载

1、构造方法与重载在Java基础中的使用

package commanclass.class2;

/**
 * @Auther: Yu Panpan
 * @Date: 2022/3/4 - 03 - 04 - 9:31
 * @Description: commanclass.class2
 * @version: 1.0
 */

/*
4.	构造方法与重载:
定义一个网络用户类,要处理的信息有用户 ID、用户密码、 email 地址。
在建立类的实例时把以上三个信息都作为构造函数的参数输入,
其中用户 ID 和用户密码存在时,email缺省时 ,email地址是用户 ID 加上字符串"@gameschool.com"。
 */
public class NetUser {

    private String ID;

    private String password;

    private String email;

    public NetUser(String ID,String password,String email){
            this.ID = ID;
            this.password = password;
            this.email = email;
    }

    public NetUser(String ID, String password) {
        this.ID = ID;
        this.password = password;
    }

    public void checkEmail(){
        if(this.email != null){
            this.email = email;
        }else{
            this.email = this.ID + "@gameschool.com";
        }
    }

    @Override
    public String toString() {
        return "NetUser{" +
                "ID='" + ID + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                '}';
    }

    public static void main(String[] args) {
        NetUser netUser = new NetUser("10001","6666666","ilovejava@sina.com");
        netUser.checkEmail();
        System.out.println(netUser);
        System.out.println("****************************************************");
        NetUser netUser2 = new NetUser("10002","8888888");
        netUser2.checkEmail();
        System.out.println(netUser2);

    }

}

2、Person类

package commanclass.class2;

/**
 * @Auther: Yu Panpan
 * @Date: 2022/3/4 - 03 - 04 - 9:20
 * @Description: commanclass.class2
 * @version: 1.0
 */

/*
2.	编写 Java 程序用于显示人的姓名和年龄。
定义一个人类Person。
该类中应该有两个私有属性:
姓名 (name) 和年龄 (age) 。
定义构造方法用来初始化数据成员。再定义显示(display()) 方法将姓名和年龄打印出来。
在main 方法中创建人类的实例然后将信息显示。
 */
public class Person {

    private String name;

    private int age;

    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }

    public void display(){
        System.out.println("姓名:" + this.name + ",年龄:" + this.age);
    }

    public static void main(String[] args) {
        Person person = new Person("小白",22);
        person.display();
    }



}

3、TestCircle

package commanclass.class2;

/**
 * @Auther: Yu Panpan
 * @Date: 2022/3/4 - 03 - 04 - 9:28
 * @Description: commanclass.class2
 * @version: 1.0
 */
public class TestCircle {

    public static void main(String[] args) {
        Circle circle = new Circle();
        circle.setR(20);
        System.out.println("圆的面积为:" + (int)circle.getArea() + ",周长为:" + (int)circle.getPerimeter());
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jason的java世界

不要吝啬你的赞赏哦~~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值