這個問題在這里已經有了答案: 什么是它,如果我不指定它我的課放在默認包? (3 個回答) 3 小時前關閉。
簡單的問題,我希望
考慮 2 同一檔案夾中的以下兩個檔案:
形狀.java
public abstract interface Shape {
public abstract double area();
public abstract double perimeter();
public default String getName() {
return "default";
}
}
class Square implements Shape {
public static void visible() {
System.out.println("Visible");
}
public double area() {
return 0.00;
}
public double perimeter() {
return 0.00;
}
public String getName() {
return "square";
}
}
class Triangle implements Shape {
public double area() {
return 0.00;
}
public double perimeter() {
return 0.00;
}
public String getName() {
return "triangle";
}
}
App.Java
public class App {
public static void main(String... args) {
Shape square = new Square();
Shape circle = new Triangle();
Square.visible();
System.out.println(square.getName());
System.out.println(circle.getName());
}
}
No packages are declared, and the two shape class implementations are not listed as public packages, so why is this compiling? I would have thought that Square and Triangle classes wouldn't be visible to App. If I label them package A and package b, they can't see each other, as expected. Does the compiler consider 2 files in the same folder with no package declaration to be in the same package by default?
uj5u.com熱心網友回復:
它們都在同一個“默認”包中。無包宣告意味著使用默認的未命名包。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/360118.html
