Skip to content

Commit 1b4dd3f

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

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.
+55
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+
}
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+
}
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.
Binary file not shown.
Binary file not shown.
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+
}
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
定义一个员工类,自己分析出几个成员,
3+
然后给出成员变量,构造方法,getXxx()/setXxx()方法,
4+
以及一个显示所有成员信息的方法。并测试。
5+
6+
Employee:
7+
成员变量:员工编号,姓名,职位
8+
构造方法:无参,带参
9+
成员方法:getXxx()/setXxx()方法,show()
10+
*/
11+
class Employee {
12+
private String eid;
13+
private String name;
14+
private String job;
15+
16+
public Employee() {}
17+
18+
public Employee(String eid,String name,String job) {
19+
this.eid = eid;
20+
this.name = name;
21+
this.job = job;
22+
}
23+
24+
public void setEid(String eid) {
25+
this.eid = eid;
26+
}
27+
28+
public String getEid() {
29+
return eid;
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 setJob(String job) {
41+
this.job = job;
42+
}
43+
44+
public String getJob() {
45+
return job;
46+
}
47+
48+
public void show() {
49+
System.out.println("员工编号是:"+eid+",姓名是:"+name+",职位是:"+job);
50+
}
51+
}
52+
53+
class EmployeeDemo {
54+
public static void main(String[] args) {
55+
//无参
56+
Employee e = new Employee();
57+
e.setEid("itcast007");
58+
e.setName("周星驰");
59+
e.setJob("高级工程师");
60+
e.show();
61+
62+
//带参
63+
Employee e2 = new Employee("itcast003","刘德华","挖掘机工程师");
64+
e2.show();
65+
}
66+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
定义一个类Demo,其中定义一个求两个数据和的方法,定义一个测试了Test,进行测试。
3+
4+
什么时候定义成员变量呢?
5+
要想知道在类中什么时候定义成员变量,就应该先思考成员变量和类的关系?
6+
成员变量是描述类的基本信息的。
7+
也就是说,只有和类有关系的变量才可以定义为成员变量。
8+
*/
9+
class Demo {
10+
private int x;
11+
private int y;
12+
13+
public Demo() {}
14+
15+
public Demo(int x,int y) {
16+
this.x = x;
17+
this.y = y;
18+
}
19+
20+
public void setX(int x) {
21+
this.x = x;
22+
}
23+
24+
public int getX() {
25+
return x;
26+
}
27+
28+
public void setY(int y) {
29+
this.y = y;
30+
}
31+
32+
public int getY() {
33+
return y;
34+
}
35+
36+
//成员变量已经有x,y了。这里就没有必要在定义了
37+
/*
38+
public int sum(int x,int y) {
39+
return x + y;
40+
}
41+
*/
42+
43+
public int sum() {
44+
return x + y;
45+
}
46+
}
47+
48+
class Test {
49+
public static void main(String[] args) {
50+
Demo d = new Demo();
51+
d.setX(10);
52+
d.setY(20);
53+
int result = d.sum();
54+
System.out.println(result);
55+
}
56+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
定义变量的时候,范围越小越好。
3+
*/
4+
class Demo {
5+
public int sum(int x,int y) {
6+
return x + y;
7+
}
8+
}
9+
10+
class Test2 {
11+
public static void main(String[] args) {
12+
Demo d = new Demo();
13+
int result = d.sum(10,20);
14+
System.out.println(result);
15+
}
16+
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)