java 第八周 继承 接口

来源:互联网 发布:淘宝文胸买家秀图片 编辑:程序博客网 时间:2024/06/06 14:50
/* (程序头部注释开始)* 程序的版权和版本声明部分* Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved.* 文件名称:                              * 作    者:   姜雅明                              * 完成日期:   2012  年  10  月  18  日* 版 本 号:   2.0       * 对任务及求解方法的描述部分* 输入描述: * 问题描述: * 程序输出: * 程序头部的注释结束*/
/*封装一个People类型,具有height和weight属性, *具有speakHello、averageHeight、averageWeight功能。 */public class People {protected int height;protected int weight;People(){height = 180;weight = 65;}public void speakHello(){System.out.println("Hello");}public void averageHeight(){System.out.println("the averageHeight is:" + height);}public void averageWeight(){System.out.println("the averageWeight is:" + weight);}}
/*封装一类ChinaPeople类型是People的子类,新增chinaMartial功能, * override超类的speakHello、averageHeight、averageWeight功能。 */public class ChinaPeople extends People{ChinaPeople(){super();this.height = 175;this.weight = 55;}public void chinaMartial(){System.out.println("中国爱好和平。");}public void speakHello(){System.out.println("你好");}public void averageHeight(){//System.out.println(super.height);System.out.println("中国人的平均身高是:" + height);}public void averageWeight(){System.out.println("中国人的平均体重是:" + weight);}}


 

/*封装一类AmericanPeople类型是People的子类, * 新增AmericanBoxing功能, * override超类的speakHello、averageHeight、averageWeight功能。 */public class AmericanPeople extends People{AmericanPeople(){super();}public void AmericanBoxing(){System.out.println("This is America Boxing!!!");}public void speakHello(){System.out.println("Hello,I'm from America.");}public void averageHeight(){System.out.println("The averageheight of America is :" + height);}public void averageWeight(){System.out.println("The averageweight of America is :" + weight);}}

 

/*封装一类BeijingPeople类型是ChinaPeople的子类, * 新增BeijingOpera功能, * override超类的speakHello、averageHeight、averageWeight功能。 */public class BeijingPeople extends ChinaPeople{BeijingPeople(){super();}public void BeijngOpera(){System.out.println("国粹京剧");}public void speakHello(){System.out.println("你好,我是北京人。");}public void averageHeight(){System.out.println("北京人的平均身高是:" + height);}public void averageWeight(){System.out.println("北京人的平均体重是:" + weight);}}

 

public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubPeople p = new People();ChinaPeople c = new ChinaPeople();p.averageHeight();p.averageWeight();c.averageHeight();c.averageWeight();}}


接口练习:

package two;/*封装一类接口ComputerWeight,该接口中有3个功能:double computrWeight,void printName,double printPrice。 */public interface ComputerWeight {double computerWeight();void printName();double printPrice(double price);}


 

package two;/*封装一类接口ComputerCompany,该接口有2个功能:String computerName,void printFunction。 */public interface ComputerCompany {String computerName(String name);void printFunction();}


 

package two;/*封装一类对象FlashMemory实现上述两类接口。 */public class FlashMemory implements ComputerCompany, ComputerWeight{private String name;private double price;public double computerWeight(){double weight = 30;return weight;}public void printName(){System.out.println("The name of the computer is : " + name);}@Overridepublic String computerName(String name) {this.name = name;return this.name;}@Overridepublic void printFunction() {System.out.println("This computer can do everything!!!");}@Overridepublic double printPrice(double price) {this.price = price;return this.price;}}


 

package two;public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFlashMemory f = new FlashMemory();String str = "lenovo";f.computerName(str);f.printName();System.out.println("The weight of the computer is : " + f.computerWeight());f.printFunction();System.out.println("The price of the computer is :" + f.printPrice(7998));}}


好像是计算机组成原理老师说过不喜欢加注释,然后我也发现我不喜欢加注释·····要改,要改·······虽然这次还是没加注释······

 

 

 

 

 


 

原创粉丝点击