public class StringAbout extends Ex{
public int a;
public void a() {
System.out.println("a:oooooooooo");
}
//子空找父空,子有找父有,找不到或父私有就报错
//父里空白,相当于有空参构造,子空能找到父空,写子空不报错
public StringAbout(int a) {
super(a);
System.out.println("子类的有参1");
this.a = a;
System.out.println("子类的有参2");
// TODO Auto-generated constructor stub
}
public StringAbout() {
super();
System.out.println("子类的空参");
// TODO Auto-generated constructor stub
}
============================================================================
package com.bw.yjs.StudyAbout;
public class Ex {
public int a = 2;
public void a() {
System.out.println("a:oo");
}
private int bb() {
return 1;
}
public Ex() {
super();
// TODO Auto-generated constructor stub
System.out.println("父类的空参");
}
public Ex(int a) {
super();
System.out.println("父类的有参1");
this.a = a;
System.out.println("父类的有参2");
}
}
=============================================================
package com.bw.yjs.StudyAbout;
public class Main {
public static void main(String[] args) {
StringAbout sa = new StringAbout();
StringAbout sa1 = new StringAbout(2);
//子类继承父类公有的属性
//子类继承父类公有的方法
//子类与父类同名属性,先执行子类,子类没有用父类
System.out.println("sa:"+sa.a);
//子类与父类同名方法,先执行子类
sa.a();
}
}
======================================================
控制台输出:
父类的空参
子类的空参
父类的有参1
父类的有参2
子类的有参1
子类的有参2
sa:0
a:oooooooooo
}