java 上机实验五 子类与继承
2017-01-02 15:07
543 查看
相关知识点:
继承:
在不在同一个包中:父类中的private 和 友好访问 权限的成员是不会被继承的
子类和父类中成员变量的名字相同的时候,继续的变量将会被隐藏
重写方法无法操作子类隐藏的成员变量和方法
实验一:
实验目的:了解继承性,方法的重写和继承
实验二:
银行利息计算
实验目的:
掌握怎么重修和使用super关键字
实验三:
实验目的:掌握上转型对象的使用
继承:
在不在同一个包中:父类中的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()); } }
相关文章推荐
- 第5章-子类与继承-上机实验1(猫与狗)
- 3.28 java 上机实验 继承
- 通过实验看 Java 中私有成员是否能被继承
- Java-关于子类是否继承了父类的private实例变量
- 《java第十周实验报告1-1》---点、线类的继承练习
- java中子类继承父类时是否继承构造函数
- Java 中子类是否只继承父类的非私有变量和方法?
- 《通过实验看 Java 中私有成员是否能被继承》续
- 第二周JAVA上机实验(三)猜数游戏
- java 子类继承父类 -- 重写、覆盖与多态
- JAVA实验2 类和继承(2学时)List类
- 第二周JAVA上机实验(一)输出26个希腊字母
- java类继承中父类调用子类函数的问题
- JAVA 子类继承父类的构造函数的重载
- Java学习笔记---13.面向对象编程08-Java中继承的概念,子类实例化过程,super()及方法复写
- 实验3_Java类的继承和接口的定义和使用
- java中子类继承父类时是否继承构造函数呢?
- java中的继承(子类调用父类构造方法还不懂)
- java中子类继承父类时是否继承构造函数
- C++和Java在 子类继承父类时,两者成员函数重写和重载的特性