小编Ade*_*lin的帖子

原型和继承

最近我开始学习Javascript,因为我来自Java世界.我得到了这本名为JavaScript The Definitive guide的..我现在对Prototypes和Inheritance有点困惑.我想知道将函数设置为对象属性和函数原型之间有什么区别.从书中的例子:

function Rectangle(w, h) {
this.width = w;
this.height = h;
this.area = function( ) { return this.width * this.height; }
}
Run Code Online (Sandbox Code Playgroud)

使用这个新版本的构造函数,您可以编写如下代码:

// How big is a sheet of U.S. Letter paper in square inches?
var r = new Rectangle(8.5, 11);
var a = r.area( );
Run Code Online (Sandbox Code Playgroud)

此解决方案效果更好,但仍然不是最佳的(为什么).创建的每个矩形都有三个属性(是的,那么什么?).每个矩形的宽度和高度属性可能不同,但每个Rectangle对象的区域总是引用相同的函数(当然,有人可能会更改它,但您通常希望对象的方法保持不变).对于打算由同一个类的所有对象共享的方法使用常规属性是低效的(为什么问题是什么?)(即,使用相同构造函数创建的所有对象).

javascript

0
推荐指数
1
解决办法
94
查看次数

将String二进制转换为整数Java

在阅读本书时,我遇到了将二进制转换为整数的问题.这本书给出的代码是:

 // convert a String of 0's and 1's into an integer
    public static int fromBinaryString(String s) {
       int result = 0;
       for (int i = 0; i < s.length(); i++) {
          char c = s.charAt(i);
          if      (c == '0') result = 2 * result;
          else if (c == '1') result = 2 * result + 1;
       }
       return result;
    }
Run Code Online (Sandbox Code Playgroud)

我解决问题的方法是:

public static int fromBinary(String s) {
        int result = 0;
        int powerOfTwo = 0;
        for (int …
Run Code Online (Sandbox Code Playgroud)

java algorithm

0
推荐指数
1
解决办法
207
查看次数

取消标记为删除Kafka的主题?

我使用此命令标记了3个要删除的主题 kafka-topics --zookeeper localhost:2181 --delete --topic topic_name

现在我不能使用它们我也不能重新创建它们如何解决这个问题?要么完全删除它们,要么重新创建它们或取消标记要删除的状态.

apache-kafka

0
推荐指数
1
解决办法
4076
查看次数

使用 Mockito 根据调用时间更改模拟对象行为?

我正在模拟一个用于向远程Http服务提交一些对象的接口,逻辑如下:如果提交成功,则尝试提交对象5次,然后继续下一个,否则尝试直到达到5次,如果仍然失败,则丢弃失败。

interface EmployeeEndPoint {

    Response submit(Employee employee);

}

class Response {

    String status;

    public Response(String status) {
        this.status = status;
    }
}


class SomeService {

    private EmployeeEndPoint employeeEndPoint;


    void submit(Employee employee) {

        Response response = employeeEndPoint.submit(employee);

        if(response.status=="ERROR"){
            //put this employee in a queue and then retry 5 more time if the call succeeds then skip otherwise keep trying until the 5th.
        }
    }


}

@Mock
EmployeeEndPoint employeeEndPoint;


@Test
public void shouldStopTryingSubmittingEmployeeWhenResponseReturnsSuccessValue() {
    //I want the first

    Employee employee
             = new …
Run Code Online (Sandbox Code Playgroud)

java unit-testing mocking mockito

0
推荐指数
1
解决办法
4191
查看次数