class Student extends Object {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// 重写toString方法
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s = new Student();
s.setName("林青霞");
s.setAge(30);
System.out.println(s); // Student{name='林青霞', age=30}
}
}
① 作用
②: 应用场景
class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// 重写equals方法
@Override
public boolean equals(Object o) {
//this -- s1
//o -- s2
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o; //student -- s2
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("林青霞");
s1.setAge(30);
Student s2 = new Student();
s2.setName("林青霞");
s2.setAge(30);
//需求:比较两个对象的内容是否相同
System.out.println(s1.equals(s2));
}
}
public class MyArrays {
public static void main(String[] args) {
// 1.toString方法
int[] arr = {
1, 2, 3, 4, 5};
String s = Arrays.toString(arr);
System.out.println(s);// [1, 2, 3, 4, 5]
// 2. sort方法
int[] arr1 = {
5, 3, 2, 1, 6, 4};
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));// [1, 2, 3, 4, 5, 6]
}
}
package com.myAPI;
import java.util.Arrays;
public class MyArrays {
public static void main(String[] args) {
int[] arr = {
2, 6, 8, 7, 9, 4, 1};
// 相邻的两个元素两两比较, 将较大的元素放到后边;
for (int i = 0; i < arr.length - 1; i++) {
// 外层循环, 控制比较的轮数
for (int j = 0; j < arr.length - 1 - i; j++) {
// 内层循环, 控制每一轮比较,
if (arr[j] > arr[j + 1]) {
// 比较相邻的两个元素,将较大的元素放到后边;
var temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(arr));// [1, 2, 4, 6, 7, 8, 9]
}
}
①: 作用
public class IntegerDemo {
public static void main(String[] args) {
//public static Integer valueOf(String s):返回一个保存指定值的Integer对象 String
Integer i = Integer.valueOf("100");
System.out.println(i);
}
}
① int转换为String
public class IntegerDemo {
public static void main(String[] args) {
//int --- String
int number = 100;
//方式1
String s1 = number + "";
System.out.println(s1);
System.out.println("--------");
//方式2
//public static String valueOf(int i)
String s2 = String.valueOf(number);
System.out.println(s2);
}
}
② String转换为int
public class IntegerDemo {
public static void main(String[] args) {
//String --- int
String s = "100";
//方式1:String --- Integer --- int
Integer i = Integer.valueOf(s);
//public int intValue()
int x = i.intValue();
System.out.println(x);
//方式2
//public static int parseInt(String s)
int y = Integer.parseInt(s);
System.out.println(y);
}
}
① 自动装箱
② 自动拆箱
public class DateDemo02 {
public static void main(String[] args) {
//创建日期对象
Date d = new Date();
//public long getTime():获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
System.out.println(d.getTime());
System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");
//public void setTime(long time):设置时间,给的是毫秒值
// long time = 1000*60*60;
long time = System.currentTimeMillis();
d.setTime(time);
System.out.println(d);
}
}
① 格式化(从Date到String)
② 解析(从String到Date)
public class SimpleDateFormatDemo {
public static void main(String[] args) throws ParseException {
//格式化:从 Date 到 String
Date d = new Date();
// SimpleDateFormat sdf = new SimpleDateFormat();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s = sdf.format(d);
System.out.println(s);
System.out.println("--------");
//从 String 到 Date
String ss = "2048-08-09 11:11:11";
//ParseException
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sdf2.parse(ss);
System.out.println(dd);
}
}
public class CalendarDemo {
public static void main(String[] args) {
//获取日历类对象
Calendar c = Calendar.getInstance();
//public int get(int field):返回给定日历字段的值
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
//public abstract void add(int field, int amount):根据日历的规则,将指定的时间量添加或减去给定的日历字段
//需求:10年后的10天前
c.add(Calendar.YEAR,10);
c.add(Calendar.DATE,-10);
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH) + 1;
date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
//public final void set(int year,int month,int date):设置当前日历的年月日
c.set(2050,10,10);
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH) + 1;
date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
}
}