继承相关作业

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

package zuoye;

//乐器
public class Instrument {

	//属性
	private String name;//乐器的名字

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	//构造方法
	public Instrument(String name) {
		super();
		this.name = name;
	}
	
	public Instrument() {
		super();
	}

	public void play()
	{
		System.out.println("弹奏乐器");
	}
	
	
}

  

package zuoye;

public class Wind extends Instrument
//没有为缺省构造函数定义隐式超构造函数 Instrument()。必须定义显式构造函数
//必须在 Instrument中构造无参无返回值的构造方法,否则会出现上面情况
{ 
	//重写
	public void play()
	{
		System.out.println("弹奏wind");
	}

	public void play2()
	{
		System.out.println("调用wind的play2");
	}
	
	
}

  

package zuoye;

public class Brass extends Instrument 
{

	public void play()
	{
		System.out.println("弹奏brass");
	}
	public void play2()
	{
		System.out.println("调用brass的play2");
	}
}

  

package zuoye;

public class Music {

	public static void main(String[] args)
	{
		Instrument a=new Instrument();
		tune(a);
			Wind w=new Wind();
			Brass b=new Brass();
			w.play();
			w.play2();
			b.play();
			b.play2();
	}
	public static void tune (Instrument i)

	{
		i.setName("吉他");
		i.play();
		System.out.println(i.getName());
	}
	
}

  

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

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

package zuoye2;

public class People 
{
	protected double height;
	protected double weight;
	private String guojia;
	
	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 String getGuojia() {
		return guojia;
	}
	public void setGuojia(String guojia) {
		this.guojia = guojia;
	}
	
	
	public People() {
		super();
	}
	public People(double height, double weight) {
		super();
		this.height = height;
		this.weight = weight;
	}
	
	public  void speackHello()
	{
		System.out.println("Hello");
	}
	public  void averageHeight()
	{
		System.out.println("平均身高是:");
	}
	public  void averageweight()
	{
		System.out.println("平均体重是:");
	}

}

  

package zuoye2;

public class ChinaPeople extends People {
	
	
	public void chinaGongfu()
	{
		System.out.println("坐如钟,站如松,行如风,睡如弓");
	}
	public  void speackHello()
	{
		System.out.println("你好");
	}

}

  

package zuoye2;

public class AmericanPeople extends People {

	
	public void americanBoxing()
	{
		System.out.println("直拳,勾拳");
	}
}

  

package zuoye2;

public class Ceshi_people {

	public static void main(String[] args) {


		ChinaPeople c=new ChinaPeople();
		
		c.setHeight(180.0);
		c.setWeight(70.0);
		c.setGuojia("中国");
		c.speackHello();
		System.out.println(c.getGuojia());
		c.averageHeight();System.out.println(c.getHeight());
		c.chinaGongfu();
		
		System.out.println("");
		
		AmericanPeople a=new AmericanPeople();
		a.setHeight(175.0);
		a.setWeight(80.0);
		a.setGuojia("美国佬");
		a.speackHello();
		System.out.println(a.getGuojia());
		a.averageHeight();
		System.out.println(a.getHeight());
		a.averageweight();
		System.out.println(a.getWeight());
		a.americanBoxing();
		
		

	}

}

  

21.编写一个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 zuoye3;

public class Peopl extends Monkey {
	
	public void speak()
	{
		System.out.println("小样的,不错嘛!会说话了!!");
	}
	
	void think()
	{
		System.out.println("别说话!认真思考!");
	}

}

  

package zuoye3;

public class Monkey {
	//属性
	
	private String s;
	
	
	public  String getS() {
		return s;
	}

	public void setS(String s) {
		this.s = s;
	}

	//构造方法
	public Monkey() {
		super();
	}

	public Monkey(String s)
	{
		super();
		this.s=s;
	
	}

	public void speak()
	{
		System.out.println("咿咿呀呀。。。。");
	}
	
}

  

package zuoye3;

public class E {

	public static void main(String[] args) {
	
		
		Monkey m1=new Monkey("猴子");
		
		System.out.println(m1.getS());
		m1.speak();

		
		Peopl p=new Peopl();
		p.setS("人类");
		System.out.println(p.getS());
		p.speak();
		p.think();
		
	}

}

  

原文地址:https://www.cnblogs.com/liuyanzeng/p/5897075.html