小编Rah*_*ran的帖子

如果无法覆盖静态方法,那么它的工作原理如何(For Java)?

我的理解是静态变量和静态方法属于类,而不是类对象.因此Override,静态方法在Java中不起作用,因为重写我们需要创建一个类的实例.但我今天尝试的东西与我的Java知识相矛盾.

请遵循以下代码:

class Parent{
   public static void doIt(){
      System.out.println("In static method 'doit' of class Parent ");
   }
}

class Child extends Parent{    
   public static void doIt(){
      System.out.println("In static method 'doit' of class Child ");
   }
}

public class StaticPractise{
   public static void main(String[] args){
      Parent.doIt();
      Child.doIt();
   }    
}
Run Code Online (Sandbox Code Playgroud)

上述实现的输出是:

D:\Rahul Shivsharan\MyPractise\JAVA>java StaticPractise
In static method 'doit' of class Parent
In static method 'doit' of class Child
Run Code Online (Sandbox Code Playgroud)

从这个输出中我可以理解,尽管Child类扩展了Parent类,但这些doit方法对于每个类都是个体的static.所以不允许覆盖它们.

现在请按照下面的代码,@Override添加到孩子的 …

java overriding

38
推荐指数
2
解决办法
5862
查看次数

如何使用"<%= request.getContextPath()%>"比"../"更好

我参与了许多J2EE项目,其中视图层是JSP.在大多数项目中,我已经看到我们使用scriptlet中的contextPath引用外部资源,即images,javascript,jsp,css等.

代码如下,

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>GC Demo Using HandlebarsJS</title>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/jqueryUI-AutoComplete/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/jqueryUI-AutoComplete/jquery-ui-1.10.3.custom.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/handlebarsJS/handlebars.js"></script>
    <link rel="stylesheet" type="text/css" href="${pageContext.servletContext.contextPath}/js/jqueryUI-AutoComplete/jquery-ui-1.10.3.custom.css">
Run Code Online (Sandbox Code Playgroud)

从上面的jsp中,我在这里导入外部资源,这些资源位于我的同一个项目包中,即在我的战争中.

现在上面的JSP可以写成如下代码,

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>GC Demo Using HandlebarsJS</title>
    <script type="text/javascript" src="../js/jqueryUI-AutoComplete/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="../js/jqueryUI-AutoComplete/jquery-ui-1.10.3.custom.js"></script>
    <script type="text/javascript" src="../js/handlebarsJS/handlebars.js"></script>
    <link rel="stylesheet" type="text/css" href="../js/jqueryUI-AutoComplete/jquery-ui-1.10.3.custom.css">
Run Code Online (Sandbox Code Playgroud)

在第二个例子中,我也引用了战争中的资源.

现在考虑上述两种情况,第一种情况作为最佳实践被赋予更多意义.

为什么?

使用第二种情况有什么缺点?

使用第二种情况,我们的项目是否与contextpath更紧密地结合在一起?

请向我解释.

jsp contextpath

25
推荐指数
1
解决办法
11万
查看次数

Angular.js中的搜索过滤器,

我是这个框架的新手,因此练习Angularjs并遵循网站上提供的教程.

有一个例子我们可以搜索表中的数据,例子如下,

<!DOCTYPE html>
<html ng-app="SmartPhoneApp">
<head>
    <title>Smart phone Angular</title>      
    <script type="text/javascript" src="D:/Rahul Shivsharan/Installers/AngularJS/angular.js"></script>
    <script type="text/javascript">
        var smartPhoneApp = angular.module("SmartPhoneApp",[]);

        smartPhoneApp.controller("phoneCtrl",function($scope){
            $scope.phones = [
                {
                    "modelName" : "LUMIA 520",
                    "companyName" : "NOKIA"
                },
                {
                    "modelName" : "GALAXY S Series",
                    "companyName" : "SAMSUNG"
                },
                {
                    "modelName" : "CANVAS",
                    "companyName" : "MICROMAX"
                },
                {
                    "modelName" : "OPTIMUS",
                    "companyName" : "LG"                        
                }
            ];

        });
    </script>   
</head>
<body>  
    Search by Model Name : <input ng-model="comp.modelName" />  
    Search by Company : <input ng-model="comp.companyName" />   
    <div …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs single-page-application

10
推荐指数
1
解决办法
9万
查看次数

为什么以及何时使用process.nextTick?

以下是"practise01.js"文件中的代码,

function fn(name){
   return f;

   function f(){
       var n = name;
       console.log("Next TICK "+n+", ");        
   }
}

function myTimeout(time,msg){
   setTimeout(function(){
       console.log("TIMEOUT "+msg);
   },time); 
}

process.nextTick(fn("ONE"));
myTimeout(500,"AFTER-ONE");
process.nextTick(fn("TWO"));
myTimeout(500,"AFTER-TWO");
process.nextTick(fn("THREE"));
myTimeout(500,"AFTER-THREE");
process.nextTick(fn("FOUR"));
Run Code Online (Sandbox Code Playgroud)

运行上面代码的输出是

rahul@rahul:~/myPractise/PlainNodeJSPractise01/Process$ node practise01.js                  

Next TICK ONE, 
Next TICK TWO, 
Next TICK THREE, 
Next TICK FOUR, 
TIMEOUT AFTER-ONE
TIMEOUT AFTER-TWO
TIMEOUT AFTER-THREE
Run Code Online (Sandbox Code Playgroud)

现在我在"practise02.js"中不使用process.nextTick编写代码,如下所示,

function myTimeout(time,msg){
  setTimeout(function(){
        console.log("TIMEOUT "+msg);
  },time);  
}

function fn(name){
  return f;

  function f(){
    var n = name;
    console.log("Next TICK "+n+", ");       
  }
}

fn("ONE")();
myTimeout(500,"AFTER-ONE");
fn("TWO")(); …
Run Code Online (Sandbox Code Playgroud)

javascript node.js

9
推荐指数
3
解决办法
1万
查看次数

@ElementCollection @CollectionTable在一对多映射中

我在使用嵌入式注释的JPA中尝试关系,但我无法成功运行它,

这里我的数据库sql脚本如下,

create table TBL_COLLEGE(
   id integer primary key generated always as identity (start with 1000, increment by 5),
   name varchar(50)
)

create table TBL_COURSE(
   Id  integer primary key generated always as identity (start with 10, increment by 1),
   college_Id integer references TBL_COLLEGE,
   name varchar(50)
)
Run Code Online (Sandbox Code Playgroud)

以下是JPA的代码,

@Embeddable
public class Course {
...
...
..
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="ID")
private Integer courseId;

@Column(name="NAME")
private String courseName;

@Column(name="COLLEGE_ID")
private Integer collegeId;
....
// getter and setter
}
Run Code Online (Sandbox Code Playgroud)

这是学院的地图,

@Entity
@Table(name="TBL_COLLEGE")
public …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa jpa-2.0

8
推荐指数
1
解决办法
3万
查看次数

使用@IdClass存储具有复合主键的实体,但无法持久化

我的id类如下,

public class EmployeeId implements Serializable{
    public EmployeeId(){}
    public EmployeeId(Integer id, String country){
        this.id = id;
        this.country = country;
    }

    private Integer id;
    private String country;

    @Override
    public int hashCode(){        
        return this.getCountry().hashCode() + getId();
    }

    @Override
    public boolean equals(Object o){
        boolean flag = false;
        EmployeeId myId = (EmployeeId) o;

        if((o instanceof EmployeeId) 
                && (this.getCountry().equals(myId.getCountry()))
                && (this.id == myId.getId())){
            flag = true;
        }
        return flag;
    }
// rest of the code with getters only
}
Run Code Online (Sandbox Code Playgroud)

以下是我的实体使用

@Entity
@IdClass(EmployeeId.class)
@Table(name="TBL_EMPLOYEE_FOUR")
public class …
Run Code Online (Sandbox Code Playgroud)

java jpa jpa-2.0

8
推荐指数
1
解决办法
2万
查看次数

为什么[array] .concat()和[array] .concat.apply()提供不同的输出?

下面的代码,

console.log([].concat.apply([2],[[99],5,6,[2,3]]));
Run Code Online (Sandbox Code Playgroud)

输出

[ 2, 99, 5, 6, 2, 3 ]
Run Code Online (Sandbox Code Playgroud)

还有下面的代码

console.log([2].concat([99]).concat([5,6,[2,3]]));
Run Code Online (Sandbox Code Playgroud)

输出

[ 2, 99, 5, 6, [ 2, 3 ] ]
Run Code Online (Sandbox Code Playgroud)

我的假设是

console.log([].concat.apply([2],[[99],5,6,[2,3]]));
Run Code Online (Sandbox Code Playgroud)

应该

[2,[99],5,6,[2,3]]
Run Code Online (Sandbox Code Playgroud)

但是,为什么呢?

javascript

7
推荐指数
2
解决办法
2980
查看次数

JPA,多对多关系,删除所有以前的关系并输入新关系

在这里,我正在尝试在JPA中建立多对多关系,我创建了“ tblcourse”和“ tblStudent”表格,一个学生可以注册许多课程,

create table tblcourse(
    id integer primary key,
    name varchar(100),
    duration integer
);

create table tblcourseStudent(
    studentid integer references tblstudent(studentId),
    courseId integer references tblcourse(id),
    constraint pk_composit_cs primary key(studentid,courseId)
)

Create table tblStudent(
    studentId integer primary key,
    ……..
    ….
);
Run Code Online (Sandbox Code Playgroud)

上述关系的JPA表示法如下,这是StudentEntity.java的代码,

@Entity
@Table(name="TBLSTUDENT")
public class StudentEntity implements Serializable{

private static final long serialVersionUID = 100034222342L;

@Id
@Column(name="STUDENTID")
private Integer studentId;

@Column(name="STUDENTNAME")
private String studentName;

@Column(name="CONTACTNO")
private String contactNumber;

@Embedded
private StudentAddress address;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="DEPTID")
private DeptEntity deptEntity;

@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name="tblcourseStudent", …
Run Code Online (Sandbox Code Playgroud)

java jpa java-ee ejb-3.0 jpa-2.0

5
推荐指数
1
解决办法
4978
查看次数

在未排序的数组中找到第 N 个最高的数字

今天在一次采访中,我被告知要编写一个程序,该程序将输出未排序数组中的第 n 个最高数字,

我用javascript解决了这个问题,程序如下,

var fn50 = function(){
    var reverseSort = function(myArray,highest){
        var x = 0,
            y = 0,
            z = 0,
            temp = 0,
            totalNum = myArray.length, // total numbers in array
            flag = false, // is the numbers sorted in reverse while iteration
            isAchieved = false; // whether we achieved the nth highest

        while(x < totalNum){
            y = x + 1; // start comparing 'yth' number which is next to 'xth' number.

            if(y < totalNum){
                // start comparing …
Run Code Online (Sandbox Code Playgroud)

javascript arrays sorting

5
推荐指数
1
解决办法
6831
查看次数

将两个排序的数组合并为一个

嗨,有人问我以下问题。

给定两个数组,即array1和array2。他们两个都包含数字的排序顺序。

Array1还包含-1,例如;array2中的数字与array1中的-1一样多。

示例如下,

array1 = [-1,-1,-1,-1,56,78,90,1200];
array2 = [1,4,5,1000]
Run Code Online (Sandbox Code Playgroud)

我需要编写一个程序,将上述数组合并为一个,该程序将按排序顺序包含两个数组中的数字,但-1除外。

这是我的代码如下,

 puzzle04([3,6,-1,11,15,-1,23,34,-1,42],[1,12,28]);
 puzzle04([3,6,-1,11,15,-1,23,34,-1,42],[7,19,38]);
 puzzle04([3,6,11,15,32,34,42,-1,-1,-1,-1],[1,10,17,56]);
 puzzle04([-1,-1,-1,-1,3,6,11,15,32,34,42],[1,10,17,56]);
 puzzle04([-1,-1,-1,-1,3,6,11,15,32,34,42],[56,78,90,100]);
 puzzle04([12,34,65,-1,71,85,90,-1,101,120,-1,200],[24,37,94]);
 puzzle04([3,6,-1,11,15,-1,32,34,-1,42,-1],[1,10,17,56]);
 puzzle04([-1,-1,-1,56,78,90,112],[1,4,5]);
 puzzle04([-1,-1,-1,-1,56,78,90,112],[1,4,5,1000]);
 puzzle04([-1,-1,-1,-1,56,78,90,1200],[1,4,5,1000]); 

 function puzzle04(array1,array2){

    var outputArray = [],
        array1Counter = 0, // counter for array1
        array2Counter = 0, // counter for array2
        isArray2NumPlaced = false, // has number from array2 found its position in output array ?       
        areAllArray2NumsFilled = false; // is number pushed in output array

    // iterating through array2 loop    
    for(array2Counter = 0; array2Counter < array2.length; array2Counter++){

        // …
Run Code Online (Sandbox Code Playgroud)

javascript arrays sorting algorithm

5
推荐指数
0
解决办法
2356
查看次数