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

2、编写一个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)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功能。

package xiejie;

public class Monkey {
   Monkey(String s ){
   }
   public void speak() {
	   System.out.println("咿咿呀呀......");
   }
 package xiejie;
public class People extends Monkey{
   People(String s){
    super(s);
	  }
    public void speak() {
    System.out.println("小样的,不错嘛!会说话了!");
  }
    public void think() {
    System.out.println("别说话!认真思考!");  
  }
}
package xiejie;

public class E {

	public static void main(String[] args) {
		Monkey m = new Monkey("圆圆");
		m.speak();
		People p = new People("毛毛");
		p.speak();
		p.think();

	}

}

3、按要求编写一个Java应用程序:

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

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

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

package xiejie;
 
public class juxing1 {
     private double a,b;
     public double getb() {
    	 return b;
     }
     public void setb(double b) {
    	 this.b=b;
     }
     public double geta() {
    	 return a;
     }
     public void seta(double a) {
    	 this.a=a;
     }
     public double s() {
    	 return this.a*this.b;
     }
 }
package xiejie;

public class changfangti extends juxing1{
   private double c;
   public double getc() {
	   return c;
   }
   public void setc(double c) {
	   this.c=c;
   }
   public double v() {
	   return this.geta()*this.getb()*this.getc();
   }
}
 package xiejie;

public class Test {

	public static void main(String[] args) {
		juxing1 j = new juxing1();
		j.seta(10);
		j.setb(10);
		System.out.println("矩形的长为:"+j.geta()+"矩形的宽为:"+j.getb());
		System.out.println("矩形的面积为:"+j.s());
   
		changfangti d = new changfangti();
		d.seta(5);
		d.setb(5);
		d.setc(5.2);
		System.out.println("长方体的长为:"+d.geta()+"长方体的宽为:"+d.getb()+"长方体的高为:"+d.getc());
		System.out.println("长方体的体积为:"+d.v());
	}
}

4、编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功能。

package xiejie;

public class Vehicle {
  private int wheels;
  private double weight;
  public int getwheels() {
	  return wheels;
  }
  public void setwheels() {
	  this.wheels=wheels;
  }
  public double getweight() {
	  return weight;
  }
  public void setweight() {
	  this.weight=weight;
  }
  public Vehicle(int wheels,double weight) {
	  super();
	  this.wheels=wheels;
	  this.weight=weight;
  }
}
package xiejie;

public class Car extends Vehicle{
	private int loader;
    public int getloader() {
    	return loader;
    }
    public void setloader() {
    	this.loader=loader;
    }
    public Car(int wheels,double weight,int loader) {
    	super(wheels,weight);
    	this.loader=loader;
    }
}
package xiejie;

public class Truck extends Car{
    private double payload;
    public double getpayload() {
    	return payload;
    }
    public void setpayload() {
    	this.payload=payload;
    }
	public Truck(int wheels, double weight, int loader,double payload) {
		super(wheels, weight, loader);
		this.payload=payload;
	}
	
package xiejie;

public class TestCar {

	public static void main(String[] args) {
	Vehicle v = new Vehicle(4,4);
	System.out.println("汽车一有"+v.getwheels()+"个轮子,重量为:"+v.getweight());
	Car c = new Car(4,8,5);	
    System.out.println("汽车二有"+c.getwheels()+"个轮子,重量为:"+c.getweight()+",载客量为:"+c.getloader());
    Truck t = new Truck(8,8,5,20);
    System.out.println("汽车三有"+t.getwheels()+"个轮子,重量为:"+t.getweight()+",载客量为:"+t.getloader()+",承受重量为:"+t.getpayload());
	}
}

posted on 2019-05-11 13:59  怀稚  阅读(101)  评论(0编辑  收藏  举报