public static void main(String[] args) {
Student s = new Student();
System.out.println(s.toString());//com.ithema.test.Student@10f3801
System.out.println(s);// 这两行输出的内容是一样的,说明toString()方法是默认的,因为所有
//的类都继承与Object类,toString()方法就是继承与Object类.
}
}
//学生类
class Student{
String name;
int age;
@Override //这个重写的toString方法是自动生成的 source--Generter toString..
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
---------------
>>>>> 字节码对象 <<<<
public static void main(String[] args) throws ClassNotFoundException {
}
}
//人类
class Person{
String name;
int age;
public Person(String name,int age){
this.name= name;
this.age = age;
}
@Override
public int hashCode() { //将hashCode()方法删除就行了,暂时用不到
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public static void main(String[] args) {
//Date 构造方法1
Date d = new Date(); //创建一个表示当前系统时间的对象
System.out.println(d);
//打印结果:Mon Mar 19 08:29:30 CST 2018
System.out.println(d.toLocaleString());
//打印结果2018-3-19 8:31:17,d.toLocaleString()可以返回
//看着比较方便的这种格式,但是已经过时,不建议使用
//Date(long date) 构造方法2
Date d2 = new Date(0);
System.out.println(d2.toLocaleString());
//打印结果为 1970-1-1 8:00:00 ,输入的是0秒,但打印的结果
//却不是1970-1-1 00:00:00,是因为它打印的是东八区的时间
典型的调用顺序是 :
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
只使用一次时可以这样用:
boolean b = Pattern.matches("a*b", "aaaaab");
--------------------
String 类中使用到正则表达式的一个应用:
String input = "1 fish 3 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.printl (s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();
System.out.println("\f------>>");
public static void main(String[] args) {
Circle c = new Circle(5);
double circumference = c.circumference();
double area = c.area();
//限制小数点位数
//第一种方法 会四舍五入
// double a =(Math.round(1.12345*10000)/10000.0);
// System.out.println(a);
//第二种 会四舍五入
// DecimalFormat df = new DecimalFormat("#.###");
// area = Double.parseDouble(df.format(area));
public static void main(String[] args) {
//创建集合对象
HashSet<Person> hs = new HashSet<Person>();
//创建元素对象
Person s1 = new Person("zhaoliu",455);
Person s2 = new Person("lisi",456);
Person s3 = new Person("lisi",456);
//添加
hs.add(s1);
hs.add(s2);
hs.add(s3);
//遍历
for(Person person : hs){
System.out.println(person);
}
}
}
class Person {
String name;
int age;
Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name="+name+",age="+age+"]";
}
//重写equals方法
@Override
public boolean equals(Object obj) {
System.out.println("-----------------");
//为了提高安全性需要对对传进来的对象进行判断,是否是
//同一种类型
public static void main(String[] args) {
Map<String,Stu> mm = new HashMap<String,Stu>();
Stu s1 = new Stu("小明",5);
Stu s2 = new Stu("大鹏",6);
Stu s3 = new Stu("叮当",3);
// Stu s3 = s2;
//方式2 获取所有的映射关系对象,通过映射关系对象获取key和value
Set<Map.Entry<Student2,String>> entrys = hm.entrySet();
for(Map.Entry<Student2,String> entry : entrys){
Student2 key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"--"+value);
}
// 打印结果:
// Student2 [name=zhangsan, age=18]--002
// Student2 [name=lisi, age=20]--003
// Student2 [name=zhangsan, age=18]--001
}
}
class Student2{
String name;
int age;
public Student2(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student2 [name=" + name + ", age=" + age + "]";
}
}
---------------------------------------------------
>>>>>>>>>>>>> 可变参数 <<<<<<<<<<<<<<
package com.jobday07;
/*
* 需求:定义一个方法,参数为两个int类型的变量,对参数求和并返回
* 定义一个方法,参数为三个int类型的变量,对参数求和并返回
* ...
*
* JDK1.5特性
* 可变参数:当参数不确定的时候,类型要明确,Java可以把多个参数直接
* 帮我们转换成数组
*
* 实参: 一个参数一个参数的传递
* 形参: 类型...变量名
*
* 注意:
* 1.在可变参数之后不可再追加参数
* 2.参数的数量自定义,可以给多个,甚至也可以一个都不给
* 3.使用可变参数的方法时也可以进行方法重载,但是使用时,
* 其它参数的数据类型要与可变参数的数据类型不同;
* 例:
* 方法调用:result = sum(1,2,3,4,5);
* 方法定义:public static int sum(int...arr){}
*
* 解析:
* 1.在可变参数之后不可再追加参数
* 例:
* public static int sum(int...arr,int a){}//错误
* 不可以在可变形参的后边添加形参
* public static int sum(int a,int...arr){}//正确
* 在可变形参的前边添加形参是可以的
* 2.参数的数量自定义,可以给多个,甚至也可以一个都不给
* 例:
* 方法调用:result = sum();
* 方法定义:public static int sum(int...arr){}
* 调用方法的时候不传参数也是可以的
* 3.使用可变参数的方法时也可以进行方法重载,但是使用时,
* 其它参数的数据类型要与可变参数的数据类型不同;
* public static void main(String[] args) {}
* public static int sum(String a,int...arr){}
*
*/
public class 可变参数 {
public static void main(String[] args) {
int result = sum(1,2,3,4,5);
}
//定义可变行参方法,进行求和 , 只有可变形参的求和方法 1
public static int sum(int...arr){
int result = 0;
for(int i=0;i<arr.length;i++){
result += arr;
}
return result;
}
//有一个可变行参,和多个其它形参的方法
public static int sum(String a,int...arr){
// int result = a;
int result = 0;
for(int i=0;i<arr.length;i++){
result += arr;
}
return result;
}
}
--------------------------------------------
>>>>>>>>>>> Map 嵌套 Map <<<<<<<<<<<<
package com.jobday07;
public static void main(String[] args) {
//创建学校Map
Map<String,List<Student1>> school = new HashMap<String,List<Student1>>();
//创建基础班,用List集合存储学员信息
List<Student1> base = new ArrayList<Student1>();
//添加基础班学员
base.add(new Student1("b001","xiaoming",18));
base.add(new Student1("b002","dapeng",16));
base.add(new Student1("b003","kulin",25));