1.
package org.jsoft;
public class People {
private String height,weight;
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public String speakHello(){
return "say hello!";
}
public double averageHeight(){
return 170;
}
public double averageWeight(){
return 120;
}
}
package org.jsoft;
public class ChinaPeople extends People{
//新增方法
public String chinaMartial(){
return "中国人会武术";
}
//重写方法
public String speakHello(){
return "ChinaPeople can say hello";
}
public double averageHeight(){
return 170;
}
public double averageWeight(){
return 130;
}
}
package org.jsoft;
public class AmericanPeople extends People{
//新增方法
public String americanBoxing(){
return "AmericanPeople can say Boxing";
}
//重写方法
public String speakHello(){
return "say hello";
}
public double averageHeight(){
return 185;
}
public double averageWeight(){
return 140;
}
}
package org.jsoft;
public class BeijingPeople extends People{
//新增方法
public String beijingOpera(){
return "beijingPeople can beijingOpera";
}
//重写方法
public String speakHello(){
return "say hello";
}
public double averageHeight(){
return 176;
}
public double averageWeight(){
return 135;
}
}
//测试
package org.jsoft;
import java.util.Scanner;
public class TextPeoleAll {
public static void main(String[] args) {
//测试People
People p=new People();
//测试ChinaPeople
ChinaPeople c=new ChinaPeople();
//测试AmericanPeople
AmericanPeople a=new AmericanPeople();
//测试BeijingPeople
BeijingPeople b=new BeijingPeople();
System.out.println(c.chinaMartial());
System.out.println("中国人平均身高:"+c.averageHeight());
System.out.println("中国人平均体重:"+c.averageWeight());
System.out.println(c.speakHello());
System.out.println(a.americanBoxing());
System.out.println("美国人平均身高:"+a.averageHeight());
System.out.println("美国人平均体重:"+a.averageWeight());
System.out.println(a.speakHello());
System.out.println(b.beijingOpera());
System.out.println("北京人平均身高:"+b.averageHeight());
System.out.println("北京人平均体重:"+b.averageWeight());
System.out.println(b.speakHello());
}
}
//结果
中国人会武术
中国人平均身高:170.0
中国人平均体重:130.0
ChinaPeople can say hello
AmericanPeople can say Boxing
美国人平均身高:185.0
美国人平均体重:140.0
say hello
beijingPeople can beijingOpera
北京人平均身高:176.0
北京人平均体重:135.0
say hello
2.
package org.jsoft;
public class Account {
private long id;
private double balance;
private String password;
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 null;
}
public void setPassword(String password) {
if (password.length()==6) {
this.password = password;
System.out.println("修改密码成功");
}
else {
System.out.println("输入错误,请重新输入");
}
this.password = password;
}
}
package org.jsoft;
public class SavingAccount extends Account{
private double interestRate;
public long id(){
return 021602;
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
if(interestRate>0&&interestRate<0.1){
this.interestRate = interestRate;
System.out.println("利率正确");
}else{
System.out.println("请重新输入:");
}
}
}
package org.jsoft;
public class CreditAccount extends Account{
Account a=new Account();
private double creditLine;
public double getCreditLine() {
return creditLine;
}
public void setCreditLine(double creditLine) {
this.creditLine = creditLine;
}
}
//测试
package org.jsoft;
import java.util.Scanner;
public class TextAccount {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("是否修改密码:");
String x = sc.next();
if(x!= null){
Account a = new Account();
System.out.println("请输入新密码:");
Scanner sc1=new Scanner(System.in);
String p = sc1.next();
a.setPassword(p);
}
SavingAccount s=new SavingAccount();
System.out.println("请输入利率:");
Scanner sc2=new Scanner(System.in);
double l=sc2.nextDouble();
s.setInterestRate(l);
}
}
//结果
是否修改密码:
是
请输入新密码:
123333
修改密码成功
请输入利率:
0.02
利率正确
3.A
4.
mc1.value改成mc1.getValue()
mc2.value改成mc2.getValue()
mc1.value改成mc1.getValue()
mc2.value改成mc2.getValue()