下面我有两个班.父母和孩子.Child类继承自Parent类.在Parent类构造函数中,我调用Parent类的print()方法.
当我在main()方法中为子类创建Object时,运行Parent类构造函数并调用Child类的print()方法而不是Parent类的print()方法.
Q1.为什么会发生这种情况.
Q2.为什么i的值为0
public class Sample
{
public static void main(String[] args)
{
Child objChild = new Child();
objChild.print();
}
}
class Parent
{
void print()
{
System.out.println("i Value");
}
Parent()
{
print();
}
}
class Child extends Parent
{
int i = 45;
void print()
{
System.out.println("i Value = "+i);
}
}
Run Code Online (Sandbox Code Playgroud)
OP
i Value = 0
i Value = 45
Run Code Online (Sandbox Code Playgroud) 标记为最终倾斜的引用变量重新分配给不同的对象.可以修改具有in对象的数据,但不能更改引用变量.
根据我的理解,我在下面创建了一个代码,我试图重新分配155的新UserId.由于定义,我只是尝试更改对象中的数据.但参考是一样的.
public class FinalClass
{
public static void main(String[] args)
{
ChildClass objChildClass = new ChildClass();
objChildClass.UserId = 155;
}
}
class ChildClass
{
public static final int UserId = 145;
}
Run Code Online (Sandbox Code Playgroud)
我相信我误解了上述概念.
请举例说明.
谢谢你的答复.
为什么在将值分配给Arrays.asList后,我无法将元素添加到List
List<Integer> sam = Arrays.asList(1,2,3,4);
sam.add(5);
for (Integer integer : sam)
{
System.out.println(integer);
}
Run Code Online (Sandbox Code Playgroud) 我有一个JSP页面,其中包含HTML表单中的复选框,如下所示

现在,在编辑用户技能时,我想从表中获取逗号分隔值,并填充JSP中的复选框.以下代码从数据库表中提供CSV技能.
List<UserDetails> Skills = new ArrayList<UserDetails>();
pstmt = (PreparedStatement) conn.prepareStatement(strSQL);
rs = pstmt.executeQuery();
String strSkills = rs.getString("Skills");
List<String> items = Arrays.asList(strSkills.split("\\s*,\\s*"));
objUserDetails.setSkills(items.toArray(new String[0]));
Skills.add(objUserDetails);
return Skills;
Run Code Online (Sandbox Code Playgroud)
现在我需要填充JSP中的复选框,并选中相应的技能.我使用了Request getAttribute()方法,我将传递给JSP,如下所示
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
dbUtil objdbUtil = new dbUtil();
List<UserDetails> Skills = objdbUtil.getSkills();
request.setAttribute("arrSkills", Skills);
RequestDispatcher rqst = request.getRequestDispatcher("Skills.jsp");
rqst.forward(request, response);
}
Run Code Online (Sandbox Code Playgroud)
如何使用我在arrSkills数组中获得的技能并填充复选框.我试过用
<c:forEach var="account" items="${arrUsersList}">
<input type="checkbox" name="chkSkills" id="chkPHP" value="PHP"/>PHP
<input type="checkbox" name="chkSkills" id="chkJava" value="Java"/>Java
<input type="checkbox" name="chkSkills" id="chkMySQL" value="MySQL"/>MySQL
<input …Run Code Online (Sandbox Code Playgroud) 我使用了Struts 2框架,并创建了一个具有登录页面的Web应用程序.我有三个不同的Action类,名称Action1,Action2和Action3,以及通过在Action类中运行一些业务逻辑呈现的JSP页面的不同视图.
现在,我想检查用户是否在Action类执行处理之前已登录.所以,我在下面创建了一个拦截器工作正常.
public String intercept(ActionInvocation invocation) throws Exception
{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session = request.getSession();
if(session.isNew())
{
response.sendRedirect("Login.action");
}
System.out.println("Interceptor Fired");
String result = invocation.invoke();
return result;
}
Run Code Online (Sandbox Code Playgroud)
我想要的struts.xml不是为下面的所有动作添加拦截器
<interceptor-ref name="newStack"/>
Run Code Online (Sandbox Code Playgroud)
我的struts.xml档案有
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="printMsgInterceptor" class="LoginInterceptor"></interceptor>
<interceptor-stack name="newStack">
<interceptor-ref name="printMsgInterceptor"/>
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<action name="actone" class="Action1">
<result name="success">/success.jsp</result>
<interceptor-ref name="newStack"/>
</action>
<action …Run Code Online (Sandbox Code Playgroud) 我的报告页面使用 java date java.util.Calendar 和 java.text.SimpleDateFormat 。
我想始终将开始日期设置为上一个星期六,并将结束日期设置为该星期六之后的星期五。
我写了一个 Java 代码来检查这个并按如下方式执行,但我确信它的逻辑是错误的
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -7);
setToDate(sdf.format(cal.getTime()));
cal.add(Calendar.DATE, -6);
setFromDate(sdf.format(cal.getTime()));
Run Code Online (Sandbox Code Playgroud)
如何获取上一个 FromDate(yyyy-mm-dd) 和 ToDate(yyyy-mm-dd) 其中 FromDate 应该是上周六,ToDate 应该是上周五。


我想为我的报告创建一个HTML文件.可以使用创建报告中的内容BufferedWriter#write(String)
File f = new File("source.htm");
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write("Content");
Run Code Online (Sandbox Code Playgroud)
或者通过使用 DataOutputStream#writeBytes(String)
File f = new File("source.htm");
DataOutputStream dosReport = new DataOutputStream(new FileOutputStream(f));
dosReport.wrtiteBytes("Content");
Run Code Online (Sandbox Code Playgroud)
其中一个比另一个好吗?为什么会这样?
我使用subList Method.Now创建了一个新的ArrayList,当我尝试使用retainAll执行交集操作时抛出以下异常
retainAll()方法适用于下面的代码
List<Integer> arrNums1 = new ArrayList<Integer>();
arrNums1.add(1);
arrNums1.add(2);
arrNums1.add(3);
List<Integer> arrNums2 = arrNums1.subList(0, 1);
arrNums2.retainAll(arrNums1);
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将retainAll应用于Below代码时,它会生成Exception,如下所示
Java代码
public class Generics1
{
public static void main(String[] args)
{
List<Fruits> arrFruits = new ArrayList<Fruits>();
Fruits objApple = new Apple();
Fruits objOrange = new Orange();
Fruits objMango = new Mango();
arrFruits.add(objApple);
arrFruits.add(objOrange);
arrFruits.add(objMango);
List<Fruits> arrNewFruits = arrFruits.subList(0, 1);
System.out.println(arrFruits.retainAll(arrNewFruits));
}
}
class Fruits {}
class Apple extends Fruits {}
class Orange extends Fruits {}
class Mango extends Fruits {}
Run Code Online (Sandbox Code Playgroud)
错误

我正在研究 Selenium 框架 2.0 Web 驱动程序,我想从具有 data-automation-id 等属性的范围中获取价值。HTML代码如下
<span data-automation-id="SEL_ERR">
Error Description
</span>
Run Code Online (Sandbox Code Playgroud)
现在我想用 in span 读取文本,该文本的属性data-automation-id设置为 SEL_ERR。
有人可以帮助我修复代码,因为我已经尝试过下面的代码但徒劳无功。
driver.findElement(By.tagName("span")).getAttribute("data-automation-id");
Run Code Online (Sandbox Code Playgroud) String对象中Hash32和Hash的区别是什么
调试时我发现String对象显示hash = 0和hash32 = 0,如下图所示.有些人可以解释它显示的原因.
谢谢回复