晚上刚看了一个人的博客代码,
public class day01_1 {
public static void main(String[] args) {
Shape sh = new circle();
System.out.println(sh.name);
sh.printType();
sh.printName();
}
}
class Shape{
public String name = "shape";
public Shape(){
System.out.println("shape constructor");
}
public void printType() {
System.out.println("this is shape");
}
public static void printName() {
System.out.println("shape");
}
}
class circle extends Shape{
public String name = "circle";
public circle(){
System.out.println("circle constructor");
}
public void printType() {
System.out.println("this is circle");
}
public static void printName(){
System.out.println("circle");
}
}
(出自「yanghan1222」的原创文章, 链接https://blog.csdn.net/yanghan1222/article/details/8015453)
结果的输出与我想象的并不一样:
shape constructor
circle constructor
shape
this is circle
shape
首先是创建对象时先运行两个构造器,然后是正常输出父类属性,子类方法,但最后一个我并不理解,为什么不是circle?
钻研了20分钟之后我才发现:静态方法不能被覆盖hhh
虽然是相当简单的一道题,但终归是由收获