Skip to content

Commit ecaf465

Browse files
committed
Java第十一天
1 parent 5318c3d commit ecaf465

21 files changed

+496
-0
lines changed

day11/Eclipse的基本使用.txt

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
1:����ʹ��
2+
A:ѡ��һ�������ռ�
3+
D:\develop\eclipse-SDK-3.7.2-win64\workspace
4+
B:���дһ��HelloWorld����(��������ĿΪ������λ)
5+
a:������Ŀ(����)
6+
*File -- New -- Java Project
7+
*����߿հ״���ֱ���Ҽ� -- New -- Java Project
8+
9+
������Ŀ���ƺ�ֱ��Finish��
10+
b:���е�java�ļ�����д��src�������Ч
11+
c:����һ����
12+
cn.itcast
13+
d:�ڰ��´���һ����
14+
HelloWorld
15+
16+
ͬʱ����������д����main������
17+
e:��main������д���ݼ���
18+
f:�������
19+
�Զ����룬�ڱ������һ�̰���������
20+
g:�����
21+
ѡ��Ҫ���е��ļ�������Ҫ���е��ļ�������
22+
�Ҽ� -- Run as - Java Application����
23+
h:������ʾ
24+
��Console����̨��ʾ����
25+
26+
2:Eclipse��������
27+
A:����ı�������еĻ�������(������Eclipse����û�����⣬�Ͳ�Ҫ������)
28+
B:ȥ��Ĭ��ע��(���Բ��ø�)
29+
C:�кŵ���ʾ������
30+
��ʾ���ڴ������������ߵĿհ������Ҽ� -- Show Line Numbers���ɡ�
31+
���أ�������Ķ�������һ�Ρ�
32+
D:�����С����ɫ
33+
a:Java��������������С����ɫ��
34+
window -- Preferences -- General -- Appearance -- Colors And Fonts -- Java�޸� -- Java Edit Text Font
35+
b:����̨
36+
window -- Preferences -- General -- Appearance -- Colors And Fonts -- Debug -- Console font
37+
c:�����ļ�
38+
window -- Preferences -- General -- Appearance -- Colors And Fonts -- Basic -- Text Font
39+
E:�����Ū���ˣ���ô��
40+
window -- Reset Perspective
41+
F:����̨�Ҳ�����
42+
Window--Show View��Console
43+
44+
3:��ݼ���ʹ��
45+
A:���ݸ�����
46+
Alt+/ ����ʾ����
47+
main+alt+/,syso+alt+/,����������ʾ
48+
B:��ݼ�
49+
��ʽ�� ctrl+shift+f
50+
����� ctrl+shift+o
51+
ע�� ctrl+/
52+
ctrl+shift+/,ctrl+shift+\
53+
���������ƶ� ѡ�д���alt+��/�¼�ͷ
54+
�鿴Դ�� ѡ������(F3����Ctrl+�����)
55+
56+
4:Eclipse�������߿���Ч��
57+
A:�Զ����ɹ��췽��
58+
a:�޲ι��췽�� �ڴ��������Ҽ�--source--Generate Constructors from Superclass
59+
b:���ι��췽�� �ڴ��������Ҽ�--source--Generate Constructors using fields.. -- finish
60+
B:�Զ�����get/set����
61+
�ڴ��������Ҽ�--source--Generate Getters and Setters...

day11/code/day11_Object/.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

day11/code/day11_Object/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>day11_Object</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Binary file not shown.
Binary file not shown.
270 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cn.itcast_01;
2+
3+
//public class Student extends Object:ÕâÖּ̳ÐÊÇĬÈÏ´æÔÚµÄ
4+
public class Student {
5+
private String name;
6+
private int age;
7+
8+
public Student() {
9+
super();
10+
}
11+
12+
public Student(String name, int age) {
13+
super();
14+
this.name = name;
15+
this.age = age;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
26+
public int getAge() {
27+
return age;
28+
}
29+
30+
public void setAge(int age) {
31+
this.age = age;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return "Student [name=" + name + ", age=" + age + "]";
37+
}
38+
39+
// @Override
40+
// public String toString() {
41+
// return name+"---"+age;
42+
// }
43+
44+
45+
46+
// @Override
47+
// public int hashCode() {
48+
// return 1000;
49+
// }
50+
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cn.itcast_01;
2+
3+
/*
4+
* Object:是类层次结构的根类,所有类都直接或者间接的继承自该类。
5+
*
6+
* Object类的方法:
7+
* public int hashCode():返回该对象的哈希码值。
8+
* 哈希值不是实际的地址值,一般我们把它理解为地址值。因为它是根据地址值换算出来的一个值。
9+
* 举例:401,401
10+
* public final Class<?> getClass():返回此 Object 的运行时类。
11+
* public String getName():返回该对象对应的类/抽象类/接口的名称。
12+
* public String toString():返回该对象的字符串表示。
13+
* getClass().getName() + '@' + Integer.toHexString(hashCode())
14+
*
15+
* Integer
16+
* public static String toHexString(int i):返回一个整数的十六进制的字符串表示形式
17+
*
18+
* 这个方法是没有任何意义的,建议重写此方法。
19+
* A:自己手动写,但是效果不好看,所以不自己写。
20+
* B:自动生成,效果还行,推荐方式。
21+
*
22+
* public boolean equals(Object obj)
23+
* protected void finalize()
24+
* protected Object clone()
25+
*/
26+
public class StudentDemo {
27+
public static void main(String[] args) {
28+
Student s1 = new Student();
29+
System.out.println(s1.hashCode()); // 2100664515
30+
Student s2 = new Student();
31+
System.out.println(s2.hashCode()); // 1237346925
32+
System.out.println("------------");
33+
34+
Student s3 = new Student();
35+
Class c = s3.getClass();
36+
String name = c.getName();
37+
System.out.println(name); // cn.itcast_01.Student
38+
System.out.println("------------");
39+
40+
Student s4 = new Student();
41+
// 如果你直接输出一个对象的名称,其实输出的是该对象调用的toString()方法后的字符串
42+
System.out.println(s4); // cn.itcast_01.Student@79f1d448
43+
// System.out.println(s4.toString()); // cn.itcast_01.Student@79f1d448
44+
45+
// cn.itcast_01.Student@79f1d448
46+
// String s = s4.getClass().getName() +"@"+
47+
// Integer.toHexString(s4.hashCode());
48+
// System.out.println(s);
49+
50+
Student s5 = new Student("林青霞",28);
51+
System.out.println(s5);
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package cn.itcast_02;
2+
3+
public class Student {
4+
private String name;
5+
private int age;
6+
7+
public Student() {
8+
super();
9+
}
10+
11+
public Student(String name, int age) {
12+
super();
13+
this.name = name;
14+
this.age = age;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public int getAge() {
26+
return age;
27+
}
28+
29+
public void setAge(int age) {
30+
this.age = age;
31+
}
32+
33+
34+
@Override
35+
public boolean equals(Object obj) {
36+
if (this == obj)
37+
return true;
38+
39+
if (obj == null)
40+
return false;
41+
42+
if (getClass() != obj.getClass())
43+
return false;
44+
45+
Student other = (Student) obj;
46+
if (age != other.age)
47+
return false;
48+
if (name == null) {
49+
if (other.name != null)
50+
return false;
51+
} else if (!name.equals(other.name))
52+
return false;
53+
return true;
54+
}
55+
56+
// @Override
57+
// public boolean equals(Object obj) { // Object obj = s2;
58+
// // return true;
59+
// // obj -- s2
60+
// // this -- s1
61+
// // Student s = (Student) obj;
62+
// // if (this.name.equals(s.name) && this.age == s.age) {
63+
// // return true;
64+
// // } else {
65+
// // return false;
66+
// // }
67+
//
68+
// // 因为自己和自己比,是没有必要进行转换的,所以,为了提高程序的效率,我们加入一个判断
69+
// if (this == obj) {
70+
// return true;
71+
// }
72+
//
73+
// // 为了让程序更健壮一些,加入一个判断
74+
// // 这个判断其实是在判断某个对象是否是某个类的对象
75+
// // 用到了一个判断符号:instanceof
76+
// // 格式:对象名 instanceof 类名
77+
// if (!(obj instanceof Student)) {
78+
// return false;
79+
// }
80+
//
81+
// // if语句的第二种格式可以使用三元运算符替换
82+
// Student s = (Student) obj;
83+
// // System.out.println("AAAAAA");
84+
// return this.name.equals(s.name) && this.age == s.age;
85+
// }
86+
87+
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cn.itcast_02;
2+
3+
/*
4+
* public boolean equals(Object obj):比较对象是否相等
5+
*
6+
* 我们发现这个结果和我们想的不太一样,那么,这是为什么呢?
7+
* 这个时候,我们要想知道为什么,就必须去看源码。
8+
* Object类中的equals()方法的实现。
9+
* 看到源码是这个样子的:
10+
* public boolean equals(Object obj) {
11+
* return (this == obj); //(s1 == s2);
12+
* }
13+
*
14+
*
15+
* 面试题:
16+
* ==和equals()的区别?
17+
* ==:
18+
* 基本类型:比较的是基本类型的值是否相同
19+
* 引用类型:比较的是引用类型的地址值是否相同
20+
* equals():
21+
* 引用类型。默认情况下,比较的是引用类型的地址是否相同。
22+
*
23+
* 一般来说,比较地址的意义不大,所以我们会重写该方法。
24+
* 如果重写呢?
25+
* 我们重写的基本思想就是让对象比较成员变量的值是否相同。
26+
* 实际开发中,我们都是自动生成。
27+
*
28+
* 字符串的比较:
29+
* 字符串本身也是一个类,是引用类型。
30+
* 他们的比较用的是equals()方法,字符串类重写了该方法,比较的是字符串的内容是否相同。
31+
*
32+
*/
33+
public class StudentDemo {
34+
public static void main(String[] args) {
35+
Student s1 = new Student("林青霞", 28);
36+
Student s2 = new Student("林青霞", 28);
37+
Student s3 = new Student("林青霞", 29);
38+
39+
// System.out.println(s1 == s1);
40+
// System.out.println(s1 == s2);
41+
System.out.println(s1.equals(s2));
42+
System.out.println(s1.equals(s3));
43+
System.out.println(s1.equals(s1));
44+
45+
Demo d = new Demo();
46+
System.out.println(s1.equals(d)); //ClassCastException
47+
}
48+
}
49+
50+
class Demo{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cn.itcast_03;
2+
3+
//在使用克隆的时候,报错CloneNotSupportedException
4+
//原因是因为我们要实现一个接口Cloneable
5+
public class Student implements Cloneable {
6+
private String name;
7+
private int age;
8+
9+
public Student() {
10+
super();
11+
}
12+
13+
public Student(String name, int age) {
14+
super();
15+
this.name = name;
16+
this.age = age;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public int getAge() {
28+
return age;
29+
}
30+
31+
public void setAge(int age) {
32+
this.age = age;
33+
}
34+
35+
@Override
36+
protected Object clone() throws CloneNotSupportedException {
37+
return super.clone();
38+
}
39+
}

0 commit comments

Comments
 (0)