java面向对象四大特性之二 继承,封装

来源:互联网 发布:纯js分页代码 编辑:程序博客网 时间:2024/06/05 07:13




/*
 * main方法程序入口控制台
 */
public class Test {


public static void main(String[] args) {
// TODO Auto-generated method stub
Ji a=new Ji("虫子","打鸣", "喔喔","芦花鸡");
a.print();
Bow b=new Bow("小鱼虾","斑嘴鸭","嘎嘎", "游泳");

a.print();


}

}




public class Cw {
/*
* 父类
* 存放共有属性类
*/
//定义变量
private String name ;
private String  pz;
//get,set  封装方法

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPz() {
return pz;
}
public void setPz(String pz) {
this.pz = pz;
}
/*
* 定义带参方法
*/
public Cw(String name , String pz){
this.name =name;
this.pz=pz;

}
//带参方法输出
public void print(){
System.out.println("我是:"+name+"我叫"+pz);
}


}



public class Ji extends Cw {
/*
 * 子类
 * 小鸡类
 */
//定义变量
private String ah;
private String xw;



//get,set 方法
public String getAh() {
return ah;
}


public void setAh(String ah) {
this.ah = ah;
}


public String getXw() {
return xw;
}


public void setXw(String xw) {
this.xw = xw;
}


//带参构造方法
public Ji(String ah, String xw, String name, String pz) {
super(name, pz);
this.ah = ah;
this.xw = xw;
//特有属性输出
System.out.println("我吃"+ah);
System.out.println("我会"+xw);
}


}



public class Bow extends Cw {
/*
 * 子类
 * 鸭子类
 */
//定义方法
private String chi;
private String dz;


//get,set 方法封装
public String getChi() {
return chi;
}


public void setChi(String chi) {
this.chi = chi;
}


public String getDz() {
return dz;
}


public void setDz(String dz) {
this.dz = dz;
}


/*
* 带参构造方法
*/
public Bow(String chi , String dz,String name, String pz) {
super(name, pz);
this.chi = chi;
this.dz = dz;
//本类特有属性输出
System.out.println("我吃"+chi);
System.out.println("我会"+dz);
}

}




阅读全文
1 0
原创粉丝点击