可以在同一个包中通过类名调用方法而无需创建类的对象或在java中无需继承
public class BoxWeight /*extends Box*/{
public static void main(String[] args) {
/*BoxWeight myCat = new BoxWeight();*/
/*Box myAnimal = myCat;*/
Box.testClassMethod();<------------ why this possible
/* myAnimal.testInstanceMethod();*/
}
}
public class Box /*extends Bicycle*/{
public static void testClassMethod() {
System.out.println("The class" + " method in Box.");
}
public void testInstanceMethod() {
System.out.println("The instance " + " method in Box.");
}
}
我的问题不是这个,因为你似乎我的问题是这个“可以在同一个包中通过类名调用一个方法,而无需创建类的对象或在 java 中没有继承”,但我已经解决了这个问题,我想确认这是否可能