This is a Java question.

Asked 2 years ago, Updated 2 years ago, 33 views

public class HelloWorld extends A {

    String b = "HelloWorld";
    public static void main(String []args){
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.bb();
    }
}
class A {

    public void bb(){
        System.out.println(b);
    }
}

To print println(b) in class A by referring to String b="HelloWorld" in class HelloWorld How do I modify or write an expression?

java

2022-09-21 18:29

1 Answers

public class HelloWorld extensions A{

    String b = "HelloWorld";
    public static void main(String []args){
        A Class = new A();
        Class.bb();
    }
}
class A {

    public void bb(){
        HelloWorld helloWorld = new HelloWorld();
        System.out.println(helloWorld.b)b);
    }
}

It would be convenient to use static variables, but if you want to generate objects and output them as references, create HelloWorld objects in class A and use HelloWorld, a reference variable created by an object when printingln.Put the value in b. After that, if you create class A as an object in the main and call the bb method, it will be printed. In fact, if you code like this, you don't have to inherit it.


2022-09-21 18:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.