javascript this 和 java this 用法

// super 关键字表示父类的意思
// this 变量代表对象本身
// 当类中有两个同名变量,一个为类的成员变量,一个属于某个特定方法,使用this可以区分开成员变量和局部变量
// 可以使用super 访问父类被子类隐藏的变量或覆盖的方法 super.XX();

// 使用this 表示当前调用方法的对象的引用
// 希望在方法的内部获得当前对象的引用,可以使用关键字this,this 关键字只能在方法内部使用 表示"调用方法的那个对象的引用"

// <[Constructor call must be the first statement in a constructor]>
class Person {
    public static void prt(String s) {
       System.out.println(s);
    }
 
    // 父类无参数构造方法
    Person() {
       prt("A Person.");
    }
 
    
    Person(String name) {
       prt("A person name is:" + name);
    }
}
 
// [如果]调用super() 必须写在子类构造方法的第一行 否则编译不通过 每个子类构造方法的第一条语句都会隐含调用super()
// 如果父类没有这种形式的构造函数,那么编译就会出错

// super()和this()类似,区别是,super从子类中调用父类的构造方法,this()在同一类内调用其它方法

// super()和this()均需放在构造方法内第一行[要么不用 要么放在第一行]

// this和super不能同时出现在一个构造函数里面,因为this必然会调用其它的构造函数,
// 其它的构造函数必然也会有super语句的存在,所以在同一个构造函数里面有相同的语句,就失去了语句的意义,编译器也不会通

// this()和super()都指的是对象,所以,均不可以在static环境中使用。包括:static变量,static方法,static语句块。
public class Chinese extends Person {

	    Chinese() { 
	       super(); // 调用父类构造方法(1) 
	       prt("子类·调用父类”无参数构造方法“: "+"A chinese coder."); 
	    } 
	    
	    Chinese(String name) { 
	       super(name);// 调用父类具有相同形参的构造方法(2) 
	       // this(); // Constructor call must be the first statement in a constructor
	       prt("子类·调用父类”含一个参数的构造方法“: "+"his name is " + name); 
	    } 
	    
	    Chinese(String name, int age) { 
	       this(name);// 调用具有相同形参的构造方法(3) 
	       prt("子类:调用子类具有相同形参的构造方法:his age is " + age); 
	    } 
	    
	    public static void main(String[] args) { 
	    	//  this(); Constructor call must be the first statement in a constructor
	       Chinese cn = new Chinese(); // new 一个对象的时候 会调用相应的构造方法
	       cn = new Chinese("rufus"); // new 一个对象的时候 会调用相应的构造方法
	       cn = new Chinese("cathy", 18); // new 一个对象的时候 会调用相应的构造方法
	      
	    } 
}

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>JAVASCRIPT 用法</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script type="text/javascript">
	   function introduce()
	   {
	   		alert('hello, i am Laruence \r\n');
	   }
	   
	   // 定义在全局的函数,函数的所有者就是当前页面,也就是window对象
	   // 定义在全局的函数,其实也就是window 对象的一个方法
	   // 可以通过函数名直接调用,也可以通过window.方法名来调用,这时,方法中的this 关键字指向它的所有者window对象
	   window.introduce();
	   
	   var name = "Larence";
	   
	   function brifY()
	   {
	   		alert(this.name);
	   }
	
	   window.brifY();
	   
	   // alert(window.brifY); // window 对象的 brifY 属性
	   
	   function passname(value)
	   {
	   		alert('上传文件的名称是:' + value);
	   }
	   
	</script>
  </head>
  
  <body>
    <!-- javascript中  一切皆对象 函数也是 -->
    <!-- 在javascript 中this 永远指向函数(方法)的[所有者] -->
    <!-- 定义在全局的函数 函数的所有者就是当前页面 也就是window 对象 -->
    
    <!-- 在dom中 访问form表单元素方法form表单元素的值 只需要使用id.value就可以了 -->
    <!-- 在dom中 form表单元素也是对象,表单元素的事件是form表单元素的属性 事件函数中的this自然是指向当前form元素了 -->
    
    <form action="" id="myform" name="myform" method="post" enctype="multipart/form-data">
          要上传的文件名称 :<input id="myinput" name="myinput" οnclick="passname(this.value);" /><br/>
            上传文件的作者 :<input id="name"  name="name" value="rufus" οnclick="passname(this.value);" /> <br/> 
          选择要上传的图片 :<input type="file" name="fileUp" id="fileUp" οnchange="myinput.value = this.value"/>  <br/> 
            表达元素的类型 :<input type="file" name="fileUp" id="fileUp" οnchange="myinput.value = this.type"/>  
    <input type="submit" value="上传" />  
    </form> 
  </body>
</html>




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值