大二的时候选的c++,而在web安全领域java也占很大的比重,一直想找时间学习一下,刚好碰到学妹问java的问题就趁机学习一波
下面代码都是根据实验指导书的要求写出来的,虽然都是一些基础的代码,但是其中包含了java的一些基础的东西,个人感觉记那么多概念还不如直接看代码来的清楚,记在这里也方便以后查阅。

exp2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package exp2.entities;

public class Student {
private String name;
private String id; // 学号
private int credit; // 已修学分
public static int creditForGraduation=100;

// 获取属性值
public Student() {
this.name="aaa";
this.id="111";
}
public String getName() {
return this.name;
}
public String getId() {
return this.id;
}
public int getCredit() {
return this.credit;
}

// 设置属性值
public void setName(String name) {
this.name=name;
}
public void setId(String id) {
this.id=id;
}
public void setCredit(int credit) {
this.credit=credit;
}

// 学生成绩等级
public void rate() {
if(this.credit>=9) {
System.out.println("优秀");
}else if(this.credit>=6&&this.credit<9) {
System.out.println("合格");
}else {
System.out.println("不合格");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package exp2.demo;
import exp2.entities.Student;

public class StudentDemo {
public static void main(String[] args) {
Student stu1;
stu1=new Student();
Student stu2;
stu2=new Student();

// 设置stu1的信息
stu1.setName("stu1");
stu1.setId("20123");
stu1.setCredit(9);

// 设置stu2的信息
stu2.setName("stu2");
stu2.setId("20456");
stu2.setCredit(7);
// System.out.println(stu1.getName());


// 输出stu1的信息
System.out.println(stu1.getName());
System.out.println(stu1.getId());
System.out.println(stu1.getCredit());
stu1.rate();

// 输出stu2的信息
System.out.println(stu2.getName());
System.out.println(stu2.getId());
System.out.println(stu2.getCredit());
stu2.rate();

}
}

exp3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package exp3.entities;

public class Student {
private String name;
private String id; // 学号
private double credit; // 已修学分
public static double creditForGraduation=100;

public static void modifyCreditForGraduation(double num) { // 静态方法
creditForGraduation=num; // 修改静态变量值
}

// 获取属性值
public Student() {
this.name="aaa";
this.id="111";
}
public String getName() {
return this.name;
}
public String getId() {
return this.id;
}
public double getCredit() {
return this.credit;
}

// 设置属性值
public void setName(String name) {
this.name=name;
}
public void setId(String id) {
this.id=id;
}
public void setCredit(int credit) {
this.credit=credit;
}

// 学生成绩等级
public void rate() {
if(this.credit>=9) {
System.out.println("优秀");
}else if(this.credit>=6&&this.credit<9) {
System.out.println("合格");
}else {
System.out.println("不合格");
}
}


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package exp3.entities;

public class StudentCadre extends Student {
private String headship;

public static void modifyCreditForGraduation(double num) { // 重写静态方法

}
public StudentCadre() { // 构造方法重载

}
public String getHeadship() {
return this.headship;
}
public void setHeadship(String headship) {
this.headship=headship;
}

public void speech() {
System.out.println(getName());
System.out.println(getHeadship());
}

public void rate() { // 覆盖父类的rate方法
if(getCredit()>=9.5) {
System.out.println("优秀");
}else if(getCredit()>=6&&getCredit()<9.5) {
System.out.println("合格");
}else {
System.out.println("不合格");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package exp3.demo;
import exp3.entities.*;
public class StudentDemo {
public static void main(String[] args) {
Student stu;
stu = new Student();

StudentCadre stuca;
stuca = new StudentCadre();

Student stus;
stus = new StudentCadre(); // 创建一个上转型对象


stu.modifyCreditForGraduation(300);
System.out.println(stu.creditForGraduation);

stuca.modifyCreditForGraduation(300);
System.out.println(stuca.creditForGraduation);

stus.modifyCreditForGraduation(300);
System.out.println(stus.creditForGraduation);

stu.setCredit(9);
System.out.println(stu.getCredit());
stu.rate();

stuca.setCredit(9);
System.out.println(stuca.getCredit());
stuca.rate();
}
}

最后更新: 2018年09月22日 01:04

原始链接: http://drac0nids.top/2018/09/18/java学习/

× 请我吃糖~
打赏二维码