书接上文
44.有时候会遇到给类排序的情况,这是就要自己写一个排序的标准,比如要按照Student类中的age属性进行排序,有两种写法,方法一是Student实现comparable接口,方法二是写一个实现comparator的类,方法一没有方法二好,原因在于方法一只能完成一种排序,因为类中只能有一个compareTo()函数,只能是升序或是降序的一种
方法一代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Student implements Comparable<Student>{
private int id;
private int age;
public Student(int id, int age) {
super();
this.id = id;
this.age = age;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Student [id=" + id + ", age=" + age + "]";
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
if (this.getAge() > o.getAge()) {
return 1;
} else if (this.getAge() < o.getAge()) {
return -1;
} else {
return 0;
}
}
}
public class ComparableTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Student> list = new ArrayList<>();
list.add(new Student(1, 20));
list.add(new Student(2, 20));
list.add(new Student(3, 19));
list.add(new Student(4, 22));
list.add(new Student(5, 18));
list.add(new Student(6, 23));
Collections.sort(list);
System.out.println(list);
}
}
[Student [id=5, age=18], Student [id=3, age=19], Student [id=1, age=20], Student [id=2, age=20], Student [id=4, age=22], Student [id=6, age=23]]
方法二代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Student {
private int id;
private int age;
public Student(int id, int age) {
super();
this.id = id;
this.age = age;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Student [id=" + id + ", age=" + age + "]";
}
}
class StudengAgeASC implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
if (o1.getAge() > o2.getAge()) {
return 1;
} else if (o1.getAge() < o2.getAge()) {
return -1;
} else {
return 0;
}
}
}
class StudentAgeDESC implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
if (o1.getAge() > o2.getAge()) {
return -1;
} else if (o1.getAge() < o2.getAge()) {
return 1;
} else {
return 0;
}
}
}
public class ComparatorTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Student> list = new ArrayList<>();
list.add(new Student(1, 20));
list.add(new Student(2, 20));
list.add(new Student(3, 19));
list.add(new Student(4, 22));
list.add(new Student(5, 18));
list.add(new Student(6, 23));
StudentAgeDESC desc = new StudentAgeDESC();
StudengAgeASC asc = new StudengAgeASC();
Collections.sort(list,desc);
System.out.println(list);
Collections.sort(list,asc);
System.out.println(list);
}
}
[Student [id=6, age=23], Student [id=4, age=22], Student [id=1, age=20], Student [id=2, age=20], Student [id=3, age=19], Student [id=5, age=18]]
[Student [id=5, age=18], Student [id=3, age=19], Student [id=1, age=20], Student [id=2, age=20], Student [id=4, age=22], Student [id=6, age=23]]
要想按照自己的意愿输出的话,需要重写toString()方法
45.做一个比较简单的网页,目的主要是练习将css,js,html文件分开
1.css
.biankuang{
width:20px;
height:40px;
border:1px solid #000;
font-size:16pt;
color:red;
text-align:center;
float:left;display:inline;
}
1.js
let timer=null;
let a;
let b;
let c;
function goTimer(){
// var v = document.getElementById("haha1");
if(timer==null){
timer=setInterval(function() {
a=document.getElementById("display1").innerHTML = Math.floor(Math.random()*2);
b=document.getElementById("display2").innerHTML = Math.floor(Math.random()*2);
c=document.getElementById("display3").innerHTML = Math.floor(Math.random()*2);
},500);}else{
window.clearInterval(timer);
timer=null;
if(a==b&&b==c){
document.getElementById("haha1").innerHTML = "win";
document.getElementById("haha1").style.width = "40px";
document.getElementById("haha1").style.height = "20px";
document.getElementById("haha1").style.border = "1px solid #000";
// v.style.border = "1px solid #000";
}else{
//var v2 = document.getElementById("haha1");
document.getElementById("haha1").innerHTML = "lost";
document.getElementById("haha1").style.width = "40px";
document.getElementById("haha1").style.height = "20px";
document.getElementById("haha1").style.border = "1px solid #F0F";
// v.style.border = "1px solid #111";
}
}
}
1.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>a page</title>
<link rel="stylesheet" type="text/css" href="1.css">
<script language="javascript" src="1.js"></script>
<!-- <script type="text/javascript">
let timer=null;
let a;
let b;
let c;
function goTimer(){
// var v = document.getElementById("haha1");
if(timer==null){
timer=setInterval(function() {
a=document.getElementById("display1").innerHTML = Math.floor(Math.random()*2);
b=document.getElementById("display2").innerHTML = Math.floor(Math.random()*2);
c=document.getElementById("display3").innerHTML = Math.floor(Math.random()*2);
},500);}else{
window.clearInterval(timer);
timer=null;
if(a==b&&b==c){
document.getElementById("haha1").innerHTML = "win";
document.getElementById("haha1").style.width = "40px";
document.getElementById("haha1").style.height = "20px";
document.getElementById("haha1").style.border = "1px solid #000";
// v.style.border = "1px solid #000";
}else{
//var v2 = document.getElementById("haha1");
document.getElementById("haha1").innerHTML = "lost";
document.getElementById("haha1").style.width = "40px";
document.getElementById("haha1").style.height = "20px";
document.getElementById("haha1").style.border = "1px solid #F0F";
// v.style.border = "1px solid #111";
}
}
}
</script> -->
</head>
<body>
<div>
<!-- <div id = "display1" style = "width:20px;height:40px;border:1px solid #000;font-size:16pt;color:red;text-align:center;float:left;display:inline"></div>
<div id = "display2" style = "width:20px;height:40px;border:1px solid #000;font-size:16pt;color:red;text-align:center;float:left;display:inline"></div>
<div id = "display3" style = "width:20px;height:40px;border:1px solid #000;font-size:16pt;color:red;text-align:center;float:left;display:inline"></div> -->
<div id = "display1" class="biankuang"></div>
<div id = "display2" class="biankuang"></div>
<div id = "display3" class="biankuang"></div>
</div>
<br>
<div>
<button id = "btn" onclick ="goTimer()">click</button>
</div>
<br>
<div id = "haha1"></div>
</body>
</html>
在1.html中的注释其实就是css\js\html写在一起的情况,
利用jquery(记得饮用Jquery):
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>a page</title>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="1.css">
<!-- <script language="javascript" src="1.js"></script> -->
<script type="text/javascript">
let timer=null;
$( document ).ready(function(){
$("#btn").on("click",function(){
if(timer==null){
document.getElementById("haha1").innerHTML = "win";
timer=setInterval(function() {
$("#display1").text(Math.floor(Math.random()*2));
$("#display2").text(Math.floor(Math.random()*2));
$("#display3").text(Math.floor(Math.random()*2));
},500);
}else{
window.clearInterval(timer);
timer=null;
}
});
} );
</script>
</head>
<body>
<div>
<!-- <div id = "display1" style = "width:20px;height:40px;border:1px solid #000;font-size:16pt;color:red;text-align:center;float:left;display:inline"></div>
<div id = "display2" style = "width:20px;height:40px;border:1px solid #000;font-size:16pt;color:red;text-align:center;float:left;display:inline"></div>
<div id = "display3" style = "width:20px;height:40px;border:1px solid #000;font-size:16pt;color:red;text-align:center;float:left;display:inline"></div> -->
<div id = "display1" class="biankuang"></div>
<div id = "display2" class="biankuang"></div>
<div id = "display3" class="biankuang"></div>
</div>
<br>
<div>
<button id = "btn">click</button>
</div>
<div id ="haha1"></div>
</body>
</html>
46.做单体测试时,有setup(),teardown(),分别是测试类执行之前和之后会执行的