package xue;
//方法重写 继承后,子类拥有父类的功能,也可以用自己的功能 当子类中的方法声明(返回值 方法名 参数列表)与父类完全一致并且 修饰符的优先级大于等于父类的修饰符优先级时,可以对
//父类的方法进行覆盖/重写(不改变父类的方法或变量,而是另外开一块空间存储)
//此外 父类的私有方法不能被重写
//在工作中,严禁直接修改源代码,所以要对其进行继承,然后进行功能拓展,即重写
//拓展过程:先发生继承关系,然后发生方法重写的现象
public class fangfachongxie {
public static void main(String[] args) {
// TODO Auto-generated method stub
son s =new son(); //结果 父类的含参构造方法
s.eat(); //重写父类方法后,显示为 喝汤
s.code();
}
}
class father {
int c=66;
public father(int a) { //含参构造方法
System.out.println(“父类的含参构造方法”);
}
private void friend() { //私有方法 不能被重写
System.out.println(“私有朋友”);
}
public void eat() {
System.out.println(“吃肉”);
}
}
class son extends father{
int c =55;
public son() {
super(8);
}
public void eat() {
System.out.println("喝汤"); //重写父类方法
}
public void code() { //子类也可以有自己特有的方法
int c =53;
System.out.println("儿子打代码"+super.c); //调用父类成员变量 66
System.out.println(this.c); //55 子类可以有自己的成员变量
System.out.println(c); //就近原则 53
}
}