1. 创建三个类People、ChinaPeople、AmericanPeople,其中People是父类,另外两个是其子类。
父类和测试类已经有完整定义了,根据输出结果,完成ChinaPeople和AmericanPeople的编写。
【输入形式】
无
【输出形式】
相应字符串。
【样例输入】
无
【样例输出】
hello,I am ChinaPeople.
avarge heigh of ChinaPeople:180.0
average weight of ChinaPeople:130.0
I can gongfu.
hello,I am American!
avarge heigh of American:175.0
average weight of American:150.0
I can boxing.
package CG;//一父类两子类一测试类,自己编写两子类
public class TestPeople {
public static void main(String[] args) {
// TODO Auto-generated method stub
ChinaPeople chp = new ChinaPeople();
chp.height = 180;
chp.weight = 130;
chp.speakHello();
chp.averageHeight();
chp.averageWeight();
chp.chinaGongfu();
AmericanPeople amp = new AmericanPeople();
amp.setHeight(175);
amp.setWeight(150);
amp.speakHello();
amp.averageHeight();
amp.averageWeight();
amp.amercianBoxing();
}
}
class People {
protected double height;
protected double weight;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void speakHello() {//根据输出结果倒推,需要在子类进行方法覆写
System.out.println("hello!");
}
public void averageHeight() {//根据输出结果倒推,需要在子类进行方法覆写
System.out.println("avarge heigh:" + height);
}
public void averageWeight() {//根据输出结果倒推,需要在子类进行方法覆写
System.out.println("average weight:" + weight);
}
}
class AmericanPeople extends People {//自己编写部分
public void speakHello() {//子类进行方法覆写
System.out.println("hello,I am American!");
}
public void averageHeight() {//子类进行方法覆写
System.out.println("avarge heigh of American:" + height);
}
public void averageWeight() {//子类进行方法覆写
System.out.println("average weight of American:" + weight);
}
public void amercianBoxing() {
System.out.println("I can boxing.");
}
}
class ChinaPeople extends People {//自己编写部分
public void speakHello() {//子类进行方法覆写
System.out.println("hello,I am ChinaPeople.");
}
public void averageHeight() {//子类进行方法覆写
System.out.println("avarge heigh of ChinaPeople:" + height);
}
public void averageWeight() {//子类进行方法覆写
System.out.println("average weight of ChinaPeople:" + weight);
}
public void chinaGongfu() {
System.out.println("I can gongfu.");
}
}
2. 定义类Human,具有若干属性和功能;定义其子类Man、Woman;在主类TestHuman中测试其特性。
【输入形式】
无
【输出形式】
my name is:Dave
my age is:28
my sex is:man
I am a man.
I am a strong man.
my name is:Jude
my age is:58
my sex is:woman
I am a woman.
I am a beautiful woman.
【样例输入】
无
【样例输出】
my name is:Dave
my age is:28
my sex is:man
I am a man.
I am a strong man.
my name is:Jude
my age is:58
my sex is:woman
I am a woman.
I am a beautiful woman.
package CG;
public class TestHuman {
public static void main(String[] args) {
// TODO Auto-generated method stub
Man m = new Man();
m.setName("Dave");
m.setAge(28);
m.setSex("man");
m.getInfo();
m.speak();
m.run();
Human h = new Woman();
h.setName("Jude");
h.setAge(58);
h.setSex("woman");
h.getInfo();
h.speak();
h.run();
}
}
class Human {
private String name;
private int age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void speak() {//根据输出结果倒推,需要在子类进行方法覆写
System.out.println("I am a human.");
}
public void run() {//根据输出结果倒推,需要在子类进行方法覆写
System.out.println("I can run.");
}
public void getInfo() {
System.out.println("my name is:" + getName());
System.out.println("my age is:" + getAge());
System.out.println("my sex is:" + getSex());
}
}
class Man extends Human {//自己编写部分
public void speak() {//进行方法覆写
System.out.println("I am a man.");
}
public void run() {//进行方法覆写
System.out.println("I am a strong man.");
}
}
class Woman extends Human {//自己编写部分
public void speak() {//进行方法覆写
System.out.println("I am a woman.");
}
public void run() {//进行方法覆写
System.out.println("I am a beautiful woman.");
}
}