(1)代码如下:
People类:
package org.jsoft.jicheng;
public class People {
private int height;
private int weight;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void speakHello(){
System.out.println("人初次见面say hello!");
}
public int averageHeigh(){
System.out.println("人的平均身高为"+height);
return this.height;
}
public int averageWeight(){
System.out.println("人的平均体重为:"+weight);
return this.weight;
}
}
ChinaPeople类:
package org.jsoft.jicheng;
public class ChinaPeople extends People{
public void chinaMartial(){
System.out.println("中国武术");
}
public void speakHello(){
System.out.println("中国人初次见面say hello!");
}
public int averageHeigh(){
System.out.println("中国人的平均身高为:"+this.getHeight());
return super.averageHeigh();
}
public int averageWeight(){
System.out.println("中国 人的平均体重为:"+this.getWeight());
return super.averageWeight();
}
}
AmericanPeople类:
package org.jsoft.jicheng;
public class AmericanPeople extends People {
public void AmericanBoxing(){
System.out.println("美国人Boxing");
}
public void speakHello(){
System.out.println("美国人初次见面say hello!");
}
public int averageHeigh(){
System.out.println("美国人人的平均身高为:"+this.getHeight());
return this.getHeight();
}
public int averageWeight(){
System.out.println("美国人的平均体重为:"+this.getWeight());
return this.getWeight();
}
}
BeijingPeople 类:
package org.jsoft.jicheng;
public class BeijingPeople extends ChinaPeople{
public void BeijingOpera(){
System.out.println("北京京剧");
}
public void chinaMartial(){
System.out.println("中国京剧");
}
public void speakHello(){
System.out.println("北京人初次见面say hello!");
}
public int averageHeigh(){
System.out.println("北京人的平均身高为:"+this.getHeight());
return this.getHeight();
}
public int averageWeight(){
System.out.println("北京人的平均体重为:"+this.getWeight());
return this.getWeight();
}
}
测试类:
package org.jsoft.jicheng;
public class Test {
public static void main(String[] args) {
People p=new People();
p.setHeight(170);
p.setWeight(120);
p.speakHello();
p.averageHeigh();
p.averageWeight();
System.out.println("*************************");
ChinaPeople c=new ChinaPeople();
c.setHeight(180);
c.setWeight(130);
c.chinaMartial();
c.speakHello();
c.averageHeigh();
c.averageWeight();
System.out.println("*************************");
AmericanPeople a=new AmericanPeople();
a.setHeight(185);
a.setWeight(135);
a.AmericanBoxing();
a.averageHeigh();
a.averageWeight();
System.out.println("*************************");
BeijingPeople b=new BeijingPeople();
b.setHeight(171);
b.setWeight(121);
b.chinaMartial();
b.BeijingOpera();
b.averageHeigh();
b.averageWeight();
}
}
运行结果:
(2)
代码如下:
Account类:
package org.jsoft.yinhang;
public class Account {
private double balance;
private String password;
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
if(password.length()==6){//如果新密码长度为6,则进行密码修改;
this.password=password;
System.out.println("您的新密码为:"+password);
}
else{
System.out.println("密码不符合条件");
}
}
}
SavingAccount类:
package org.jsoft.yinhang;
public class SavingAccount extends Account {
private double interesRate;
public double getInteresRate() {
return interesRate;
}
public void setInteresRate(double interesRate) {
if(interesRate > 0 && interesRate < 0.1){
this.interesRate = interesRate;}
}
}
CreditAccount类:
package org.jsoft.yinhang;
public class CreditAccount extends Account{
private double creditLine;
public double getCreditLine() {
return creditLine;
}
public void setCreditLine(double creditLine) {
this.creditLine = creditLine;
}
}
测试类:
package org.jsoft.yinhang;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入您的新密码:");
String password= input.nextLine();
Account a=new Account();
a.setId(807071708);
a.setBalance(10000000);
a.setPassword(password);
Scanner input1 = new Scanner(System.in);
System.out.println("请输入您的利率:");
double i=input1.nextDouble();
SavingAccount s=new SavingAccount();
s.setInteresRate(i);
System.out.println("您的利率为:"+s.getInteresRate());//不符合条件则为0.0
}
}
运行结果:
(3)
答:
A能通过:子类中方法的访问修饰符必须 >= 父类中对应方法的访问修饰符 ,A项符合该条件
B能通过:和父类中的方法没有关系,是子类中自己定义的方法
C能通过:和父类中的方法没有关系,是子类中自己定义的方法
(4)
将System.out.println()括号中的.value都改为.getValue()