继承与接口实验
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
四、结果与分析
实验1
源代码:class People {
protected double weight,height;
public void speakHello( ) {
System.out.println("yayawawa");
}
public void averageHeight() {
height=173;
AmericanPeople americanPeople=new AmericanPeople( );
BeijingPeople beijingPeople=new BeijingPeople( );
chinaPeople.speakHello( );
americanPeople.speakHello( );
BeijingPeople是ChinaPelple的子类,新增public void beijingOpera()方法。要求ChinaPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。
for(int i=0;i<goods.length;i++)
{
totalWeights+=goods[i].computeWeight();
}
return totalWeights;
}
}
public class Road
{
public static void main(String[] args)
public void speakHello(){
System.out.println("您好");
}
public void averageHeight(){
System.out.println("北京人的平均身高: 167.0 cm");
}
public void averageWeight(){
System.out.println("北京人的平均体重: 68.5 kg");
Public abstract earnings();
子类必须重写父类的earnings()方法,给出各自领取薪水的具体方式。
有一个Company类,该类用Employee数组作为成员,Employee数组的单元可以是YearWorker对象的上转型对象、MonthWorker对象的上转型对象、WeekWorker对象的上转型对象。程序能输出Company对象一年需要支付的薪水总额。
{
salaries=salaries+employee[i].earnings();
}
return salaries;
}
}
public class HardWork
{
public static void main(String args[])
{
Employee[] employee=new Employee[20];
beijingPeople.averageWeight( );
chinaPeople.chinaGongfu( );
americanPeople.americanBoxing( );
beijingPeople.beijingOpera( ) ;
beijingPeople.chinaGongfu( );
}
}
class MonthWorker extends Employee
{
public double earnings( ){
return 12*2300;
}
}
class WeekWorker extends Employee
{
public double earnings( ){
return 48*500;
AmericanPeople类是People的子类,新增public void americanBoxing()方法。要求AmericanPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。
for(int i=0;i<employee.length;i++)
{
if(i%3==0)
employee[i]=new WeekWorker();
else if(i%3==1)
employee[i]=new MonthWorker();
else if(i%3==2)
employee[i]=new YearWorker();
System.out.println("How do you do");
}
public void averageHeight(){
System.out.println("American Average height: 188.0 cm");
}
public void averageWeight(){
JDK1.5Winxp
三、实验步骤与方法
实验1
编写一个Java应用程序,除了主类外,该程序中还有4个类:People、ChinaPeople、AmericanPeople类。该程序具体要求如下:
People类有访问权限是protected的double型变量:height和weight,以及public void speakHello()、public void averageHeight()和public void averageWeight()方法。
Βιβλιοθήκη Baidu实验2
要求有一个abstract类,类名为Employee。Employee的子类有YearWorker、MonthWorker和WeekWorker。YearWorker对象按年领取薪水,MonthWorker按月领取薪水,WeekWorker按周领取薪水。Employee类有个abstract方法:
beijingPeople.speakHello( );
chinaPeople.averageHeight( );
americanPeople.averageHeight( );
beijingPeople.averageHeight( );
chinaPeople.averageWeight( );
americanPeople.averageWeight( );
{
public double computeWeight();
}
class Television implements ComputerWeight
{
public double computeWeight()
{
return 10;//实现computeWeight()方法
}
}
class Computer implements ComputerWeight
实验3
卡车要装载一批货物,货物有3种商品:电视、计算机和洗衣机。需要计算出大货车和小货车各自做载重的3种货物的总重量。
要求有一个ComputerWeight()接口,该接口中有一个方法:
Public double computeWeight()
有3个实现该接口的类:Telvision、Computer和WashMachine。这3类通过实现接口computerTotalSales给出自重。
}
}
class Car
{
ComputerWeight[] goods;
double totalWeights=0;
Car(ComputerWeight[] goods)
{
this.goods=goods;
}
public double getTotalWeights()
{
totalWeights=0;
System.out.println("average height:"+height);
}
public void averageWeight( ) {
weight=70;
System.out.println("average weight:"+weight);
}
}
class ChinaPeople extends People {
深圳大学
实验报告
课程名称:Java
实验序号:实验2
实验名称:继承与接口
班级:计算机3姓名:卢志敏
同组人:实验日期:2008年11月16日
教师签字:
一、实验目的
1.继承
子类的继承性
子类对象的创建过程
成员变量的继承与隐藏
方法的继承与重写
2.上转型对象
掌握上转型对象的使用
3.接口回调
掌握接口回调技术
二、实验环境
{
public double computeWeight()
{
return 15;//实现computeWeight()方法
}
}
class WashMachine implements ComputerWeight
{
public double computeWeight()
{
return 30.5;//实现computeWeight()方法
public void speakHello(){
System.out.println("你好,吃了吗");
}
public void averageHeight(){
System.out.println("中国人的平均身高:173.0厘米");
}
public void averageWeight(){
}
}
运行效果截屏:
实验2
源代码:abstract class Employee
{
public abstract double earnings( );
}
class YearWorker extends Employee
{
public double earnings( ){
return 50000 ;
System.out.println("中国人的平均体重:67.34公斤");
}
public void chinaGongfu() {
System.out.println("坐如钟,站如松,睡如弓");
}
}
class AmericanPeople extends People {
public void speakHello(){
System.out.println("American Average weight: 80.23 kg");
}
public void americanBoxing() {
System.out.println("直拳钩拳");
}
}
class BeijingPeople extends ChinaPeople {
有一个Car类,该类用ComputeWeight接口类型的数组作为成员,那么该数组的单元就可以存放Television对象的引用、Computer对象的引用或WashMachine对象引用。程序能输出Car对象所能装载的货物的总重量。
按程序模板的要求编写源文件,要特别注意程序的输出结果,并能正确解释输出的结果。
}
Company company=new Company(employee);
System.out.println("公司年工资总额:"+ company.salariesPay());//调用Company对象的方法输出工资总额
}
}
运行截屏:
实验3
源代码:interface ComputerWeight
ChinaPeople类是People的子类,新增了public void chinaGongfu()方法。要求ChinaPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。
}
}
class Company
{
Employee[] employee;
double salaries;
Company (Employee[] employee)
{
this.employee=employee;
}
public double salariesPay()
{
salaries=0;
for(int i=0;i<employee.length;i++)
{
ComputerWeight[] goodsOne=new ComputerWeight[50],goodsTwo=new ComputerWeight[22];
}
public void beijingOpera() {
System.out.println("京剧术语");
}
}
public class Example {
public static void main(String args[ ]) {
ChinaPeople chinaPeople=new ChinaPeople( );
实验1
源代码:class People {
protected double weight,height;
public void speakHello( ) {
System.out.println("yayawawa");
}
public void averageHeight() {
height=173;
AmericanPeople americanPeople=new AmericanPeople( );
BeijingPeople beijingPeople=new BeijingPeople( );
chinaPeople.speakHello( );
americanPeople.speakHello( );
BeijingPeople是ChinaPelple的子类,新增public void beijingOpera()方法。要求ChinaPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。
for(int i=0;i<goods.length;i++)
{
totalWeights+=goods[i].computeWeight();
}
return totalWeights;
}
}
public class Road
{
public static void main(String[] args)
public void speakHello(){
System.out.println("您好");
}
public void averageHeight(){
System.out.println("北京人的平均身高: 167.0 cm");
}
public void averageWeight(){
System.out.println("北京人的平均体重: 68.5 kg");
Public abstract earnings();
子类必须重写父类的earnings()方法,给出各自领取薪水的具体方式。
有一个Company类,该类用Employee数组作为成员,Employee数组的单元可以是YearWorker对象的上转型对象、MonthWorker对象的上转型对象、WeekWorker对象的上转型对象。程序能输出Company对象一年需要支付的薪水总额。
{
salaries=salaries+employee[i].earnings();
}
return salaries;
}
}
public class HardWork
{
public static void main(String args[])
{
Employee[] employee=new Employee[20];
beijingPeople.averageWeight( );
chinaPeople.chinaGongfu( );
americanPeople.americanBoxing( );
beijingPeople.beijingOpera( ) ;
beijingPeople.chinaGongfu( );
}
}
class MonthWorker extends Employee
{
public double earnings( ){
return 12*2300;
}
}
class WeekWorker extends Employee
{
public double earnings( ){
return 48*500;
AmericanPeople类是People的子类,新增public void americanBoxing()方法。要求AmericanPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。
for(int i=0;i<employee.length;i++)
{
if(i%3==0)
employee[i]=new WeekWorker();
else if(i%3==1)
employee[i]=new MonthWorker();
else if(i%3==2)
employee[i]=new YearWorker();
System.out.println("How do you do");
}
public void averageHeight(){
System.out.println("American Average height: 188.0 cm");
}
public void averageWeight(){
JDK1.5Winxp
三、实验步骤与方法
实验1
编写一个Java应用程序,除了主类外,该程序中还有4个类:People、ChinaPeople、AmericanPeople类。该程序具体要求如下:
People类有访问权限是protected的double型变量:height和weight,以及public void speakHello()、public void averageHeight()和public void averageWeight()方法。
Βιβλιοθήκη Baidu实验2
要求有一个abstract类,类名为Employee。Employee的子类有YearWorker、MonthWorker和WeekWorker。YearWorker对象按年领取薪水,MonthWorker按月领取薪水,WeekWorker按周领取薪水。Employee类有个abstract方法:
beijingPeople.speakHello( );
chinaPeople.averageHeight( );
americanPeople.averageHeight( );
beijingPeople.averageHeight( );
chinaPeople.averageWeight( );
americanPeople.averageWeight( );
{
public double computeWeight();
}
class Television implements ComputerWeight
{
public double computeWeight()
{
return 10;//实现computeWeight()方法
}
}
class Computer implements ComputerWeight
实验3
卡车要装载一批货物,货物有3种商品:电视、计算机和洗衣机。需要计算出大货车和小货车各自做载重的3种货物的总重量。
要求有一个ComputerWeight()接口,该接口中有一个方法:
Public double computeWeight()
有3个实现该接口的类:Telvision、Computer和WashMachine。这3类通过实现接口computerTotalSales给出自重。
}
}
class Car
{
ComputerWeight[] goods;
double totalWeights=0;
Car(ComputerWeight[] goods)
{
this.goods=goods;
}
public double getTotalWeights()
{
totalWeights=0;
System.out.println("average height:"+height);
}
public void averageWeight( ) {
weight=70;
System.out.println("average weight:"+weight);
}
}
class ChinaPeople extends People {
深圳大学
实验报告
课程名称:Java
实验序号:实验2
实验名称:继承与接口
班级:计算机3姓名:卢志敏
同组人:实验日期:2008年11月16日
教师签字:
一、实验目的
1.继承
子类的继承性
子类对象的创建过程
成员变量的继承与隐藏
方法的继承与重写
2.上转型对象
掌握上转型对象的使用
3.接口回调
掌握接口回调技术
二、实验环境
{
public double computeWeight()
{
return 15;//实现computeWeight()方法
}
}
class WashMachine implements ComputerWeight
{
public double computeWeight()
{
return 30.5;//实现computeWeight()方法
public void speakHello(){
System.out.println("你好,吃了吗");
}
public void averageHeight(){
System.out.println("中国人的平均身高:173.0厘米");
}
public void averageWeight(){
}
}
运行效果截屏:
实验2
源代码:abstract class Employee
{
public abstract double earnings( );
}
class YearWorker extends Employee
{
public double earnings( ){
return 50000 ;
System.out.println("中国人的平均体重:67.34公斤");
}
public void chinaGongfu() {
System.out.println("坐如钟,站如松,睡如弓");
}
}
class AmericanPeople extends People {
public void speakHello(){
System.out.println("American Average weight: 80.23 kg");
}
public void americanBoxing() {
System.out.println("直拳钩拳");
}
}
class BeijingPeople extends ChinaPeople {
有一个Car类,该类用ComputeWeight接口类型的数组作为成员,那么该数组的单元就可以存放Television对象的引用、Computer对象的引用或WashMachine对象引用。程序能输出Car对象所能装载的货物的总重量。
按程序模板的要求编写源文件,要特别注意程序的输出结果,并能正确解释输出的结果。
}
Company company=new Company(employee);
System.out.println("公司年工资总额:"+ company.salariesPay());//调用Company对象的方法输出工资总额
}
}
运行截屏:
实验3
源代码:interface ComputerWeight
ChinaPeople类是People的子类,新增了public void chinaGongfu()方法。要求ChinaPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。
}
}
class Company
{
Employee[] employee;
double salaries;
Company (Employee[] employee)
{
this.employee=employee;
}
public double salariesPay()
{
salaries=0;
for(int i=0;i<employee.length;i++)
{
ComputerWeight[] goodsOne=new ComputerWeight[50],goodsTwo=new ComputerWeight[22];
}
public void beijingOpera() {
System.out.println("京剧术语");
}
}
public class Example {
public static void main(String args[ ]) {
ChinaPeople chinaPeople=new ChinaPeople( );