Java基础练习中,子类如何通过继承实现长尾词功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计441个文字,预计阅读时间需要2分钟。
java// 父类:鸟class Bird { protected String name; protected String feather; protected String leg;
public Bird(String name, String feather, String leg) { this.name=name; this.feather=feather; this.leg=leg; }
public void speak() { System.out.println(name + says: 翅膀拍拍,两腿站立。); }}
// 子类:麻雀class Sparrow extends Bird { public Sparrow(String feather, String leg) { super(麻雀, feather, leg); }}
// 子类:鹏鸟class Phoenix extends Bird { public Phoenix(String feather, String leg) { super(鹏鸟, feather, leg); }}
// 子类:鹤class Crane extends Bird { public Crane(String feather, String leg) { super(鹤, feather, leg); }}
// 测试类public class Main { public static void main(String[] args) { Sparrow sparrow=new Sparrow(灰褐色, 细长腿); Phoenix phoenix=new Phoenix(金色, 强壮腿); Crane crane=new Crane(白色, 细长腿);
sparrow.speak(); phoenix.speak(); crane.speak(); }}
1.写一个Java应用程序,主要是体现父类子类间的继承关系。父类:鸟,子类:麻雀、鸵鸟、鹰。子类继承父类的一些特点,如都是鸟的话就都会有翅膀、两条腿等,但它们各自又有各自的特点,如麻雀的年龄、体重;鸵鸟的身高、奔跑速度;鹰的捕食、飞翔高度等。package d; public class Bird { int leg=2; int chibang=2; public void cry(){ System.out.println("@#$%^%^&*~~"); } } package d; public class Maque extends Bird{ int age; int weight; public void setAge(int age) { this.age = age; } public void setWeight(int weight) { this.weight = weight; } public void cry(){ System.out.println("Maque-cry"); } public void showMaque(){ System.out.println(age); System.out.println(weight); } } package d; public class tuoniao extends Bird { int height; int speed; public void setHeight(int height) { this.height = height; } public void setSpeed(int weight) { this.speed = speed; } public void cry(){ System.out.println("tuoniao -cry"); } public void showMaque(){ System.out.println(height); System.out.println(speed); } } package d; public class ying extends Bird{ int Catch; int FlySpeed; public void setCatch(int catch1) { Catch = catch1; } public void setFlySpeed(int flySpeed) { FlySpeed = flySpeed; } public void cry(){ System.out.println("ying-cry"); } public void showMaque(){ System.out.println(Catch); System.out.println(FlySpeed); } }
本文共计441个文字,预计阅读时间需要2分钟。
java// 父类:鸟class Bird { protected String name; protected String feather; protected String leg;
public Bird(String name, String feather, String leg) { this.name=name; this.feather=feather; this.leg=leg; }
public void speak() { System.out.println(name + says: 翅膀拍拍,两腿站立。); }}
// 子类:麻雀class Sparrow extends Bird { public Sparrow(String feather, String leg) { super(麻雀, feather, leg); }}
// 子类:鹏鸟class Phoenix extends Bird { public Phoenix(String feather, String leg) { super(鹏鸟, feather, leg); }}
// 子类:鹤class Crane extends Bird { public Crane(String feather, String leg) { super(鹤, feather, leg); }}
// 测试类public class Main { public static void main(String[] args) { Sparrow sparrow=new Sparrow(灰褐色, 细长腿); Phoenix phoenix=new Phoenix(金色, 强壮腿); Crane crane=new Crane(白色, 细长腿);
sparrow.speak(); phoenix.speak(); crane.speak(); }}
1.写一个Java应用程序,主要是体现父类子类间的继承关系。父类:鸟,子类:麻雀、鸵鸟、鹰。子类继承父类的一些特点,如都是鸟的话就都会有翅膀、两条腿等,但它们各自又有各自的特点,如麻雀的年龄、体重;鸵鸟的身高、奔跑速度;鹰的捕食、飞翔高度等。package d; public class Bird { int leg=2; int chibang=2; public void cry(){ System.out.println("@#$%^%^&*~~"); } } package d; public class Maque extends Bird{ int age; int weight; public void setAge(int age) { this.age = age; } public void setWeight(int weight) { this.weight = weight; } public void cry(){ System.out.println("Maque-cry"); } public void showMaque(){ System.out.println(age); System.out.println(weight); } } package d; public class tuoniao extends Bird { int height; int speed; public void setHeight(int height) { this.height = height; } public void setSpeed(int weight) { this.speed = speed; } public void cry(){ System.out.println("tuoniao -cry"); } public void showMaque(){ System.out.println(height); System.out.println(speed); } } package d; public class ying extends Bird{ int Catch; int FlySpeed; public void setCatch(int catch1) { Catch = catch1; } public void setFlySpeed(int flySpeed) { FlySpeed = flySpeed; } public void cry(){ System.out.println("ying-cry"); } public void showMaque(){ System.out.println(Catch); System.out.println(FlySpeed); } }

