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

java 上机实验五 子类与继承

2017-01-02 15:07 543 查看
相关知识点:

继承:

在不在同一个包中:父类中的private 和 友好访问 权限的成员是不会被继承的

子类和父类中成员变量的名字相同的时候,继续的变量将会被隐藏

重写方法无法操作子类隐藏的成员变量和方法

实验一:

实验目的:了解继承性,方法的重写和继承

public class people {
protected double weight,height;
public void speakHello(){
System.out.println("yayayaya");
}

public void averageHeight(){
height=173;
System.out.println("average height :"+ height);
}

public void averageWeight(){
weight=70;
System.out.println("average weight :"+ weight);
}
}


public class ChinaPeople extends people{
public void speakHello(){
System.out.println("你好");
}
public void averageHeight(){
height=168.78;
System.out.println("中国人的平均身高 :" +height+" CM ");
}
public void averageWeight(){
weight=65;
System.out.println("中国人的平均体重 :" +weight+" KG ");
}
public void ChinaChongFu(){
System.out.println("中国功夫");
}
}


public class AmericanPeople extends people{
public void speakHello(){
System.out.println("How do you do");
}

public void averageHeight(){
height=176;
System.out.println("average height :"+ height);
}

public void averageWeight(){
weight=75;
System.out.println("average weight :"+ weight);
}
public void americanBoxing(){
System.out.println("美国搏击");
}
}


public class BeijingPeople extends ChinaPeople{
public void averageHeight(){
height=172.5;
System.out.println("北京人的平均身高 :" +height+" CM ");
}
public void averageWeight(){
weight=70;
System.out.println("北京人的平均体重 :" +weight+" KG ");
}
public void beijingOpera(){
System.out.println("京剧");
}
}


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

chinaPeople.speakHello();
americanPeople.speakHello();
beijingPeople.speakHello();

chinaPeople.averageHeight();
americanPeople.averageHeight();
beijingPeople.averageHeight();

chinaPeople.averageWeight();
americanPeople.averageWeight();
beijingPeople.averageWeight();

chinaPeople.ChinaChongFu();;
americanPeople.americanBoxing();;
beijingPeople.beijingOpera();
beijingPeople.ChinaChongFu();
}
}


实验二:

银行利息计算

实验目的:

掌握怎么重修和使用super关键字

public class Bank {
int saveMoney;
int year;
double interest;
double interestRate=0.29;

public double computerInterest(){
return interest=year*interestRate*saveMoney;
}
public void setInterestRate(double rate){
interestRate=rate;
}
}


public class ConstructionBank extends Bank{
double year;
public double computerInterest(){
super.year=(int)year;
double r=year-(int)year;
int day=(int)r*1000;

double yearInterest=super.computerInterest();
double dayInterest=day*0.0001*saveMoney;

interest=yearInterest+dayInterest;
System.out.printf("%d 元存在建设银行%d年零%d天的利息:%f元\n",
saveMoney,super.year,day,interest);
return interest;
}
}


public class BankOfDalian extends Bank{
double year;
public double computerInterest(){
super.year=(int)year;
double r=year-(int)year;
int day=(int)(r*1000);

double yearInterest=super.computerInterest();
double dayInterest=day*0.00012*saveMoney;
interest=yearInterest+dayInterest;
System.out.printf("%d 元存在大连银行%d年零%d天的利息:%f元\n",
saveMoney,super.year,day,interest);
return interest;
}
}


public class Main{
public static void main(String args[]){
int amount=8000;

ConstructionBank bank1=new ConstructionBank();
bank1.year=8.236;
bank1.saveMoney=amount;
bank1.setInterestRate(0.035);
double interest1=bank1.computerInterest();

BankOfDalian bank2=new BankOfDalian();
bank2.saveMoney=amount;
bank2.year=8.236;
bank2.setInterestRate(0.035);
double interest2=bank2.computerInterest();
System.out.printf("两个银行利息相差%f元 \n",interest1-interest2);
}
}


实验三:

实验目的:掌握上转型对象的使用

abstract class Employee{
public abstract double earning();
}

class YearWorkers extends Employee{
public double earning(){
return 12000;
}
}

class MonthWorkers extends Employee{
public double earning(){
return 12*2300;
}
}

class WeekWorkers extends Employee{
public double earning(){
return 52*780;
}
}

class Company{
Employee[] employee;
double salaries=0;
Company(Employee[] employee){
this.employee=employee;
}

public double salariesPay(){
salaries=0;
for(int i=0;i<employee.length;i++)
salaries+=employee[i].earning();
return salaries;
}
}

public class Main{
public static void main(String args[]){
Employee[] employee=new Employee[29];
for(int i=0;i<employee.length;i++){
if(i%3==0)
employee[i]=new WeekWorkers();
else if(i%3==1)
employee[i]=new MonthWorkers();
else
employee[i]=new YearWorkers();
}

Company company=new Company(employee);
System.out.println("公司总薪水"+company.salariesPay());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: