简体   繁体   English

为什么我们不能在扩展类的静态方法中使用此实例?

[英]Why can't we use this instance in the static method of extended class?

Base class 基类

class Basics415 {   

    Basics1 b1 = new Basics1();

    public static void main_hooo(){
        out.println("1234");     
    }

    void main_ho(){

    }

}

Extended class 扩展类

public class Basics5 extends Basics415{     

    public static void main(String[] args){

        this.main_hooo();  // this line throws error.
    }

}

Why we aren't able to use this instance inside a static method of the extended class? 为什么我们不能在扩展类的静态方法中使用此实例?

Your main method is static, which means there is no instance of Basic associated with it so this won't work. 您的main方法是静态的,这意味着没有与之关联的Basic实例,因此this不起作用。 To access the static methods of Basics415 , you should refer to them explicitly like this: 要访问Basics415的静态方法,您应该像这样明确地引用它们:

public class Basics5 extends Basics415{
    public static void main(){
        Basics415.main_hooo();
    }
}

You could also just do this since Basic5 extends Basic415 . 由于Basic5扩展了Basic415您也可以这样做。 Both are acceptable, but your org may have their own style guidelines: 两者都可以接受,但是您的组织可能有自己的样式准则:

public class Basics5 extends Basics415{
    public static void main(){
        main_hooo();
    }
}

That is not the correct way to access a static method. 那不是访问静态方法的正确方法。 this is not static. this不是静态的。 Instead do Basic5.main_hooo() 取而代之的是Basic5.main_hooo()

this和static彼此相反,就这么简单。如果要使用它,请在非静态方法中使用它。

The main() method must be defined with a String[] parameter if you want it to execute first. 如果要首先执行main()方法,则必须使用String []参数定义它。 Also, main() is a static method (it belongs to the class). 而且,main()是静态方法(它属于该类)。 There is no instance (this) to refer to. 没有要引用的实例。

Use the class name to call it within main: 使用类名称在main中调用它:

Basics415.main_hooo();  // this line no longer throws an error.

For static method not require create an object, 对于不需要创建对象的静态方法,

    public static void main(String[] args) {
        Basics415 b = new Basics415();
        b.main_hooo();// output : 1234
        main_hooo();// output : 1234
        Basics415.main_hooo();// output : 1234
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我们不能用私有扩展类方法覆盖基类方法? - Why can’t we override a base class method with private extended class method? 为什么我们不能在静态方法中使用“this”关键字 - Why can't we use 'this' keyword in a static method 为什么我们不能在带有ehcache的spring中使用带有静态方法的@Cacheable? - Why can't we use @Cacheable with static method in spring with ehcache? 我们可以在抽象类中使用静态方法吗? - Can we use static method in an abstract class? 为什么我们可以使用 'this' 作为实例方法参数? - Why can we use 'this' as an instance method parameter? 为什么我不能在Java中实例化扩展类的实例? - Why can't I instantiate an instance of an extended class in Java? 我们可以使用反射来获取类的静态成员而无需在对象实例上调用该方法吗? - Can we use reflection to get a static member of a class without invoking that method on an object instance? 为什么我们不能在(非静态)内部类(Java 16 之前)中使用静态方法? - Why can't we have static method in a (non-static) inner class (pre-Java 16)? 我们无法从静态方法访问非静态实例,但可以启动类。 怎么样? - We cant access non static instance from a static method, but can initiate a class. how? 为什么我们不能创建Collections类的实例(不是Collection Interface)? - Why can't we create instance of Collections class (not Collection Interface)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM