Skip to content

Commit 1b4dd3f

Browse files
committed
Java第七天
1 parent 7b3c6d6 commit 1b4dd3f

44 files changed

Lines changed: 1077 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
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+
class Phone {
2+
private String brand;
3+
private int price;
4+
private String color;
5+
6+
public Phone() {}
7+
8+
public Phone(String brand,int price,String color) {
9+
this.brand = brand;
10+
this.price = price;
11+
this.color = color;
12+
}
13+
14+
public void setBrand(String brand) {
15+
this.brand = brand;
16+
}
17+
18+
public String getBrand() {
19+
return brand;
20+
}
21+
22+
public void setPrice(int price) {
23+
this.price = price;
24+
}
25+
26+
public int getPrice() {
27+
return price;
28+
}
29+
30+
public void setColor(String color) {
31+
this.color = color;
32+
}
33+
34+
public String getColor() {
35+
return color;
36+
}
37+
38+
public void show() {
39+
System.out.println("我的手机是:"+brand+",价格是:"+price+",颜色是:"+color);
40+
}
41+
}
42+
class PhoneTest {
43+
public static void main(String[] args) {
44+
//无参+setXxx()
45+
Phone p = new Phone();
46+
p.setBrand("三星");
47+
p.setPrice(1000);
48+
p.setColor("黑色");
49+
p.show();
50+
51+
//带参
52+
Phone pp = new Phone("华为",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+
class Student {
25+
//成员变量
26+
private String name;
27+
private int age;
28+
29+
//构造方法
30+
public Student() {
31+
System.out.println("我是无参构造方法");
32+
//return;
33+
}
34+
35+
public Student(String name) {
36+
this.name = name;
37+
}
38+
39+
public Student(int age) {
40+
this.age = age;
41+
}
42+
43+
public Student(String name,int age) {
44+
this.name = name;
45+
this.age = age;
46+
}
47+
48+
//getXxx()/setXxx()方法
49+
public void setName(String name) {
50+
this.name = name;
51+
}
52+
53+
public String getName() {
54+
return name;
55+
}
56+
57+
public void setAge(int age) {
58+
this.age = age;
59+
}
60+
61+
public int getAge() {
62+
return age;
63+
}
64+
65+
//显示所有成员变量的方法
66+
public void show() {
67+
System.out.println("姓名是:"+name+",年龄是:"+age);
68+
}
69+
}
70+
71+
class StudentDemo {
72+
public static void main(String[] args) {
73+
//创建对象
74+
Student s = new Student();
75+
s.show();
76+
77+
//创建对象
78+
Student s2 = new Student("林青霞");
79+
s2.show();
80+
81+
//创建对象
82+
Student s3 = new Student(28);
83+
s3.show();
84+
85+
//创建对象
86+
Student s4 = new Student("林青霞",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+
class Student {
22+
private String name;
23+
private int age;
24+
25+
public Student() {}
26+
27+
public Student(String name,int age) {
28+
this.name = name;
29+
this.age = age;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public void setAge(int age) {
41+
this.age = age;
42+
}
43+
44+
public int getAge() {
45+
return age;
46+
}
47+
48+
public void show() {
49+
System.out.println("姓名是:"+name+",年龄是:"+age);
50+
}
51+
}
52+
53+
class StudentTest {
54+
public static void main(String[] args) {
55+
//无参+setXxx
56+
Student s1 = new Student();
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+
Student s2 = new Student("王重阳",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+
class MyMath {
5+
public int add(int a,int b) {
6+
return a + b;
7+
}
8+
9+
public int subtract(int a,int b) {
10+
return a - b;
11+
}
12+
13+
public int multiply(int a,int b) {
14+
return a * b;
15+
}
16+
17+
public int divide(int a,int b) {
18+
return a / b;
19+
}
20+
}
21+
class MyMathDemo {
22+
public static void main(String[] args) {
23+
//创建对象
24+
MyMath my = new MyMath();
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)