您的位置:首页 > 编程语言 > Java开发

吉软-java-第七次作业

2017-11-09 00:00 417 查看
1.People类

package org.jsoft.zuoye;

public class People {
private double height;
private 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 String speakHello(){
return "unknown";
}
public double averageHeight(){
return 173;
}
public double averageWeight(){
return 70;
}

}

ChinaPeople类

package org.jsoft.zuoye;

public class ChinaPeople extends People{
public String speakHello(){
return "您好,吃了吗?";
}
public double averageHeight(){
return 168.78;
}
public double averageWeight(){
return 65;
}
public String chinaMartial(){
return "中国功夫";
}
}

AmericanPeople类

package org.jsoft.zuoye;

public class AmericanPeople extends People {
public String speakHello(){
return "Hello,how are you?";
}
public double averageHeight(){
return 176;
}
public double averageWeight(){
return 75;
}
public String americanBoxing(){
return "直拳、勾拳、组合拳";
}
}

BeijingPeople类

package org.jsoft.zuoye;

public class BeijingPeople extends ChinaPeople {
public String speakHello(){
return "嘛呢?";
}
public double averageHeight(){
return 172.5;
}
public double averageWeight(){
return 70;
}
public String beijingOpera(){
return "花脸、青衣、花旦、老生";
}
}
测试类test

package org.jsoft.zuoye;

public class Test {
public static void main(String[] args){
ChinaPeople chinaPeople=new ChinaPeople();
AmericanPeople americanPeople=new AmericanPeople();
BeijingPeople beijingPeople=new BeijingPeople();

System.out.println(chinaPeople.speakHello());
System.out.println(americanPeople.speakHello());
System.out.println(beijingPeople.speakHello());

System.out.println("中国人的平均身高:"+chinaPeople.averageHeight());
System.out.println("美国人的平均身高:"+americanPeople.averageHeight());
System.out.println("北京人的平均身高:"+beijingPeople.averageHeight());

System.out.println("中国人的平均体重:"+chinaPeople.averageWeight());
System.out.println("美国人的平均体重:"+americanPeople.averageWeight());
System.out.println("北京人的平均体重:"+beijingPeople.averageWeight());

System.out.println(chinaPeople.chinaMartial());
System.out.println(americanPeople.americanBoxing());
System.out.println(beijingPeople.beijingOpera());
}
}

输出结果:

您好,吃了吗?
Hello,how are you?
嘛呢?
中国人的平均身高:168.78
美国人的平均身高:176.0
北京人的平均身高:172.5
中国人的平均体重:65.0
美国人的平均体重:75.0
北京人的平均体重:70.0
中国功夫
直拳、勾拳、组合拳
花脸、青衣、花旦、老生

2.Account类:

package org.jsoft.zuoye;

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 password;
}
public void setPassword(String password) {
this.password = password;
}

}

CreditAccount类:

package org.jsoft.zuoye;

public class CreditAccount extends Account{
private double creditLine;

public double getCreditLine() {
return creditLine;
}
public void setCreditLine(double creditLine) {
this.creditLine = creditLine;
}
//重写的setPassword方法
public void setPassword(String str){
if(str.length()==6){
super.setPassword(str);
System.out.println("密码设置成功!");
}
else{
System.out.println("密码应为6位!");
}
}

//重写的getPassword方法
public String getPassword(){
return null;
}
}

SavingAccount类:

package org.jsoft.zuoye;

public class SavingAccount extends Account{
private double interestRate;

public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
//重写的setPassword方法
public void setPassword(String str){
if(str.length()==6){
super.setPassword(str);
}
else{
System.out.println("密码应为6位!");
}
}

//重写的getPassword方法
public String getPassword(){
return null;
}
}

测试类:TestAccount

package org.jsoft.zuoye;

public class TestAccount {
public static void main(String[] args){
CreditAccount ca=new CreditAccount();
SavingAccount sa=new SavingAccount();

ca.setId(150511630);
ca.setCreditLine(100000);
ca.setPassword("123456");
System.out.println(ca.getPassword());

sa.setId(150511640);
sa.setInterestRate(0.02);
sa.setPassword("12345");
System.out.println(sa.getPassword());
}
}

3.A,能编译通过,子类重写的方法有更高的访问权限,父类中是缺省权限,子类中是public权限

B,不能编译通过,方法重写不能只有返回值类型不同

C,能编译通过,方法重写,参数列表不同

4.TestMyClass类中:

mc1.value改成:mc1.getValue()

mc2.value改成:mc2.getValue()

这两个地方不能编译的原因相同,value是private方法,只能在本类中使用

TestMyClass2类中:

MyClass mc2=new MyClass();//MyClass类中,带参数的构造方法,权限是缺省,只能在本包中使用

mc2.setValue(10);

mc1.value改成:mc1.getValue()

mc2.value改成:mc2.getValue()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: