留着自己看的Java基础知识

[b]以下代码可以复制直接执行![/b][color=blue][/color]


测 试:
在父类中有一个方法和一个构造方法 在构造方法中调用那个方法,
子类重写父类的那个方法,当 new 子类 用父类接收时,
父类的构造方法中调用的那个方法是调用自己的还是子类的?
测试结果:Desccend.amethod()
1
Desccend.amethod()
结 论:
在父类中有一个方法和一个构造方法 在构造方法中调用那个方法
子类重写父类的那个方法,
当 new 子类 用父类接收时,父类的构造方法中调用的那个方法
将是调用子类重写的那个方法。
示例:
public class Base {
int i=1;
public void amethod(){
System.out.println("Base.amethod()");
}
Base(){
amethod();
}
}

public class Desccend extends Base{
int i=-1;
public static void main(String[] args) {
Base b=new Desccend();
System.out.println(b.i);
b.amethod();
}
public void amethod(){
System.out.println("Desccend.amethod()");
}
}

------------------------------------------------------------------
测 试:测试走不走Catch后面不在finally里的语句?

测试结果:finallyExceptionFinished
结 论:不管捕捉没捕捉到异常都会走Catch块后面的语句,但是如果Catch块里面有异常会中断就不会走不在finally块中的语句了

示例:
第一种情况:没有捕捉到异常
public class Test {
public static void aMethod(){
try{

}finally{
System.out.print("finally");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
aMethod();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.print("Exception");
}
System.out.print("Finished");
}
}
第二种情况:捕捉到异常
public class Test {
public static void aMethod()throws Exception{
try{
throw new Exception();
}finally{
System.out.print("finally");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
aMethod();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.print("Exception");
}
System.out.print("Finished");
}
}

------------------------------------------------------------------
测试内容:
1、测 试:alert((3+4/2))弹出的内容
测试结果:弹出:3.5
结 论:alert()括号中会进行计算

2、测 试:使层隐藏
测试结果:以下两种方式可以实现层隐藏
结 论:以下两种方式都可以
document.getElementsByName('1')[0].style.display='none';
document.getElementById('1').style.display='none';

3、测 试: <div ID="info"><p>请填写</p></div>
alert(document.getElementById('info').innerHTML)弹出的内容
测试结果:弹出:<p>请填写</p>
结 论:alert()不会去管是不是标签而是直接弹出,如果里面是数值的话,他会先计算后弹出

示例:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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>My JSP 'index.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script>

function show()
{
var x=3;
var y=4;
var z=2;
<%-- 弹出的是:3.5 --%>
alert((x+y)/z);
<%-- 使DIV标签隐藏的两种方式 --%>
document.getElementsByName('1')[0].style.display='none';
document.getElementById('1').style.display='none';
<%-- 弹出的是:<p>请填写</p> --%>
alert(document.getElementById('info').innerHTML);
}
<%-- 会走if,弹出: 2很好 --%>
function show1()
{
var a =10;
var b=5;
var c=10/5;
if(c==0||a>5)
{
confirm(c+"很好");
}
else if(c>0&&a<5)
{
confirm(c+"一般");
}
else
{
confirm(c+"很差");
}
}
</script>
</head>

<body οnlοad="show()">
This is my JSP page. <br>
<%-- 单击按钮时,按钮的背景颜色会变成红色 --%>
<input type="button" value="ok" οnclick="this.style.background='red'"/>
<%-- 测试两种方式 隐藏div --%>
<div id="1" name="1">123</div>
<%-- 测试弹出信息的div --%>
<div ID="info"><p>请填写</p></div>
<%

%>
</body>
</html>

------------------------------------------------------------------
测试:在HashMap可不可以用null作为键或值?
测试结果:2
结论:可以

示例:
import java.util.HashMap;
public class Ttt {
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap h=new HashMap();
h.put(null,"123");
h.put("11", null);
h.put(null, null);
System.out.println(h.size());
}
}
------------------------------------------------------------------
示例1
测试:输出的结果
测试结果:101
结论:不会报错,会把能转的转了。

示例1:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script>
function show()
{
var r1=parseInt("101中学")
document.write(r1);
}
</script>
</head>

<body onLoad="show()">
</body>
</html>

示例2
测试:输出的结果
测试结果:NaN
结论:不会报错,转不了会输出NaN。

示例2:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script>
function show()
{
var r1=parseInt("中学")
document.write(r1);
}
</script>
</head>

<body onLoad="show()">
</body>
</html>
-------------------------------------------------------------------------------
程序会出现编译错误
写了有参构造后,默认的无参构造方法会没有了!
public class Test {

/**
* @param args
*/
int number;
String strname;
Test(int num,String name)
{
number=num;
strname=name;
System.out.println("学号:"+number+"姓名:"+name);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test objTest1=new Test();
Test objTest2=new Test(1,"张三");
}

}
-----------------------------------------------------------------------------------------------------
测 试:程序第一次运行会输出什么,程序10秒后关闭的方法是哪个?

测试结果:i=1
i=10
10秒后提示用户关闭窗口

结 论:当想要多长时间后提示用户关闭窗口时用:setTimeout('window.close()',时间(单位是毫秒));
setTimeOut() out首字母大写是不会执行的。

示例:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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>My JSP 'index.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
setTimeout('window.close()',10000);
<%-- out 首字母大写是不会执行关闭的--%>
<%-- setTimeOut('window.close()',10000); --%>
</script>
</head>

<body>
<%!int i=1;%>
<%!
int getNumber(){
return i++;
}
%>
<%
int i=10;
out.print("i="+getNumber()+"<br>");
out.print("i="+i);
%>
</body>
</html>
------------------------------------------------------------------------------------------
测 试:程序运行的结果?
测试结果:出现编译错误
结 论:

示例:
public class Parent1 {
Parent1(String s){
System.out.println(s);
}
}
public class Parent2 extends Parent1 {
Parent2(){
System.out.println("Parent2");
}
}
Public class Child extends Parent2{
public static void main(String[] args){
Child child=new Child();
}
}

----------------------------------------------------------------------------------
测试:输出的结果?

测试结果:9

示例:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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>My JSP 'index.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<%!
public static void func(int num){
++num;
}
%>
<%int num=9;func(num);out.println(num++); %>
</body>
</html>
------------------------------------------------------------------------------------------------
测试:输出结果?

测试结果:a=3 b=1

测试结论:在小脚本中声明的方法是界面的方法,如果做数值增加的话,界面若不关闭 刷新一次增加1,在小脚本中声明的变量会初始化

示例:
<%! int a=0; %>
<%
int b=0;
a++;
b++;
%>
a=<%=a %>
b=<%=b %>
------------------------------------------------------------------------------------------------
测试:假如,今天的日期是2010年6月8日9时17分 下面代码输出的结果?
测试结果:现在的时间是:9:17
测试结论:document.write()会解析里面的内容然后弹出

示例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script>
function show()
{
var today=new Date();
document.write("现在的时间是:"+today.getHours()+":"+today.getMinutes());
}
</script>
</head>

<body onLoad="show()">
</body>
</html>
----------------------------------------------------------------------------------
问题:alert();document.write();out.print();哪个会解析标签?哪个不会解析标签?
哪个会先计算里面的数值再显示?哪个不会对里面的数值进行计算而直接显示里面的内容?

测试结果:11
结 论:document.write()会先计算再显示
示例:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script>
function show()
{
x=11;
y="number";
m=x+y;
document.write(10+1);
}
</script>
</head>

<body onLoad="show()">

</body>
</html>
----------------------------------------------------------------------------------------
测试:输出的结果
测试结果:11number
测试结论:不加var也可以,不会报错

示例:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script>
function show()
{
x=11;
y="number";
m=x+y;
document.write(m);
}
</script>
</head>
<body onLoad="show()">

</body>
</html>
----------------------------------------------------------------------------------
JSP代码如下:有两个客户依次使用浏览器浏览该JSP,且每个客户只浏览一次,那么第二个客户看到的浏览器显示
结果是:23
for循环中的j会先判断然后再执行++

<%!static int i=0; %>
<% int j=0;%>
<%
for(;j++<2;)
{
out.print(i++);
out.print("");
}
%>
---------------------------------------------------------------------------------------
public class Partent {
private int age;
public int getSalary(int level){
return 3000+level*1000;
}
}
public class Test extends Partent {
public int workYear;
public static void main(String[] args){
Partent p=new Partent();
Test t=new Test();
int i;
//i=workYear; 这句会报错,如果把workYear前面加上static的话就不报错了
// 下面两句是正确的
i=t.workYear;
i=p.getSalary(3);
}
}
----------------------------------------------------------------------------------------
jsp页面测试输出结果
测试结果:str is null
并不会报错

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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>My JSP 'index.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<%String str=null; %>
str is <%=str %>
</body>
</html>
---------------------------------------------------------------------------
jsp页面测试输出结果
测试结果:buffer is ABC
并不会报错

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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>My JSP 'index.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<%
java.lang.StringBuffer buffer=new StringBuffer();
buffer.append("ABC");
%>
buffer is <%=buffer %>
</body>
</html>
---------------------------------------------------------------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值