无法通过Java中的静态方法调用来识别嵌套类的类型。

huangapple go评论73阅读模式
英文:

Not able to identify what type of nested class by static method call in java

问题

通常情况下,我们可以通过查看它们的签名来识别所有的类、方法和数据成员。

示例:Math.sqrt(),System.out.println

Math - 类

sqrt - 静态方法

print/println - 实例方法

但无法识别嵌套类的静态方法。

package so;

class Outer {
    static String outerVar = "ABC"; 
    class InsInner {
        static void show() {
            System.out.println("实例内部 " + outerVar);
        }
    }
    static class StaticInner {
        static void show() {
            System.out.println("静态内部类的静态方法 " + outerVar);
        }
    }
}
public class NestedClass {
    public static void main(String[] args) {
        Outer.InsInner.show();
        Outer.StaticInner.show();
    }
}

为什么静态和非静态类具有相同的约定?

Outer.InsInner.show();
Outer.StaticInner.show();
英文:

Usually, we can identify all the classes, methods & data members by seeing their signatures.

Ex: Math.sqrt(), System.out.println

Math - class

sqrt - static method

print/println - instance method

But Not able to identify static methods of nested classes.

package so;

class Outer {
	static String outerVar = "ABC"; 
	class InsInner {
		static void show() {
			System.out.println("Instance inner "+outerVar);
		}
	}
	static class StaticInner {
		static void show() {
			System.out.println("Static inner class static method "+outerVar);
		}
	}
}
public class NestedClass {
	public static void main(String[] args) {
		Outer.InsInner.show();
		Outer.StaticInner.show();
	}
}

Why both static and non static classes have same convention?

Outer.InsInner.show();
Outer.StaticInner.show();

答案1

得分: 1

因为show()在这两个地方都是static。当一个方法是static时,它不需要实例来操作,所以你不需要一个InsInnerStaticInner来调用show(),调用看起来是一样的。如果你把show()变成实例方法,那么它就会有所不同。像这样,

class Outer {
    static String outerVar = "ABC";

    class InsInner {
        void show() {
            System.out.println("Instance inner " + outerVar);
        }
    }

    static class StaticInner {
        void show() {
            System.out.println("Static inner class static method " + outerVar);
        }
    }

    public static void main(String[] args) {
        new Outer().new InsInner().show();
        new Outer.StaticInner().show();
    }
}
英文:

Because show() is static in both places. When a method is static it does not require an instance to operate, so you don't need an InsInner or StaticInner to call either show() and the calls look the same. If you make show() an instance method, then it would have to differ. Like,

class Outer {
	static String outerVar = "ABC";

	class InsInner {
		void show() {
			System.out.println("Instance inner " + outerVar);
		}
	}

	static class StaticInner {
		void show() {
			System.out.println("Static inner class static method " + outerVar);
		}
	}

	public static void main(String[] args) {
		new Outer().new InsInner().show();
		new Outer.StaticInner().show();
	}
}

huangapple
  • 本文由 发表于 2023年5月28日 19:52:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定