java 第八周 继承 接口

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* 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));
	}

}


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

 

 

 

 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值