不能。不能重写
构造函数的重载:
public class Pen {
public String color;
public int price;
public String name;
public Pen(String color,int price,String name) {
this.color=color;
this.name=name;
this.price=price;
}
public Pen(String name) {
this.name=name;
this.color="黑色";
this.price=2;
}
public Pen() {
this.name="圆珠笔";
this.price=1;
this.color="蓝色";
}
public void write(String s) {
System.out.println(price+"元"+color+name+"写"+s);
}
public void draw(String s) {
System.out.println(price+"元"+color+name+"画"+s);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Pen p1 =new Pen("红色",10,"彩笔");
p1.draw("山水画");
Pen p2 =new Pen("铅笔");
p2.write("古诗");
Pen p3 = new Pen();
p3.write("作业");
}
}
//创建笔Pen类,包含颜色,价格,名称,写字、画画方法。
//创建三个构造函数,分别是无参,一个参数,三个参数。
//在测试类中创建10元红色彩笔画山水画,
//2元黑色铅笔写古诗。
构造函数不能被继承,不能被继承就谈不上重写。
构造方法语法:
访问权限修饰符 类名(参数类型 参数名){}
构造方法创建对象时调用,目的是给属性赋初始值