继承1st
程序员文章站
2024-02-01 21:34:16
...
代码1
package FirstPackage;
public class Extend1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Zi a=new Zi();
a.show();
}
}
class Fu{
public int num = 10;
public Fu(){
System.out.println("fu");
}
}
class Zi extends Fu{
public int num = 20;
public Zi(){
System.out.println("zi");
}
public void show(){
int num = 30;
System.out.println(num);
System.out.println(this.num);
System.out.println(super.num);
}
}
输出:
fu
zi
30
20
10
- 第一父类,第二子类
- 局部,this,super
代码2
package FirstPackage;
public class Extend2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Zi a=new Zi();
}
}
class Fu {
static {
System.out.println("静态代码块Fu");
}
{
System.out.println("构造代码块Fu");
}
public Fu() {
System.out.println("构造方法Fu");
}
}
class Zi extends Fu {
static {
System.out.println("静态代码块Zi");
}
{
System.out.println("构造代码块Zi");
}
public Zi() {
System.out.println("构造方法Zi");
}
}
输出:
静态代码块Fu
静态代码块Zi
构造代码块Fu
构造方法Fu
构造代码块Zi
构造方法Zi
- 第一父类,第二子类
- 先静态,后其他
上一篇: 习题2-2 阶梯电价 (15分)