继承学习笔记
public class Fruits {
private String name;
private String color;
private String shape;
public Fruits() {
super();
System.out.println("水果父类空参构造。。。");
}
public Fruits(String name, String color, String shape) {
super();
this.name = name;
this.color = color;
this.shape = shape;
System.out.println("父类水果有参构造。。。");
}
public void grow() {
System.out.println("父类水果成长。。。");
}
public static void smell() {
System.out.println("父类水果散发香味。。。");
}
private void privateMethod() {
System.out.println("父类水果私有方法。。。");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getShape() {
return shape;
}
public void setShape(String shape) {
this.shape = shape;
}
@Override
public String toString() {
return "Fruits [name=" + name + ", color=" + color + ", shape=" + shape + "]";
}
}
public class Apple extends Fruits {
private String name;
private String color;
private String shape;
public Apple() {
super();
System.out.println("水果子类空参构造。。。");
}
public Apple(String name, String color, String shape) {
super(name, color, shape);
this.name = name;
this.color = color;
this.shape = shape;
System.out.println("子类水果有参构造。。。");
}
public void grow() {
super.grow();
System.out.println("子类水果成长。。。");
}
public static void smell() {
System.out.println("子类水果散发香味。。。");
}
private void privateMethod() {
System.out.println("子类水果私有方法。。。");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getShape() {
return shape;
}
public void setShape(String shape) {
this.shape = shape;
}
@Override
public String toString() {
return "Apple [name=" + name + ", color=" + color + ", shape=" + shape + "]";
}
public static void main(String[] args) {
Fruits f = new Fruits("香蕉", "黄色", "长的");
Fruits a = new Apple();
Fruits a1 = new Apple("苹果", "红色", "圆形");
System.out.println(a1);
a.grow();
a1.grow();
a.smell();
f.smell();
}
}
运行结果:
父类静态方法不能被子类静态方法覆盖