[code]
/********************
* super的两种用法
* 用法一:
* 子类的构造方法中初始化从父类继承过来的变量。
* 用法二:
* 用来访问父类中具有和自身相同字段的值。
* ************************/
package com.aabnn.parent;
public class Parent {
protected String name;
protected boolean gender;
protected int age;
protected String hobby;
public Parent(){
}
public Parent(String name,boolean gender,int age,String hobby){
this.name=name;
this.gender=gender;
this.age=age;
this.hobby=hobby;
}
}
package com.aabnn.parent;
public class Son extends Parent{
private String company;
private String hobby;
public Son(){
super();
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getParentName(){
return super.name;
}
public String getParentHobby(){
return super.hobby;
}
public Son(String name,boolean gender,int age,String hobby,String hobby1,String company){
super(name,gender,age,hobby);
this.hobby=hobby1;
this.company=company;
}
}
package com.aabnn.test;
import com.aabnn.parent.Son;
public class TestSuper {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Son son=new Son("shanzao",true,18,"basketball","sing","hotmail");
System.out.println("the father name of the son is:"+son.getParentName());
System.out.println("the father hobby is:"+son.getParentHobby());
System.out.println("the son hobby is:"+son.getHobby());
System.out.println("the company of the son is:"+son.getCompany());
}
}
[/code]
/********************
* super的两种用法
* 用法一:
* 子类的构造方法中初始化从父类继承过来的变量。
* 用法二:
* 用来访问父类中具有和自身相同字段的值。
* ************************/
package com.aabnn.parent;
public class Parent {
protected String name;
protected boolean gender;
protected int age;
protected String hobby;
public Parent(){
}
public Parent(String name,boolean gender,int age,String hobby){
this.name=name;
this.gender=gender;
this.age=age;
this.hobby=hobby;
}
}
package com.aabnn.parent;
public class Son extends Parent{
private String company;
private String hobby;
public Son(){
super();
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getParentName(){
return super.name;
}
public String getParentHobby(){
return super.hobby;
}
public Son(String name,boolean gender,int age,String hobby,String hobby1,String company){
super(name,gender,age,hobby);
this.hobby=hobby1;
this.company=company;
}
}
package com.aabnn.test;
import com.aabnn.parent.Son;
public class TestSuper {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Son son=new Son("shanzao",true,18,"basketball","sing","hotmail");
System.out.println("the father name of the son is:"+son.getParentName());
System.out.println("the father hobby is:"+son.getParentHobby());
System.out.println("the son hobby is:"+son.getHobby());
System.out.println("the company of the son is:"+son.getCompany());
}
}
[/code]