例子1
1、编写一个Java应用程序,该程序包括3个类:Monkey类、People类和测试类。要求:
(1)Monkey类中有个public void speak()方法,在speak方法中输出“咿咿呀呀。。。。。。”的信息。
(2)People类是Monkey类的子类,在People类中重写方法speak,在speak方法中输出“小样的,不错嘛,会说话了!”的信息。
(3)在People类中新增方法void think(),在think方法中输出“别说话,认真思考!”的信息。
/**
* Monkey 父类
*/
package cn.yjlblog.www;
public class Monkey
{
public void speak()
{
System.out.println("咿咿呀呀。。。。。。");
}
}
/**
* People 子类
*/
package cn.yjlblog.www;
public class People extends Monkey
{
public void speak()
{
System.out.println("小样的,不错嘛,会说话了!");// TODO Auto-generated method stub
}
void think()
{
System.out.println("别说话,认真思考!");
}
}
/**
* TestClass 测试类
*/
package cn.yjlblog.www;
public class TestClass {
public static void main(String[] args) {
Monkey m = new Monkey();
m.speak();
Monkey p = new People();
p.speak();
//Monkey p1 = new People();//The method think() is undefined for the type Monkey
People p1 = new People();
p1.think();
}
}
例子2
2、按要求编写一个Java应用程序:
(1)定义一个类(Rectangle),描述一个矩形,包含有长、宽两种属性和计算面积的方法。
(2)定义一个类(Cuboid),继承自矩形类,同时该类描述长方体,具有长、宽、高属性和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。
/**
* Rctangle 父类
*/
package cn.yjlblog.www;
public class Rectangle
{
private double length;
private double width;
//生成set 和get 方法
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
//构造含有参数的方法
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
//求面积
public double Aera()
{
return length * width;
}
}
/**
* Cuboid 子类
*/
package cn.yjlblog.www;
public class Cuboid extends Rectangle
{
private double height;
private double volume;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getVolume() {
return volume;
}
public void setVolume(double volume) {
this.volume = volume;
}
public Cuboid(double length, double width, double height) {
super(length, width);
this.height = height;
}
}
/**
* TestClass 测试类
*/
package cn.yjlblog.www;
public class TestClass {
public static void main(String[] args) {
Cuboid rct = new Cuboid(10,20,30);
double v = rct.Aera()*rct.getHeight();
double s = rct.Aera();
System.out.println("The Rctangle's volume is:"+v);
System.out.println("The Rctangle's floor space is:"+s);
}
}
运行结果
The Rctangle's volume is:6000.0
The Rctangle's floor space is:200.0
在这个例子中,我觉的并没有实现题目中的效果...enmmmm,以为题目中(2)中说,要求写出计算体积的方法,但是在Cuboid中不会写......于是把体积 的计算方法写在了测试类中....
例子3
3、按要求编写一个Java应用程序:
(1)编写一个Shape类,具有属性周长(perimeter)和面积(area);
(2)定义其子类三角形(Triangle)和矩形(Rectangle),分别具有求周长和面积的方法。
(3)定义测试类,在其main方法中创建三角形和矩形类的对象,并赋给Shape类的对象a和b,使用对象a、b来测试其特性。
/**
* Shape 父类
*/
package cn.yjlblog.www;
public class Shape {
private double perimeter;
private double area;
//get set 方法
public double getPerimeter() {
return perimeter;
}
public void setPerimeter(double perimeter) {
this.perimeter = perimeter;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
//构造方法
public Shape(double perimeter, double area) {
this.perimeter = perimeter;
this.area = area;
}
}
/**
* Triangle 子类
*/
package cn.yjlblog.www;
public class Triangle extends Shape {
public Triangle(double perimeter, double area) {
super(perimeter, area);
// TODO Auto-generated constructor s
}
private double a1;
private double a2;
private double a3;
//set get 方法
public double getA1() {
return a1;
}
public void setA1(double a1) {
this.a1 = a1;
}
public double getA2() {
return a2;
}
public void setA2(double a2) {
this.a2 = a2;
}
public double getA3() {
return a3;
}
public void setA3(double a3) {
this.a3 = a3;
}
public double perimeter()
{
return a1+a2+a3;
}
public double area()
{
double s1=(a1+a2+a3)/2;
double s2 = s1*(s1-a1)*(s1-a2)*(s1-a3);
double result = Math.sqrt(s2);
return result;
}
}
package cn.yjlblog.www;
/**
* Rectangle 子类
*/
public class Rectangle extends Shape{
public Rectangle(double perimeter, double area) {
super(perimeter, area);
// TODO Auto-generated constructor stub
}
private double b1;
private double b2;
public double getB1() {
return b1;
}
public void setB1(double b1) {
this.b1 = b1;
}
public double getB2() {
return b2;
}
public void setB2(double b2) {
this.b2 = b2;
}
public double perimeter()
{
return (b1+b2)*2;
}
public double area()
{
return b1*b2;
}
}
/**
* TestClass 测试类
*/
package cn.yjlblog.www;
public class TestClass {
public static void main(String[] args) {
Triangle a = new Triangle(0, 0);
a.setA1(3);
a.setA2(4);
a.setA3(5);
System.out.println(a.perimeter());
System.out.println(a.area());
Rectangle b = new Rectangle(0, 0);
b.setB1(3);
b.setB2(4);
System.out.println(b.perimeter());
System.out.println(b.area());
}
}
运行结果
12.0 //三角形周长
6.0 //三角形面积
14.0 //长方形周长
12.0 //长方形面积
这个题目总感觉自己的父类没有用上...enmmm..可能自己的关于java的语法还是.....再想想。