super 例解

[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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值