Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1b4dd3f

Browse files
committed
Java第七天
1 parent7b3c6d6 commit1b4dd3f

File tree

44 files changed

+1077
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1077
-0
lines changed
1.21 KB
Binary file not shown.
569 Bytes
Binary file not shown.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
classPhone {
2+
privateStringbrand;
3+
privateintprice;
4+
privateStringcolor;
5+
6+
publicPhone() {}
7+
8+
publicPhone(Stringbrand,intprice,Stringcolor) {
9+
this.brand =brand;
10+
this.price =price;
11+
this.color =color;
12+
}
13+
14+
publicvoidsetBrand(Stringbrand) {
15+
this.brand =brand;
16+
}
17+
18+
publicStringgetBrand() {
19+
returnbrand;
20+
}
21+
22+
publicvoidsetPrice(intprice) {
23+
this.price =price;
24+
}
25+
26+
publicintgetPrice() {
27+
returnprice;
28+
}
29+
30+
publicvoidsetColor(Stringcolor) {
31+
this.color =color;
32+
}
33+
34+
publicStringgetColor() {
35+
returncolor;
36+
}
37+
38+
publicvoidshow() {
39+
System.out.println("我的手机是:"+brand+",价格是:"+price+",颜色是:"+color);
40+
}
41+
}
42+
classPhoneTest {
43+
publicstaticvoidmain(String[]args) {
44+
//无参+setXxx()
45+
Phonep =newPhone();
46+
p.setBrand("三星");
47+
p.setPrice(1000);
48+
p.setColor("黑色");
49+
p.show();
50+
51+
//带参
52+
Phonepp =newPhone("华为",799,"白色");
53+
pp.show();
54+
}
55+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
构造方法:给对象的数据进行初始化
3+
4+
特点:
5+
A:方法名与类名相同
6+
B:没有返回值类型,连void都没有
7+
C:没有具体的返回值
8+
9+
构造方法的格式:
10+
修饰符 类名(...) {
11+
12+
}
13+
14+
构造方法的注意事项:
15+
A:如果你不提供构造方法,系统会给出默认无参构造方法
16+
B:如果你提供了构造方法,系统将不再提供默认无参构造方法
17+
这个时候,如果你还想继续使用无参构造方法,只能自己给出。
18+
推荐:永远自己给出无参构造方法。
19+
C:构造方法也是可以重载的
20+
D:构造方法中可以有return语句吗?
21+
可以。只不过是return;
22+
23+
*/
24+
classStudent {
25+
//成员变量
26+
privateStringname;
27+
privateintage;
28+
29+
//构造方法
30+
publicStudent() {
31+
System.out.println("我是无参构造方法");
32+
//return;
33+
}
34+
35+
publicStudent(Stringname) {
36+
this.name =name;
37+
}
38+
39+
publicStudent(intage) {
40+
this.age =age;
41+
}
42+
43+
publicStudent(Stringname,intage) {
44+
this.name =name;
45+
this.age =age;
46+
}
47+
48+
//getXxx()/setXxx()方法
49+
publicvoidsetName(Stringname) {
50+
this.name =name;
51+
}
52+
53+
publicStringgetName() {
54+
returnname;
55+
}
56+
57+
publicvoidsetAge(intage) {
58+
this.age =age;
59+
}
60+
61+
publicintgetAge() {
62+
returnage;
63+
}
64+
65+
//显示所有成员变量的方法
66+
publicvoidshow() {
67+
System.out.println("姓名是:"+name+",年龄是:"+age);
68+
}
69+
}
70+
71+
classStudentDemo {
72+
publicstaticvoidmain(String[]args) {
73+
//创建对象
74+
Students =newStudent();
75+
s.show();
76+
77+
//创建对象
78+
Students2 =newStudent("林青霞");
79+
s2.show();
80+
81+
//创建对象
82+
Students3 =newStudent(28);
83+
s3.show();
84+
85+
//创建对象
86+
Students4 =newStudent("林青霞",28);
87+
s4.show();
88+
}
89+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
类的组成:
3+
成员变量
4+
构造方法
5+
成员方法
6+
7+
给类的成员变量赋值有几种方式:
8+
A:setXxx()方法
9+
B:带参构造方法
10+
11+
练习:
12+
Phone:
13+
成员变量:brand,price,color
14+
构造方法:无参,带参
15+
成员方法:setXxx()/getXxx()
16+
show()
17+
18+
PhoneTest:
19+
main
20+
*/
21+
classStudent {
22+
privateStringname;
23+
privateintage;
24+
25+
publicStudent() {}
26+
27+
publicStudent(Stringname,intage) {
28+
this.name =name;
29+
this.age =age;
30+
}
31+
32+
publicvoidsetName(Stringname) {
33+
this.name =name;
34+
}
35+
36+
publicStringgetName() {
37+
returnname;
38+
}
39+
40+
publicvoidsetAge(intage) {
41+
this.age =age;
42+
}
43+
44+
publicintgetAge() {
45+
returnage;
46+
}
47+
48+
publicvoidshow() {
49+
System.out.println("姓名是:"+name+",年龄是:"+age);
50+
}
51+
}
52+
53+
classStudentTest {
54+
publicstaticvoidmain(String[]args) {
55+
//无参+setXxx
56+
Students1 =newStudent();
57+
s1.setName("林青霞");
58+
s1.setAge(28);
59+
System.out.println(s1.getName()+"---"+s1.getAge());
60+
s1.show();
61+
System.out.println("----------------------------");
62+
63+
//带参
64+
Students2 =newStudent("王重阳",82);
65+
System.out.println(s2.getName()+"---"+s2.getAge());
66+
s2.show();
67+
68+
}
69+
}
Binary file not shown.
403 Bytes
Binary file not shown.
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
定义一个类MyMath,提供基本的加减乘除功能,然后进行测试。
3+
*/
4+
classMyMath {
5+
publicintadd(inta,intb) {
6+
returna +b;
7+
}
8+
9+
publicintsubtract(inta,intb) {
10+
returna -b;
11+
}
12+
13+
publicintmultiply(inta,intb) {
14+
returna *b;
15+
}
16+
17+
publicintdivide(inta,intb) {
18+
returna /b;
19+
}
20+
}
21+
classMyMathDemo {
22+
publicstaticvoidmain(String[]args) {
23+
//创建对象
24+
MyMathmy =newMyMath();
25+
26+
System.out.println("加法:"+my.add(23,34));
27+
System.out.println("减法:"+my.subtract(23,34));
28+
System.out.println("乘法:"+my.multiply(2,4));
29+
System.out.println("除法:"+my.divide(10,4));
30+
}
31+
}
1.16 KB
Binary file not shown.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp