This and super use

Source: Internet
Author: User
This and super use


One, this use:

One is to reference implicit arguments, and the other is to invoke other constructors of that class. second, super use:

One is to call the superclass method, the other is to call the superclass constructor three, use examples

Package COM.TEST.XQH;

	public class Manage extends Employee {private String sex;
	Public Manage (string name, int age, String sex) {Super (name, age);//Super Use 2, call superclass constructor this.setsex (sex); Public String GetName () {return super.getname () + "HHHH";//Super Use 1, calling superclass method} public void Setsex (String sex)
	{this.sex = sex;
	Public String Getsex () {return sex;
	public void Say () {SYSTEM.OUT.PRINTLN ("* *");
		public static void Main (string[] args) {employee[] e = new employee[2];
		E[0] = new Employee ("John", 22);
		E[1] = new Manage ("Dick", 22, "male");
		System.out.println (E[0].getname ());
	System.out.println (E[1].getname ());
	Class Employee {private String name;

	private int age; Public Employee (String name, int age) {this ();//This use 2, invoke the constructor of the class (this example is the default constructor) THIS.name = name;//This use 1, reference implicit parameter T His.age = age;
	This purpose 1 refers to the implicit parameter} public Employee () {} public String GetName () {return name; public void SetName (String name) {thIs.name = name;
	public int getage () {return age;
	public void Setage (int age) {this.age = age; }

}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.