java 笔记(五)

本文介绍了两种排序方法,一种是通过实现Comparable接口对对象进行排序,另一种是使用Comparator接口实现自定义排序逻辑。此外,还展示了如何将CSS、JS和HTML文件进行分离以简化网页开发流程。
摘要由CSDN通过智能技术生成

书接上文

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(),分别是测试类执行之前和之后会执行的






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值