首页 > 代码库 > 0922继承,练习题目-作业

0922继承,练习题目-作业

 

1.实现如下类之间的继承关系,并编写Music类来测试这些类。

技术分享技术分享

 

package workhome0922休息;public class People {	protected double height;	protected double weight;		public double getHeight() {		return height;	}	public void setHeight(double height) {		this.height = height;	}	public double getWeight() {		return weight;	}	public void setWeight(double weight) {		this.weight = weight;	}	//方法	public void speakHello()	{		System.out.print("我是一个人"+"  ");	}	public void averageHeight()	{		System.out.print("我有身高"+"  ");	}	public void averageWeight()	{		System.out.println("我有体重"+"  ");	}	}package workhome0922休息;public class ChinaPeople extends People {	public void speakHello()	{		System.out.print("中国人"+"  ");	}	public double averageHeight(double height)	{//		return height;		this.height=height;//为什么要把height赋给父类的height??????		return this.height;		}	public double averageWeight(double weight)	{		this.weight=weight;		return this.weight;			}	public void chinaGongFu()	{		System.out.println("中国功夫"+"太极拳");	}}package workhome0922休息;public class AmericanPeople extends People {	public void speakHello()	{		System.out.print("美国人");	}	public double averageHeight(double height)	{		this.height=height;		return this.height;	}	public double averageWeight(double weight)	{		this.weight=weight;		return this.weight;	}	public void americanBoxing()	{		System.out.println("美国功夫:"+"直拳");	}}package workhome0922休息;public class Peopletest {	public static void main(String[] args) {		//实例化人		People p=new People();		p.speakHello();		p.averageHeight();		p.averageWeight();		//实例化中国人		ChinaPeople c=new ChinaPeople();		c.speakHello();		System.out.print(""+"身高:"+c.averageHeight(171.2));		System.out.print("  "+"体重:"+c.averageWeight(125)+"  ");		c.chinaGongFu();		//实例化美国人		AmericanPeople a=new AmericanPeople();		a.speakHello();		System.out.print("  "+"身高:"+a.averageHeight(180));		System.out.print("  "+"体重:"+a.averageWeight(160)+"  ");		a.americanBoxing();			}}

技术分享

2.创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople

AmericanPeople类重写父类的三个方法)。

技术分享

package workhome0922休息;public class Instrument {	//方法-输出:弹奏乐器	public void play()	{		System.out.println("弹奏乐器");	}}package workhome0922休息;public class Wind extends Instrument {	//方法1-输出:弹奏Wind	public void play()	{		System.out.println("弹奏Wind");	}	//方法2-输出:调用Wind的play2	public void play2()	{		System.out.println("调用Wind的play2");	}}package workhome0922休息;public class Brass extends Instrument {	//方法1-输出:弹奏brass	public void play()	{		System.out.println("弹奏brass");	}	//方法2-输出:调用brass的play2	public void play2()	{		System.out.println("调用brass的play2");	}}package workhome0922休息;public class Music {	//调用对象i的play方法	public static void tune(Instrument i)	{		i.play();	}		public static void main(String[] args) {				Wind w=new Wind();		Music.tune(w);		w.play2();		Brass b=new Brass();		Music.tune(b);	}}

技术分享

3.编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类E

要求:

(1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak()

方法,在speak方法中输出“咿咿呀呀......”的信息。

(2)People类是Monkey类的子类,在People类中重写方法speak(),speak方法

中输出“小样的,不错嘛!会说话了!”的信息。

(3)People类中新增方法void think(),在think方法中输出“别说话!认真思考!”的信息。

(4)在主类Emain方法中创建MonkeyPeople类的对象类测试这2个类的功能。

 

package workhome2;public class Monkey {	//构造方法	Monkey(String s)	{		super();		System.out.println(s+1);	}	Monkey(int ss)	{		System.out.println(ss);	}	//方法	public void speak()	{		System.out.println("咿咿呀呀......");	}}package workhome2;public class People extends Monkey {	//继承构造方法	public People(String s) {		super(s);		System.out.println(s+10);			}	//重写父类方法speak	public void speak()	{		System.out.println("小样的,不错嘛!会说话了");	}	//新增方法	public void think()	{		System.out.println("别说话!认真思考!");	}}package workhome2;public class E {	public static void main(String[] args) {		//创建Monkey对象		Monkey m=new Monkey("10");		m.speak();		//创建People对象		People p=new People("10");		p.speak();		p.think();	}}

技术分享

4.定义类Human,具有若干属性和功能;定义其子类ManWoman

在主类Test中分别创建子类、父类和上转型对象,并测试其特性。

 

package workhome3;public class Human {	//人的属性	public String leg;	public String foot;	public String getLeg() {		return leg;	}	public void setLeg(String leg) {		this.leg = leg;	}	public String getFoot() {		return foot;	}	public void setFoot(String foot) {		this.foot = foot;	}	//人的方法	public void run()	{		System.out.println("可以跑");	}	public void jump()	{		System.out.println("可以跳");	}	public int Int(int a)	{		a=a+1;		return a;	}}package workhome3;public class Man extends Human {	//男人的方法		public void run()		{			System.out.println("可以跑快");		}		public void jump()		{			System.out.println("可以跳高");		}		public void eat()		{			System.out.println("吃饭睡觉打豆豆");		}	}package workhome3;public class Woman extends Human {	//女人的方法	public void run()	{		System.out.println("可以跑慢");	}	public void jump()	{		System.out.println("可以跳矮");	}}package workhome3;public class humantest {	public static void main(String[] args) {				//向上转型		Human h1=new Man();		h1.setFoot("人有两条腿");		System.out.println(h1.getFoot());		h1.run();		System.out.println(h1.Int(10));		//向下转型		Man m1=(Man)h1;		m1.setFoot("男人有两条比女人长的腿");		System.out.println(m1.getFoot());		m1.jump();		System.out.println(m1.Int(10));		m1.eat();	}}

技术分享

6.按要求编写一个Java应用程序:

1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。

2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。

3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。

 

package workhome4;public class jivxing {//	包含有长、宽两种属性,和计算面积方法。	private double chang;	private double kuan;	public double getChang() {		return chang;	}	public void setChang(double chang) {		this.chang = chang;	}	public double getKuan() {		return kuan;	}	public void setKuan(double kuan) {		this.kuan = kuan;	}	//方法	public double mianji(double chang,double kuan)	{		this.chang=chang;		this.kuan =kuan;		return this.chang*this.kuan;	}}package workhome4;public class jivxing2 extends jivxing {	private double gao;		public double getGao() {		return gao;	}	public void setGao(double gao) {		this.gao = gao;	}	public double tiji(double chang,double kuan,double gao)	{		this.gao=gao;		return super.mianji(chang, kuan)*this.gao;			}}package workhome4;public class test {	public static void main(String[] args) {		// TODO Auto-generated method stub		jivxing2 j=new jivxing2();		System.out.println(j.mianji(1, 2));		System.out.println(j.tiji(1, 2, 3));	}}

技术分享

7. 

编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数

wheels和车重weight。小车类CarVehicle的子类,其中包含的属性有载人数

loader。卡车类TruckCar类的子类,其中包含的属性有载重量payload。每个

类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功

能。

 

package workhome4;public class Vehicle {	//wheels和车重weight	private int wheel;//轮胎数量	private double weight;//车重		public int getWheel() {		return wheel;	}	public void setWheel(int wheel) {		this.wheel = wheel;	}	public double getWeight() {		return weight;	}	public void setWeight(double weight) {		this.weight = weight;	}	//构造方法	public Vehicle(int wheel,double weight)	{		super();		this.wheel=wheel;		this.weight=weight;			}	//方法	public void vv()	{		System.out.print("轮胎数:"+this.wheel+"  "+"车重:"+this.weight+"  ");	}}package workhome4;public class Car extends Vehicle {		private int loader;//载人数	public int getLoader() {		return loader;	}	public void setLoader(int loader) {		this.loader = loader;	}	public Car(int wheel,double weight,int loader) {		super(wheel,weight);		this.loader=loader;			}	//方法	public void cc()	{		System.out.println();		super.vv();		System.out.print("和载人数"+this.loader+"  ");	}	}package workhome4;public class Truck extends Car {	private double payload;//载重量	public double getPayload() {		return payload;	}	public void setPayload(double payload) {		this.payload = payload;	}	public Truck(int wheel, double weight, int loader, double payload) {		super(wheel, weight, loader);		this.payload=payload;	}//方法	public void tt()	{		super.cc();		System.out.println("载重量"+this.payload+"  ");	}		}package workhome4;public class cartest {	public static void main(String[] args) {			Vehicle v=new Vehicle(4,99999);			v.vv();			Car c=new Car(4,888888,3);			c.cc();			Truck t=new Truck(4,777777,6,90000.888);			t.tt();		}		}

技术分享

8.编写一个Shape类,具有属性:周长和面积;

定义其子类三角形和矩形,分别具有求周长的方法。

定义主类E,在其main方法中创建三角形和矩形类的对象,

并赋给Shape类的对象ab,使用对象ab来测试其特性。

 

 

 

 

0922继承,练习题目-作业