Java super关键字使用

1、super

package ClassN1;

public class Super1 {
	
	private String name;
	private int foot;
	public int hands;
	
	public Super1() {
	}

	public Super1(String name, int foot) {
		super();
		this.name = name;
		this.foot = foot;
	}

	@Override
	public String toString() {
		return "Super1 [name=" + name + ", foot=" + foot + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
				+ ", toString()=" + super.toString() + "]";
	}
	
}
package ClassN1;

public final class Super2 extends Super1 {
	
	private int hands;//隐藏了父类的hands
	
	public Super2(String name,int foot,int hands2,int hands1) {
		super(name,foot);   //1、调用父类的构造函数必须是在构造函数的第一行
		super.hands=hands2; //2、调用被 隐藏的父类成员变量或成员函数
		this.hands=hands1;  	
	}

	
	@Override
	public String toString() {
		return "Super2 [hands=" + hands + ", toString()=" + super.toString() + "]";
	}

	
	public void gethands() {
		System.out.println("子类:"+this.hands);
		System.out.println("父类:"+super.hands);
	}
	public static void main(String[] args) {
		Super2 s=new Super2("BOBO",30,1000,20);
		System.out.println(s.toString());
		s.gethands();
	}
	
}


猜你喜欢

转载自blog.csdn.net/cincoutcin/article/details/79806855