我想知道为什么List<Number>不能用List<Integer> 来调用它甚至整数是抽象类Number的扩展类?有一个逻辑错误,因为我可以使用参数Number和Integer调用Method.
public class Que
{
public void enterNumbers(List<Number> nummern)
{
for (Number number : nummern)
{
System.out.println(number + "\n");
}
}
public void enterNum(Number num)
{
System.out.println("This is a number " + num);
}
public static void main(String[] args)
{
Que que = new Que();
Integer myInteger = new Integer(7);
// possible (of course!)
que.enterNum(myInteger);
List<Integer> num = new ArrayList<Integer>();
num.add(4);
num.add(45);
Integer inte = new Integer(333);
num.add(inte);
// not possible !
que.enterNumbers(num);
}
} …Run Code Online (Sandbox Code Playgroud) 我的问题是我是否使用 StringBuffer(或 StringBuilder),以及我是否多次在实例上调用 toString 方法。StringBuffer 每次都会返回新的 String 实例还是从 String 池中返回 String?(假设我在两次调用之间没有对 StringBuffer 进行任何更改)
以下类型的实例化有什么区别吗?我是直接在定义变量的地方设置值,还是在类构造函数中设置值。
如果不是,最佳实践是什么?
“在班上”:
class A {
boolean b = true;
public A(){
}
}
Run Code Online (Sandbox Code Playgroud)
“在构造函数中”:
class B {
boolean b;
public B(){
b = true;
}
}
Run Code Online (Sandbox Code Playgroud)
变量类型仅用于示例。我看到的唯一区别是,当属性是复杂类型(类)时,其构造函数取决于为包含类的构造函数提供的值:
class A {
B b;
public A(String s){
b = new B(s);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试检查我的String键是否在NETFLIX列中.
public boolean checkSerial(String key){
boolean isValid = false;
sql = "Select * from KEYS WHERE NETFLIX=?";
try{
ps = con.prepareStatement(sql);
ps.setString(1, key);
rs = ps.executeQuery();
if(rs.next())
isValid = true;
}catch(SQLException e){
System.out.println(e);
}
return isValid;
}
Run Code Online (Sandbox Code Playgroud)
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误; 检查与您的MariaDB服务器版本对应的手册,以便在第1行'KEYS WHERE NETFLIX ='IPMAN''附近使用正确的语法
我知道开闭原则意味着对扩展开放,对修改封闭。考虑如下示例
public class Vehicle{
public void service(){
//vehicle servicing code
}
}
public class Bike extends Vehicle{
public void service(){
// bike specific servicing
}
}
Run Code Online (Sandbox Code Playgroud)
现在我明白Bike该类Vehicle使用开放封闭原则扩展并添加了新功能。
考虑我创建Vehicle类的jar 文件,然后类从 jarBike扩展Vehicle类。在这种情况下,我们不能修改Vehicle类并Bike扩展它。这是开闭原则的一个很好的例子吗?我想知道 OCP 与继承有何不同
polymorphism inheritance abstraction open-closed-principle solid-principles
我知道有很多问题,并且答案很好.问题是所有这些问题都使用MongoDBObject,MongoDBList来检索数组.我的问题是我正在使用http://api.mongodb.org/java/3.0/index.html?overview-summary.html api,我很难检索数组并向其中添加元素.我必须使用MongoCollection,MongoDatabase和MongoClient.我正试图解决mongodb课程的作业.问题陈述是找到一个数组并在mongod中更新它.
这是我尝试过的
Document post = null; Bson filter = new Document("permalink",
permalink); Bson projection = new Document("comments", true);
List<Document> comments = postsCollection.find(filter)
.projection(projection).into(new ArrayList<Document>());
System.out.println(comments);
post = postsCollection.find(Filters.eq("permalink",
permalink)).first();
Document newComment = new Document();
newComment.append("author", name); newComment.append("body", body);
if (email != null && (!email.equals(""))) {
newComment.append("email", email); }
comments.add(newComment);
Bson filter2 = new Document("_id", post.get("_id"));
System.out.println(comments); post =
postsCollection.find(filter).first();
postsCollection.updateOne(filter2, new Document("$unset",new
Document("comments",true))); postsCollection.updateOne(filter2, new
Document("$set", new Document( "comments", comments)));
Run Code Online (Sandbox Code Playgroud)
这不会创建新评论.相反,它在comments数组本身中创建了另一个注释数组.数组应该在学生中更新
这是json数据 …
嗨,我想根据数字排序目录列表.我有目录名称11-20,1-5,6-10,21-30等等.现在我想根据它中的数字对它们进行排序,以便1到N目录按顺序排列1-5,6-10,11-20,21-30.我使用以下代码,但它无法正常工作.
File[] dirList = mainDir.listFiles();
Arrays.sort(dirList);
Run Code Online (Sandbox Code Playgroud)
我是Java新文件和目录操作请提前帮助感谢.
我是一名新的java学生,我在命令提示符下运行hello world程序,但是我收到了一个错误
class hello{
public static void main(String agrs[]){
system.out.println("Hello world");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的hello world program
G:\java>javac hello.java
G:\java>dir
Volume in drive G has no label.
Volume Serial Number is 32DF-BA6B
Directory of G:\java
14-Sep-13 04:36 PM <DIR> .
14-Sep-13 04:36 PM <DIR> ..
14-Sep-13 04:36 PM 415 hello.class
14-Sep-13 04:35 PM 100 hello.java
2 File(s) 515 bytes
2 Dir(s) 55,645,966,336 bytes free
G:\java>java hello
Error: Could not find or load main class hello
Run Code Online (Sandbox Code Playgroud)
我的java路径是对的
G:\java>path
PATH=G:\Windows\system32;G:\Windows;G:\Windows\System32\Wbem;G:\Windows\System32
\WindowsPowerShell\v1.0\;G:\Program Files\Java\jdk1.7.0_25\bin
Run Code Online (Sandbox Code Playgroud)
但是什么时候使用这个命令然后程序运行. …
我们通常在java类中创建方法,将它们导入jsp文件并在我们的jsp文件中调用这些方法.
但我们在客户端环境中工作,我们无权创建或修改.java文件.因此,我们迫切需要在jsp文件中创建一个函数,并从另一个jsp文件中调用它.
例如:
A.jsp
.....
<jsp:include page="B.jsp"/>
....
<%= getName(); %>
Run Code Online (Sandbox Code Playgroud)
B.jsp ....
<%!
public String getName()
{
return "Hello";
}
>%
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
xml文件位于WebContent/WEB-INF/web.xml我的项目中.我正在使用Eclipse并运行Tomcat(它不是通过Eclipse安装的.我更喜欢它是一个单独的安装).
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>EmployeeManagement</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>name</param-name>
<param-value>Pramod</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
当表单页面提交给servlet时它不起作用.我每次都会收到404错误.我一直遇到这个问题.有人请帮帮我.
java ×9
abstraction ×1
eclipse ×1
generics ×1
inheritance ×1
java-ee ×1
jsp ×1
mongodb ×1
mongodb-java ×1
polymorphism ×1
servlets ×1
string ×1
stringbuffer ×1
tomcat ×1
xml ×1