小编Kic*_*ick的帖子

如何调用Parent重写方法

我有两个类 Parent 和 Child。在 Child 类中,我调用父类重写方法(show)。在父类中,我调用另一个方法(display),但由于调用了 Child 方法,该方法也被重写。我想从show方法调用Parent方法display。

public class Parent {


    public void show()
    {
        System.out.println("Show of parent ");
        this.display();
    }

    public void display()
    {
        System.out.println("Display of parent");
    }

}

public class Child extends Parent{

    public void show()
    {
        System.out.println("Show of child ");
        super.show();
    }

    public void display()
    {
        System.out.println("Display of child");
    }

    public static void main(String[] args) {
        Parent obj = new Child();
        obj.show();
    }


}
Run Code Online (Sandbox Code Playgroud)

输出 :

Show of child 
Show of parent 
Display of child
Run Code Online (Sandbox Code Playgroud)

需要 …

java polymorphism inheritance

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

如何+内部工作在JAVA中的字符串

我从博客中读到,当我们使用+运算符时,内部java使用StringBuilder来连接String .我只是检查它,发现了一些奇怪的输出.

public class StringDemo {

    public static void main(String[] args) {

        String a = "Hello World";
        String b = "Hello World";
        String c = "Hello";
        String d =  c + " World".intern();
        String e =  new StringBuilder().append(String.valueOf(c)).append(" World").toString().intern() ;
        String f =  new StringBuilder(String.valueOf(c)).append(" World").toString().intern();

        System.out.println(a == b);   // Line 1 Expected output true
        System.out.println(a == d);   // Line 2 Output is false
        System.out.println(a == e);   // Line 3 Output is true
        System.out.println(a == f);   // Line …
Run Code Online (Sandbox Code Playgroud)

java string

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

线程在调用 stop() 方法时不会停止

我已经创建了 java Thread 的示例程序,其中我使用 stop() 方法来停止使用以下程序的线程

public class App extends Thread
{
    Thread th;

    App(String threadName)
    {
        th = new Thread(threadName);
    }

    public synchronized void run() // Remove synchronized
    {
        for (int i = 0; i < 5; i++) {
            System.out.println(th.getName()+" "+i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }
        }
    }

    public static void main( String[] args ) 
    {
       App thread_1 = new App("Thread-1");
       thread_1.start();
       thread_1.setPriority(MAX_PRIORITY); //Comment this
       thread_1.stop();
       App thread_2 = new App("Thread-2");
       thread_2.start();
    }
}
Run Code Online (Sandbox Code Playgroud)

上面程序的输出是: …

java multithreading

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

标签 统计

java ×3

inheritance ×1

multithreading ×1

polymorphism ×1

string ×1