java 第八周 继承 接口
2012-10-21 16:21
274 查看
/* (程序头部注释开始) * 程序的版权和版本声明部分 * 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 stub People 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); } @Override public String computerName(String name) { this.name = name; return this.name; } @Override public void printFunction() { System.out.println("This computer can do everything!!!"); } @Override public 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 stub FlashMemory 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)); } }
好像是计算机组成原理老师说过不喜欢加注释,然后我也发现我不喜欢加注释·····要改,要改·······虽然这次还是没加注释······
相关文章推荐
- java--类继承和实现的接口中含有相同的方法
- java接口多继承
- Java中继承thread类与实现Runnable接口的区别
- 黑马程序员——Java基础---继承,抽象,接口
- 黑马程序员-java基础 继承、抽象、接口
- Java开发知识之Java的继承多态跟接口*
- 学习笔记——Java核心技术之接口、继承与多态练习题
- JAVA-继承和接口
- Java入门第10章接口继承与多态
- Java 不支持多继承,所以引入接口
- Java - Anonymous Inner Class(匿名内部类)是否可以继承其它类?是否可以实现接口?
- Java 接口 实现和继承关系
- Java 接口和继承
- java的接口可以继承接口吗?
- Java学习笔记(一)----封装 继承 多态 抽象 接口
- Java中泛型插入排序,继承Comparable接口
- 澄清Java语言接口与继承的本质
- java一个实现类如何实现有多个继承的接口?
- java面向对象浅析系列2——抽象类、接口与多继承
- java--抽象类与接口,继承与多态