以下简单的Java程序似乎通过语句显示字符串Hello World,System.out.println("Hello World");但它没有.它只是用另一个字符串替换它,在这种情况下,美好的一天!并在控制台上显示它.字符串Hello World根本不显示.我们来看看Java中的以下简单代码片段.
package goodday;
import java.lang.reflect.Field;
final public class Main
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
static
{
try
{
Field value = String.class.getDeclaredField("value");
value.setAccessible(true);
value.set("Hello World", value.get("Good Day !!"));
}
catch (Exception e)
{
throw new AssertionError(e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里只有一个关于此代码的问题.它完全按预期工作,但我无法减少字符串的好日子!.如果尝试这样做,则会导致a java.lang.ArrayIndexOutOfBoudsException.如果长度增加,程序运行良好但显示字符串中的其余字符被截断意味着两个字符串的长度应该有些相同.为什么?这是我无法理解的事情.
在面向对象的范例中,虚函数或虚方法是一种函数或方法,其行为可以通过具有相同签名的函数在继承类中重写,以提供多态行为.
根据定义,除了final方法和私有方法之外,Java中的每个非静态方法都是默认的虚方法.不能为多态行为继承的方法不是虚方法.
Java中的抽象类只不过是与C++等效的纯虚方法.
为什么我们说Java中的静态方法不是虚方法?即使我们可以覆盖静态方法,因此它可以提供多态性的一些优点,并且Java中的静态方法可以主要使用它的关联类名调用,但也可以使用它的关联类的对象来调用它.Java与调用实例方法的方式相同.
我们来看看下面的Javascript代码.
<script type="text/javascript" lang="javascript">
function test()
{
alert('2'+8);
alert(8-'2');
}
</script>
Run Code Online (Sandbox Code Playgroud)
在第一个警告框中,它显示连接2和8的结果,即28.但是在第二个警告框中,它显示两个数字的减法,即6.怎么样?
请考虑Java中的以下代码段.我知道temp[index] = index = 0;以下代码片段中的语句几乎是不可接受的,但在某些情况下可能是必要的:
package arraypkg;
final public class Main
{
public static void main(String... args)
{
int[]temp=new int[]{4,3,2,1};
int index = 1;
temp[index] = index = 0;
System.out.println("temp[0] = "+temp[0]);
System.out.println("temp[1] = "+temp[1]);
}
}
Run Code Online (Sandbox Code Playgroud)
它在控制台上显示以下输出.
temp[0] = 4
temp[1] = 0
Run Code Online (Sandbox Code Playgroud)
我不明白temp[index] = index = 0;.
怎么temp[1]包含0?这项任务是如何发生的?
我见过很多地方使用Math.floor()和Math.random()
如下
$('a.random-color').hover(function() { //mouseover
var col = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
$(this).animate({
'color': col,
'paddingLeft': '20px'
},1000);
},function() { //mouseout
$(this).animate({
'color': original,
'paddingLeft': '0'
},500);
});
});
Run Code Online (Sandbox Code Playgroud)
为何使用Math.floor()和Math.random()?
class Test extends JPanel implements MouseListener
{
private JButton b1, b2, b3, b4;
public Test()
{
setLayout (new GridLayout (2, 3));
b1 = new JButton ("Button 1");
b2 = new JButton ("Button 2");
add (b1);
add (b2);
}
public void mousePressed (MouseEvent event)
{
if (event.getText() == b1.getText())
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道在类MouseListener或JPanel类中是否有一个方法允许我获取单击按钮的文本.谢谢
我正在尝试使用Spring和Hibernate使用HibernateValidator在JSP中验证一个简单的表单.JSP页面Temp.jsp如下(web.xml中的url pttern是*.htm).
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<form:form method="post" action="Temp.htm" commandName="validationForm">
<!--validationForm is a model class-->
<table>
<tr>
<td>User Name:<font color="red"><form:errors path="userName" /></font></td>
</tr>
<tr>
<td><form:input path="userName" /></td>
</tr>
<tr>
<td>Age:<font color="red"><form:errors path="age" /></font></td>
</tr>
<tr>
<td><form:input path="age" /></td>
</tr>
<tr>
<td>Password:<font color="red"><form:errors path="password" /></font></td>
</tr>
<tr>
<td><form:password path="password" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
Run Code Online (Sandbox Code Playgroud)
课程validationForm如下.
package validators;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import …Run Code Online (Sandbox Code Playgroud) 我明白那个:
'\n' // literally the backslash character followed by the character for lowercase n
"\n" // interpreted by php as the newline character
Run Code Online (Sandbox Code Playgroud)
但对于我的生活,我无法理解为什么'\n' === '\\n'.在我看来,'\\n'将等于三个单独的字符:两个单独的反斜杠,后跟字母n.
'\n' === '\\n'PHP中的原因是什么?
以下代码用于检查模糊字段中是否输入了4个数字.如果不是,则删除字段值,并且该字段被聚焦.删除工作正常,但对focus()的调用不起作用.
$('input.dateValue').live('blur',function(event){
if (!(/(\d){4}$/.test($(this).attr('value')))) $(this).attr('value','').focus();
});
Run Code Online (Sandbox Code Playgroud)
为什么对焦点()的调用不关注该领域?
有一点pdo问题,我现在已经工作了一段时间.由于我不知道这里有什么问题,我想把它带到这个清单.也许有些人知道更多......
我有一个登录网站,用于检查用户和密码对mysql驱动的数据库.当在同一个文件中建立pdo连接一切正常时,可以登录,没有任何问题.正如它应该工作......
但是,当我将数据库连接部分移动到我从另一个文件中包含的单独函数时,pdo就失败了,并且给了我:
SQLSTATE [28000] [1045]拒绝访问用户'...'@'...'(使用密码:否)致命错误:在/.../中的非对象上调用成员函数prepare() ....../......在第41行
为清楚起见,这里是代码:
版本1:
这有效:
<?php
require "./vars_and_functions.php";
/* open database connection */
try {
$pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
/* query */
$query = "SELECT uname, passw FROM members WHERE uname = ? AND passw = ?";
$q = $pdo->prepare($query);
$q->execute(array($u_name, $p_word_md5));
$result = $q->rowCount();
if($result == 1) { /* we have a match */
/* close the database connection */
$pdo = null;
/* and redirect */
header("...");
} /* if */ …Run Code Online (Sandbox Code Playgroud)