Skip to content

Commit 8cf4c70

Browse files
add demo related to method
1 parent 88e7123 commit 8cf4c70

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ABasic.A7Method;
2+
3+
public class MethodReference {
4+
public static void main(String[] args) {
5+
Person person=new Person("name", 18, "black");
6+
Person person1=new Person("name1", 19);
7+
System.out.println(person1.toString());
8+
copyAttribute(person, person1);
9+
System.out.println(person1.toString());
10+
}
11+
12+
public static void copyAttribute(Person person, Person person1){
13+
person1.setEyeColor(person.getEyeColor());
14+
}
15+
16+
}

src/ABasic/A7Method/Person.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package ABasic.A7Method;
2+
3+
import java.util.Date;
4+
5+
public class Person {
6+
private String name;
7+
private int age;
8+
private String eyeColor;
9+
private Date birthday;
10+
11+
/**
12+
* 构造方法具有两个特性:①名称是固定的,与类名相同; ②没有返回值,也不能有返回值
13+
* @param name
14+
* @param age
15+
* @param eyeColor
16+
*/
17+
public Person(String name, int age, String eyeColor){
18+
this.name=name;
19+
this.age=age;
20+
this.eyeColor=eyeColor;
21+
}
22+
23+
/**
24+
* 构造方法的重载
25+
* @param name
26+
* @param age
27+
*/
28+
public Person(String name, int age){
29+
this.name=name;
30+
this.age=age;
31+
}
32+
33+
public String getName() {
34+
return this.name;
35+
}
36+
37+
public void setName(String name) {
38+
this.name=name;
39+
}
40+
41+
public int getAge() {
42+
return this.age;
43+
}
44+
45+
public void setAge(int age) {
46+
this.age=age;
47+
}
48+
49+
public String getEyeColor() {
50+
return eyeColor;
51+
}
52+
53+
public void setEyeColor(String eyeColor) {
54+
this.eyeColor = eyeColor;
55+
}
56+
57+
public Date getBirthday() {
58+
return birthday;
59+
}
60+
61+
public void setBirthday(Date birthday) {
62+
this.birthday = birthday;
63+
}
64+
65+
/**
66+
* 重载Object的toString()方法
67+
*/
68+
@Override
69+
public String toString() {
70+
String string="name: "+name+", age: "+age+", eyeColor: "+eyeColor;
71+
return string;
72+
}
73+
}

src/ABasic/A4String/StringBufferBuilder.java renamed to src/ABasic/A7Method/StringMethodReference.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package ABasic.A4String;
1+
package ABasic.A7Method;
22

3-
public class StringBufferBuilder {
3+
public class StringMethodReference {
44
public static void main(String[] args) {
55
stringMethod();
66
}

0 commit comments

Comments
 (0)