继承训练

父类中必须有不带参数的构造方法,否则就在子类的构造方法中的第一行加super()显示的继承父类构造方法


package day6;

    class Fu1 
    { 
        int num =9;
      Fu1()
      {
        System.out.println("fu run"+num);
          
      }
      Fu1(int x)
      {
          x=3;
          System.out.println("fu run "+x);
          
      }
      
    }
    class Zi1 extends Fu1
    {

        Zi1() {
             System.out.println("Zi1 runssss");
         
        }
        Zi1(int num)
    {       this();//调用本类参数为空的构造函数
            System.out.println("Zi1 run"+num);
            
        }
        
    }
    public class Test3   {
         
        public static void main(String[] args) {
            
              Fu1 f = new Zi1(6);
              System.out.println();
              Zi1 z = new Zi1(6);
              System.out.println();
              Fu1 f1 = new Zi1();
              System.out.println();
              Zi1 z1 = new Zi1();
            
        }
     
    }

fu run9
Zi1 runssss
Zi1 run6


fu run9
Zi1 runssss
Zi1 run6


fu run9
Zi1 runssss


fu run9
Zi1 runssss

 

如果父类有空参的构造函数,那么如果子类中的构造函数第一行没有super(参数)那么就之访问空参的父类中的构造函数,如果有super(参数)那么就直接访问父类中的带参数的构造函数。

有super就不能有this因为super、this不能在同一行。

 
 
package day6;

    class Fu1 
    { 
        int num =9;
      Fu1()
      {
        System.out.println("fu run"+num);
          
      }
      Fu1(int x)
      {
          x=3;
          System.out.println("fu run "+x);
          
      }
      
    }
    class Zi1 extends Fu1
    {

        Zi1() {
             System.out.println("Zi1 runssss");
         
        }
        Zi1(int num)
    {     //  this();//调用本类参数为空的构造函数
            super(4);
            System.out.println("Zi1 run"+num);
            
        }
        
    }
    public class Test3   {
         
        public static void main(String[] args) {
            
              Fu1 f = new Zi1(6);
              System.out.println();
              Zi1 z = new Zi1(6);
              System.out.println();
              Fu1 f1 = new Zi1();
              System.out.println();
              Zi1 z1 = new Zi1();
            
        }
     
    }

fu run 3
Zi1 run6


fu run 3
Zi1 run6


fu run9
Zi1 runssss


fu run9
Zi1 runssss

super(4)参数为4,运行出来却是3,当函数里面有自己的参数赋值的时候,直接用函数内的参数值,而不是调用者的参数值。

package day6;

    class Fu1 
    { 
        int num =9;
      Fu1()
      {
        this.show();
        System.out.println("fu run"+num);
          
      }
      Fu1(int x)
      {
          x=3;
          System.out.println("fu run "+x);
          
      }
      void show()
      {
          System.out.println("hahahahahahahaha");
          
      }
      
    }    
    class Zi1 extends Fu1
    {

        Zi1() {
            this.show();
             System.out.println("Zi1 runssss");
         
        }
        Zi1(int num)
    {       this();//调用本类参数为空的构造函数
           // super(4);
            System.out.println("Zi1 run"+num);
            
        }
       void show()
       {
           System.out.println("lalalalalalallala");
           
       }
    }
    public class Test3   {
         
        public static void main(String[] args) {
            
              Fu1 f = new Zi1(6);
              System.out.println();
              Zi1 z = new Zi1(6);
              System.out.println();
              Fu1 f1 = new Zi1();
              System.out.println();
              Zi1 z1 = new Zi1();
            
        }
     
    }

lalalalalalallala
fu run9
lalalalalalallala
Zi1 runssss
Zi1 run6


lalalalalalallala
fu run9
lalalalalalallala
Zi1 runssss
Zi1 run6


lalalalalalallala
fu run9
lalalalalalallala
Zi1 runssss


lalalalalalallala
fu run9
lalalalalalallala
Zi1 runssss

 

注意观察父类中的show方法和子类中的show方法,父类中的show方法被覆盖了。

一个对象在内存中的产生过程:

1、将该对象所需要的类文件加载进内存

2、在内存进行空间的方法区的空间分配

3、通过new在堆内存中开辟空间

4、对象中的属性进行默认初始化

5、调用与之对象的构造函数进行舒适化

6、通过构造函数的super调用父类中的构造函数初始化

7、对象中的属性进行初始化

8、构造代码块初始化

9、该构造函数内部自定义内容进行初始化

原文地址:https://www.cnblogs.com/mmlovejj/p/5083754.html