我寫(xiě)了一個(gè),內(nèi)容比較簡(jiǎn)單的。代碼如下:public class AnimalTest {
成都創(chuàng)新互聯(lián)公司是專業(yè)的城子河網(wǎng)站建設(shè)公司,城子河接單;提供網(wǎng)站制作、網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行城子河網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
Animal animal;
public void eat(Animal animal){
animal.eat();
}
public void walk(Animal animal){
animal.walk();
}
public static void main(String args[]){
Animal animal=new Animal("animal");
Wolf w=new Wolf("wolf");
Goat g=new Goat("goat");
AnimalTest at=new AnimalTest();
at.eat(animal);
at.eat(w);
at.eat(g);
at.walk(animal);
at.walk(w);
at.walk(g);
}
}
class Animal {
String name;
public Animal(String name){
this.name=name;
}
public Animal(){}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void eat(){
System.out.println("animal eat");
}
public void walk(){
System.out.println("animal walk");
}
public String toString(){
return name;
}
}class Wolf extends Animal {
public Wolf(String name){
super(name);
}
public Wolf(){}
public void eat(){
System.out.println("wolf eat meat");
}
public void walk(){
System.out.println("wolf walk");
}
public String toString(){
return name;
}
}class Goat extends Animal {
public Goat(String name){
super(name);
}
public Goat(){}
public void eat(){
System.out.println("goat eat grass");
}
public void walk(){
System.out.println("goat walk");
}
public String toString(){
return name;
}
}
JAVA中被?final?標(biāo)志的類,是最終類,不可以被繼承,如:public 、final 、String。
JAVA繼承的介紹
繼承是面向?qū)ο笞铒@著的一個(gè)特性,繼承是從已有的類中派生出新的類,新的類能吸收已有類的數(shù)據(jù)屬性和行為,并能擴(kuò)展新的能力。
Java繼承是使用已存在的類的定義作為基礎(chǔ)建立新類的技術(shù),新類的定義可以增加新的數(shù)據(jù)或新的功能,也可以用父類的功能,但不能選擇性地繼承父類。
這種技術(shù)使得復(fù)用以前的代碼非常容易,能夠大大縮短開(kāi)發(fā)周期,降低開(kāi)發(fā)費(fèi)用。比如可以先定義一個(gè)類叫車(chē),車(chē)有以下屬性:車(chē)體大小,顏色,方向盤(pán),輪胎,而又由車(chē)這個(gè)類派生出轎車(chē)和卡車(chē)兩個(gè)類,為轎車(chē)添加一個(gè)小后備箱,而為卡車(chē)添加一個(gè)大貨箱。
第一個(gè):
public?class?Yaojing?{
protected?String?name;
protected?int?age;
protected?String?gender;
public?void?showBasicInfo()?{
System.out.println(toString());
}
public?void?eatTangSeng()?{
System.out.println("吃飽了");
}
@Override
public?String?toString()?{
return?"Yaojing?[name="?+?name?+?",?age="?+?age?+?",?gender="?+?gender?+?"]";
}
}
第二個(gè)類
public?class?Zhizhujing?extends?Yaojing?{
public?void?buildNet(){
System.out.println("蜘蛛在織網(wǎng)");
}
}
第三個(gè)類
public?class?Baigujing?extends?Yaojing?{
public?void?beBeauty(){
System.out.println("白骨精");
}
}
參考程序:
public?class?Student?{
private?String?name;
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?void?goHome(Traffic?t){
System.out.println("學(xué)生:"+this.name);
t.run();
}
public?Student(String?name)?{
this.name?=?name;
}
}
public?abstract?class?Traffic?{
protected?double?speed;
public?void?run(){
}
}
public?class?Bus?extends?Traffic{
@Override
public?void?run()?{
System.out.println("時(shí)速為"+super.speed+"公里每小時(shí)的汽車(chē)正在嘀嗒嘀嗒的開(kāi)著.");
}
public?Bus(double?busSpeed)?{
super.speed?=?busSpeed;
}
}
public?class?Train?extends?Traffic?{
@Override
public?void?run()?{
System.out.println("時(shí)速為"+super.speed+"公里每小時(shí)的火車(chē)正在況且況且的開(kāi)著.");
}
public?Train(double?trainSpeed)?{
super.speed?=?trainSpeed;
}
}
public?class?AirPlan?extends?Traffic?{
@Override
public?void?run()?{
System.out.println("時(shí)速為"+super.speed+"公里每小時(shí)的飛機(jī)正在吳屋吳屋的開(kāi)著.");
}
public?AirPlan(double?airPlanSpeed)?{
super.speed?=?airPlanSpeed;
}
}
public?class?Test?{
public?static?void?main(String[]?args)?{
Traffic?traffic1?=?new?Bus(40);
Student?student?=?new?Student("小明");
student.goHome(traffic1);
Traffic?traffic2?=?new?Train(120);
Student?student2?=?new?Student("小花");
student2.goHome(traffic2);
Traffic?traffic3?=?new?AirPlan(300);
Student?student3?=?new?Student("小紅");
student3.goHome(traffic3);
}
}