小编Art*_*hur的帖子

覆盖和过载

我的问题是:为什么a4.equals(a2)会导致method1执行不method2a2指向AA并且它是参数.同样的a2.equals(b1).似乎当它没有指向BB(对于所有equals方法所在的类)时,它只会导致method1执行,并且方法获得的参数类型无关紧要.

public class AA
{
    public int getVal()
    {
       return 5;
    }
}

public class BB extends AA
{
    private String _st = "bb";

    public boolean equals(Object ob)  //method1
    {
      System.out.println("Method 1");
        if((ob != null) && (ob instanceof BB))
        {
            if(_st.equals(((BB)ob)._st) && (getVal() == ((BB)ob).getVal()))
                return true;
        }
        return false;
    }


    public boolean equals(AA ob)  //method2
    {
    System.out.println("Method 2");
        if((ob != null) …
Run Code Online (Sandbox Code Playgroud)

java

4
推荐指数
1
解决办法
82
查看次数

while循环内嵌套for循环的时间复杂度

如果 while 循环内有一个嵌套的 for 循环,如下所示:

while(condition)
   for(i=0; i<size; i++)
Run Code Online (Sandbox Code Playgroud)

每次执行 for 循环时,for 循环的大小都会增加,从 1,2,...,n-1 开始,而 while 循环运行 n-1 次。

也就是说时间复杂度是O(n^3)?

time-complexity

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

标签 统计

java ×1

time-complexity ×1