继承
主函数
package heima;
public class Demo {
public static void main(String[] args) {
fu f=new fu();
f.show();
zi z=new zi();
z.method();
z.show();
}
}
子类//子类不仅有自己的特点还继承了父类的特点
package heima;
public class zi extends fu{
public void method(){
System.out.println("method被调用");
}
}
父类
package heima;
public class fu {
public void show(){
System.out.println("show方法被调用");
}
}
super
//super调用父类的方法
主函数
package heima;
public class Demo {
public static void main(String[] args) {
zi z=new zi();
z.show();
}
}
父类
package heima;
public class Demo {
public static void main(String[] args) {
zi z=new zi();
z.show();
}
}
子类
package heima;
public class Demo {
public static void main(String[] args) {
zi z=new zi();
z.show();
}
}
package heima;
public class Demo {
public static void main(String[] args) {
zi z=new zi();
zi z2=new zi(11);
}
}
package heima;
public class fu {
public fu(){
System.out.println("fu中无参构造被调用");
}
public fu(int age){
System.out.println("fu中有参构造被调用");
}
}
package heima;
public class zi extends fu{
public zi(){
//super();//写不写都一样
super(22);
System.out.println("Zi中无参构造方法被调用");
}
public zi(int age){
//super();写不写都一样
super(11);
System.out.println("Zi中带参构造方法被调用");
}
}