Use the keyword super in the constructor method, super operation hidden member variables and methods

Keyword super in the constructor mean:

1, when an object is created with the constructor of a subclass, the subclass constructor is always the first to call a constructor of the parent class;

2, when the constructor of a subclass does not clearly indicate which parent class constructor is used, then it is a subclass of the default constructor calls the parent class without parameters;

3. Note: When a class defines one or more configuration method, then the Java system can not provide a default constructor parameter of the free;

The following code ↗:

When constructing subclass of no Super (); keyword, the default method is Super (), this time is called the parent class constructor with no arguments;

When the constructor has subclasses Super (); keyword, the system invokes a constructor parameter corresponding parent class;

//父类
public class superKeyword {
	//父类的成员变量
	int numberFa;
	//无参数构造方法
	superKeyword(){
		System.out.println("系统默认调用了父类无参数构造方法 ");
	} 
	superKeyword(int numberFa){
		this.numberFa = numberFa;
		System.out.println("父类的手机号码: "+numberFa);
	}
}
//父类
public class superKeyword_Test extends superKeyword {
	//成员变量
	int age,number;
	double Fraction;
	
	//没有super();的情况时,系统默认该方法有 super(),此时就调用了父类没有参数的构造方法
	superKeyword_Test(int age,int number){
		this.age = age;
		this.number = number;
		System.out.println("子类的年龄:"+age+" 子类的手机号码: "+number+"\n");
	}

	//有super();的情况时,系统就调用父类相应参数的构造方法
	superKeyword_Test(double Fraction, int number) {
		super(117330111);
		this.Fraction = Fraction;
		this.number = number;
		System.out.println("子类的分数:"+Fraction+" 子类的手机号码: "+number);
	}
	
   public static void main(String[] args){
	   //调用没有super()的构造方法
	   superKeyword_Test skt = new superKeyword_Test(18,117120);  
           //调用有super()的构造方法
	   superKeyword_Test skt1 = new superKeyword_Test(111.11,117110);     
   }
}

Code output:

The default parent class called no-argument constructor 
ages subclasses: sub-class 18 phone number: 117120


Parent's phone number: 117330111
fraction subclass: 111.11 subclass phone number: 117 110


have to be aware of is:

When there is no sub-class constructor super () keyword, and not the parent class constructor without parameters, there is one or more constructor parameters when there is an error;

The following code ↗:

//父类
public class superKeyword {
	//父类的成员变量
	int numberFa;
	superKeyword(int numberFa){
		this.numberFa = numberFa;
		System.out.println("父类的手机号码: "+numberFa);
	}
}
//子类
public class superKeyword_Test extends superKeyword {
	//成员变量
	int age,number;
	double Fraction;
	
	//没有super();的情况时,系统默认该方法有 super(),此时就调用了父类没有参数的构造方法
        //当父类中没有无参数构造方法,却有一个或多个有参数构造方法时,是错误的;
	superKeyword_Test(int age,int number){
		this.age = age;
		this.number = number;
		System.out.println("子类的年龄:"+age+" 子类的手机号码: "+number+"\n");
	}

   public static void main(String[] args){
	   //调用没有super()的构造方法
	   superKeyword_Test skt = new superKeyword_Test(18,117120);  
   }
}

代码报错:Implicit super constructor superKeyword() is undefined. Must explicitly invoke another constructor 

                  Implicit super constructor defined superKeyword (). You must explicitly call another constructor


 

Keyword super operation hidden member variables and methods:

1, when the subclass inherits member variables and methods of the parent class, once a hidden parent class member variables and methods, member variables and methods that these will be assigned to the keyword super owned;

2, the sub-class call these hidden member variables and methods, we need to use the keyword super job;

3, when the call is hidden method super, member variables which appear in a quilt class member variables hidden or inherited member variables;

The following code ↗:

//父类
public class superKeyword {
    //声明父类的成员变量和方法
	double Fa = 456.456;
	void methodFa(int x,int y){
		Fa = x*y;
		System.out.println("父类的方法,x*y后 Fa的值="+Fa);
	}
}
//父类
public class superKeyword_Test extends superKeyword {
	
	//进行重写,对父类的成员变量和方法进行隐藏
	double Fa = 111.111;
	void methodFa(int x,int y){
		Fa = x+y;
		System.out.println("子类的方法,调用自己的成员变量Fa,x+y后 Fa的值="+Fa);
		//调用父类被隐藏的成员变量
		super.Fa = x-y;
System.out.println("子类的方法,通过super调用被隐藏的成员变量Fa,x-y后 Fa的值="+super.Fa);
	//调用父类被隐藏的方法,此时方法中操作的成员变量是被子类隐藏的成员变量 或 继承的成员变量
		super.methodFa(11, 11);
	}
	
   public static void main(String[] args){
	   //创建对象
	   superKeyword_Test skt = new superKeyword_Test();
	   //调用方法
	   skt.methodFa(100, 10);
   }
}

Code output:

Method subclass, calling their member variables Fa, x + value of Fa after y = 110.0
subclass of the super calling Fa hidden member variable, value = 90.0 xy after Fa is
the parent class, x * after the value of y = Fa 121.0


have to be aware of is:

In the main class main () method is not performed super.XXX operation;

Will get an error: Can not use super in a static context; // super can not be used in a static context

 

Published 57 original articles · won praise 10 · views 7540

Guess you like

Origin blog.csdn.net/LagerSwan/article/details/104276903