Java基础入门:Java常用核心类
Object类 根类 终极父类
package com.cnblogs.wanson;
public class Driver {
String name;
Integer age;
public Driver(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
//重写toString方法
/* @Override
public String toString() {
return "Driver{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}*/
}
import com.cnblogs.wanson.Driver;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Driver_Test {
public static void main(String[] args) {
Driver driver = new Driver("wanson", 18);
// 方法1
System.out.println(driver.toString());
// 方法2
System.out.println(driver);
/*案例1*/
System.out.println("随机数案例");
Random random = new Random();
System.out.println(random);
Scanner scanner = new Scanner(System.in);
System.out.println(scanner);
/*案例2*/
System.out.println("集合ArrayLista案例");
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(11);
arrayList.add(12);
arrayList.add(15);
System.out.println(arrayList);
/*equals()方法使用*/
Driver driver1 = new Driver("a,", 12);
Driver driver2 = new Driver("b", 18);
// 打印地址
System.out.println(driver1);
System.out.println(driver2);
// 打印hash值
System.out.println(driver2.hashCode());
System.out.println(driver2.hashCode());
System.out.println(driver1.equals(driver2));
driver1 = driver2;//driver2的地址复制给driver1
System.out.println(driver1.equals(driver2));
}
}
posted on 2019-03-11 12:37 Indian_Mysore 阅读(110) 评论(0) 编辑 收藏 举报