第七次网页前端培训(Java Script第三次)(p24-p26)

学习网址:

【优极限】 HTML+CSS+JavaScript+jQuery前端必学教程,小白教学,前端基础全套完成版_哔哩哔哩_bilibili


三、基础语法

10.内置对象

名称属性
Arguments只在函数内部定义,保存了函数的实参
Array数组对象
Date日期对象,用来创建和获取日期
Nath数学对象
String字符串对象,提供对字符串的一系列操作

(1)String

charAt(idx)返回指定位置处的字符
indexOf(Chr)返回指定字符串的位置,从左到右。找不到返回-1
substr(m,n)返回给定字符串中从m位置开始,取n个字符;若n省略,则意味着取到最后一位
substring(m,n)返回给定字符串中从m位置开始,到n位置结束;若n省略,则意味着取到最后一位
toLowerCase()将字符串的字符全部转化成小写
toUpperCase()将字符串的字符全部转化成大写
length属性,不是方法,返回字符串的长度

<script type="text/javascript">
		var str = "Hello World!";
		console.log(str.substring(3));
		console.log(str.substring(3,5));
		console.log(str.length);
		console.log(str.toUpperCase());
		console.log(str.toLowerCase());
		console.log(str.substr(1,6));
		console.log(str.substr(1));
		console.log(str.charAt(3));
		</script>

 (2)Math

Math.random()随机数
Math.floor()向下取整,小于最小整数
Math.ceil()向上去正,大于最大整数

<script type="text/javascript">
	console.log(Math.ceil(1.2));
	console.log(Math.random());
	console.log(Math.floor(1.2));
</script>

(3) Date

<script type="text/javascript">
	var date = new Date();//直接获取时间
	console.log(date);
	console.log(date.getFullYear());//年
	console.log(date.getMonth()+1);//月
	console.log(date.getDay());//日
	console.log(date.getHours());//时
	console.log(date.getMinutes());//分
	console.log(date.getSeconds());//秒
</script>

 11. 对象

 {

         键1:值1;

         键2:值2;

         ········

}

(1)对象的创建

  • 字面量形式创建对象
<script type="text/javascript">
	var obj1 = {};
	var obj2 = {
		name:"zhangsan",
		age:18,
	};
	console.log("obj3:"+obj1);
	console.log("obj2:"+obj2);
</script>

  • 通过 newObject 创建
<script type="text/javascript">
	var obj3 = new Object();
	console.log(obj3);
	var obj4 = Object.create(null);
</script>

  • 通过 Object 对象的 create 方法创建
<script type="text/javascript">
    var obj4 = Object.create(null);
	console.log(obj4);
	var obj5 = Object.create(obj2);
	console.log(obj5);
</script>

 (2)对象的操作

  • 获取对象的属性
<script type="text/javascript">
    var obj2 = {
		name:"zhangsan",
		age:18,
	};
    console.log(obj2.name);//zhangsan
	console.log(obj1.name);//undefined
	console.log(obj3.name);//undefined
	console.log(obj4.name);//undefined
	console.log(obj5.name);//zhangsan
</script>

  • 设置对象的属性
<script type="text/javascript">
    var obj2 = {
		name:"zhangsan",
		age:18,
	};
    obj2.age = 20;
	obj2.tel = "10088";
	console.log(obj2);
</script>

  •  对象的序列化和反序列化
  1. 序列化:将JS对象转换成JOSN字符串
  2. 序列化:将JOSN字符串转换成JS对象
<script type="text/javascript">
    var objx = {
		name:"zhangsan",
		age:18,
		tel:"10088",
	};
	var objstr = '{"name":"zhangsan","age":18,"tel":"10088"}';
	console.log(objx);
	console.log(objstr);
	var objxToStr = JSON.stringify(objx);//对象转化为字符串
	console.log(objxToStr);
	console.log("-----");
	var strToobj = JSON.parse(objstr);//字符串转化为对象
	console.log(strToobj);
</script>

 (3)this

           this 是 JavaScript 语言的一个关键字。

           谁调用函数,this指代谁。

  • 直接调用函数,this代表全局的window对象
<script type="text/javascript">
    function test(){
		console.log("这是一个测试方法");
		console.log(this);//window对象
	}
	test();
</script>

  • 调用对象中的函数,this代表的是对象本身
<script type="text/javascript">
    var o = {
		name:"zhangsan",
		age:18,
		tel:"10088",
		SayHello:function(){
			console.log("你好呀~");
			console.log(this);//当前o对象
		},
	};
	o.SayHello();
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在上一题Point2D和Point3D类的基础上,新建一个TestPointV2类,在TestPointV2类的main()方法中添加如下语句。 Scanner sc = new Scanner(System.in); System.out.println("Please enter the coordinates of p23:"); double p23x = sc.nextDouble(); double p23y = sc.nextDouble(); Point2D p23 = new Point2D(p23x, p23y); System.out.println("Please enter the coordinates of p31:"); double p31x = sc.nextDouble(); double p31y = sc.nextDouble(); double p31z = sc.nextDouble(); Point3D p33 = new Point3D(p31x, p31y, p31z); System.out.println("Please enter the coordinates of p24:"); double p24x = sc.nextDouble(); double p24y = sc.nextDouble(); double p24z = sc.nextDouble(); sc.close(); // The reference of the parent class refers to the object of the subclass. Point2D p24 = new Point3D(p24x, p24y, p24z); System.out.println("Does " + p23 + " coincide with " + p33 + "? -- "+ p23.equals(p33)); System.out.println("Does " + p33 + " coincide with " + p23 + "? -- "+ p33.equals(p23)); System.out.println("Does " + p33 + " coincide with " + p24 + "? -- "+ p33.equals(p24)); System.out.println("Does " + p24 + " coincide with " + p33 + "? -- "+ p24.equals(p33)); System.out.println("Does " + p23 + " coincide with " + p24 + "? -- "+ p23.equals(p24)); System.out.println("Does " + p24 + " coincide with " + p23 + "? -- "+ p24.equals(p23)); 假设引用变量p23、p33和p24所指点对象的坐标依次为(0, 0),(0, 0, 5),(0, 0, 5)。从键盘输入这三个点的坐标值,上述语句的运行结果如下: Please enter the coordinates of p23: 0 0 Please enter the coordinates of p31: 0 0 5 Please enter the coordinates of p24: 0 0 5 Does (0.0, 0.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0)? -- true 该结果显然不符合事实,请分析原因并改进Point2D类的代码,使得上述TestPointV2类的代码能够得到正确的运行结果。
05-05
上述运行结果不正确的原因在于 Point2D p24 = new Point3D(p24x, p24y, p24z); 这一语句,将一个 Point3D 对象赋值给了 Point2D 类型的引用变量 p24,导致在调用 p24.equals(p33) 时出现了错误的结果。 为了解决这个问题,我们需要在 Point2D 类中添加一个 equals 方法,用于比较两个点是否相同。具体代码如下: ```java public boolean equals(Object obj) { if (obj instanceof Point2D) { Point2D other = (Point2D) obj; return this.x == other.x && this.y == other.y; } return false; } ``` 这个 equals 方法首先判断参数是否为 Point2D 类型,如果是,再将其转换为 Point2D 类型进行比较。这样就可以避免上述问题的出现。 修改后的 Point2D 类的完整代码如下: ```java public class Point2D { protected double x; protected double y; public Point2D() { this(0, 0); } public Point2D(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } public boolean equals(Object obj) { if (obj instanceof Point2D) { Point2D other = (Point2D) obj; return this.x == other.x && this.y == other.y; } return false; } } ``` 修改后的 TestPointV2 类的完整代码如下: ```java import java.util.Scanner; public class TestPointV2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter the coordinates of p23:"); double p23x = sc.nextDouble(); double p23y = sc.nextDouble(); Point2D p23 = new Point2D(p23x, p23y); System.out.println("Please enter the coordinates of p31:"); double p31x = sc.nextDouble(); double p31y = sc.nextDouble(); double p31z = sc.nextDouble(); Point3D p33 = new Point3D(p31x, p31y, p31z); System.out.println("Please enter the coordinates of p24:"); double p24x = sc.nextDouble(); double p24y = sc.nextDouble(); double p24z = sc.nextDouble(); sc.close(); // The reference of the parent class refers to the object of the subclass. Point2D p24 = new Point3D(p24x, p24y, p24z); System.out.println("Does " + p23 + " coincide with " + p33 + "? -- " + p23.equals(p33)); System.out.println("Does " + p33 + " coincide with " + p23 + "? -- " + p33.equals(p23)); System.out.println("Does " + p33 + " coincide with " + p24 + "? -- " + p33.equals(p24)); System.out.println("Does " + p24 + " coincide with " + p33 + "? -- " + p24.equals(p33)); System.out.println("Does " + p23 + " coincide with " + p24 + "? -- " + p23.equals(p24)); System.out.println("Does " + p24 + " coincide with " + p23 + "? -- " + p24.equals(p23)); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值