码迷,mamicode.com
首页 > 编程语言 > 详细

Java类和对象编程示例

时间:2018-10-24 22:02:40      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:oct   date   src   inter   math   super   运行   rabl   java   

一、Java类的定义和对象的创建

1、定义一个Circle圆类。

(1)一个名为radius的double型数据域,表示圆的半径,其默认值为1。

(2)创建默认圆的无参构造方法。

(3)一个创建radius为指定值的圆的构造方法。

(4)数据域radius的访问器和修改器。

(5)一个名为getArea()的方法返回这个圆的面积。

(6)一个名为getPerimeter()的方法返回周长。

技术分享图片
class Circle {
    private double radius=1;
    public Circle() {
    }
    public Circle(double radius) {
        this.radius = radius;
    }
    public void setRadius(double radius) {
        this.radius = radius;
    }
    public double getRadius() {
        return radius;
    }
    public double getArea() {
        return Math.PI*radius*radius;
    }
    public double getPerimeter() {
        return Math.PI*radius*2;
    }
}
Circle类代码

2、定义一个Rectangle矩形类

(1)两个名为width和height的double型数据域,它们分别表示矩形的宽和高。width和height的默认值都为1。

(2)创建默认矩形的无参构造方法。

(3)一个创建width和height为指定值的矩形的构造方法。

(4)数据域width和height的访问器和修改器。

(4)一个名为getArea()的方法返回这个矩形的面积。

(5)一个名为getPerimeter()的方法返回周长。

技术分享图片
class Rectangle {
    private double width=1.0;
    private double height=1.0;
    public Rectangle() {
    }
    public Rectangle(double width,double height) {
        this.width = width;
        this.height = height;
    }
    public void setWidth(double width) {
        this.width = width;
    }
    public double getWidth(double width) {
        return width;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public double getHeight(double Height) {
        return height;
    }
    public double getArea() {
        return width*height;
    }
    public double getPerimeter() {
        return 2*(width+height);
    }
}
Rectangle类代码

3、创建一个半径为10的Circle,输出其面积和圆周长;创建一个长为4,宽为6的Rectangle,输出其面积和周长。

技术分享图片
public class Test{
    public static void main(String[] args) {
        Circle circle = new Circle(10);
        System.out.printf("The area of Circle  is %.2f, The perimeter of Circle is %.2f\n",circle.getArea(),circle.getPerimeter());
        Rectangle rectangle = new Rectangle(4,6);
        System.out.printf("The area of Rectangle  is %.2f, The perimeter of Circle is %.2f\n",rectangle.getArea(),rectangle.getPerimeter());

    }
}
测试代码

运行结果:

The area of Circle is 314.16, The perimeter of Circle is 62.83
The area of Rectangle is 24.00, The perimeter of Circle is 20.00

二、Java类的继承和多态

1、定义一个GeometricObject类。

(1)一个名为color的String型数据域,表示几何对象的颜色,默认值为white。一个名为filled的boolean型数据,表示是否着色。一个名为dateCreated的Date型数据域,表示几何对象创建时间。

(2)创建默认几何对象的无参构造方法。

(3)一个创建带特定颜色和填充值的构造方法。

(4)数据域color和filled的访问器和修改器。dateCreated的访问器。

(5)增加一个equalArea()方法,比较两个几何对象的面积是否相等。

技术分享图片
import java.util.Date;
class GeometricObject {
    private String color = "white";
    private boolean filled =false;
    private Date dateCreated;

    public GeometricObject() {
        dateCreated = new Date();
    }

    public GeometricObject(String color, boolean filled) {
        this.color = color;
        this.filled = filled;
        dateCreated = new Date();
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    public Date getDateCreated() {
        return dateCreated;
    }
    public double getArea() {return 1;}
    public boolean equalArea(GeometricObject o) {
        return  (getArea()==o.getArea());
    }
}
GeometricObject类的定义

 

2、改写Circle类和Rectangl类继承GeometricObject类,并分别重写toString()方法返回这个对象的字符串描述。

技术分享图片
class Circle extends GeometricObject{
    private double radius=1;
    public Circle() {
    }
    public Circle(double radius) {
        this.radius = radius;
    }
    public void setRadius(double radius) {
        this.radius = radius;
    }
    public double getRadius() {
        return radius;
    }
    public double getArea() {
        return Math.PI*radius*radius;
    }
    public double getPerimeter() {
        return Math.PI*radius*2;
    }
    @Override
    public String toString() {
        return "This is a Circle created at +"+super.getDateCreated()+"\nradius = "+radius+", area = "+getArea();
    }
}


class Rectangle extends GeometricObject{
    private double width=1.0;
    private double height=1.0;
    public Rectangle() {
    }
    public Rectangle(double width,double height) {
        this.width = width;
        this.height = height;
    }
    public void setWidth(double width) {
        this.width = width;
    }
    public double getWidth(double width) {
        return width;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public double getHeight(double Height) {
        return height;
    }
    public double getArea() {
        return width*height;
    }
    public double getPerimeter() {
        return 2*(width+height);
    }
    @Override
    public String toString() {
        return "This is a rectangle created at +"+super.getDateCreated()+"\nwidth = "+width+", height = "+height+", area = "+getArea();
    }
}
Circle类和Rectangle类

3、创建一个半径为10的Circle,创建一个长为4,宽为6的Rectangle,调用toString()方法分别输出两个对象的描述,并比较这两个对象的面积是否相等。

技术分享图片
public class Test{
    public static void main(String[] args) {
        GeometricObject circle = new Circle(10);
        GeometricObject rectangle = new Rectangle(4,6);
        System.out.println(circle.toString());
        System.out.println(rectangle.toString());
        System.out.println("the area equal? : "+circle.equalArea(rectangle));
    }
}
测试代码

运行结果:

This is a Circle created at +Wed Oct 24 15:20:39 CST 2018
radius = 10.0, area = 314.1592653589793
This is a rectangle created at +Wed Oct 24 15:20:39 CST 2018
width = 4.0, height = 6.0, area = 24.0
the area equal? : false

三、Java类的抽象和接口

 1、改写GeometricObject类为抽象类。

(1)定义一个抽象方法getArea()方法。

(2)定义一个抽象方法getPerimeter()方法。

技术分享图片
import java.util.Date;
abstract class GeometricObject {
    private String color = "white";
    private boolean filled =false;
    private Date dateCreated;

    public GeometricObject() {
        dateCreated = new Date();
    }

    public GeometricObject(String color, boolean filled) {
        this.color = color;
        this.filled = filled;
        dateCreated = new Date();
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public abstract double getArea();
    public abstract double getPerimeter();

    public boolean equalArea(GeometricObject o) {
        return  (getArea()==o.getArea());
    }
}
GeometricObject抽象类
技术分享图片
class Circle extends GeometricObject{
    private double radius=1;
    public Circle() {
    }
    public Circle(double radius) {
        this.radius = radius;
    }
    public void setRadius(double radius) {
        this.radius = radius;
    }
    public double getRadius() {
        return radius;
    }
     @Override
    public double getArea() {
        return Math.PI*radius*radius;
    }
     @Override
    public double getPerimeter() {
        return Math.PI*radius*2;
    }
    @Override
    public String toString() {
        return "This is a Circle created at +"+super.getDateCreated()+"\nradius = "+radius+", area = "+getArea();
    }
}
覆盖抽象方法的Circle类
技术分享图片
class Rectangle extends GeometricObject{
    private double width=1.0;
    private double height=1.0;
    public Rectangle() {
    }
    public Rectangle(double width,double height) {
        this.width = width;
        this.height = height;
    }
    public void setWidth(double width) {
        this.width = width;
    }
    public double getWidth(double width) {
        return width;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public double getHeight(double Height) {
        return height;
    }
    @Override
    public double getArea() {
        return width*height;
    }
    @Override
    public double getPerimeter() {
        return 2*(width+height);
    }
    @Override
    public String toString() {
        return "This is a rectangle created at +"+super.getDateCreated()+"\nwidth = "+width+", height = "+height+", area = "+getArea();
    }
}
覆盖抽象方法的Rectangle类

2、设计一个GeometricInterface接口,其中包含getArea()方法和getPerimeter()方法。

技术分享图片
public interface GeometricInterface {
    double getArea();
    double getPerimeter();

}
GeometricInterface接口类

3、改写GeometricObject类,实现GeometricInterface接口和Compareble接口。

技术分享图片
import java.util.Date;
abstract class GeometricObject implements GeometricInterface,Comparable<GeometricObject>{

    private String color = "white";
    private boolean filled =false;
    private Date dateCreated;

    public GeometricObject() {
        dateCreated = new Date();
    }

    public GeometricObject(String color, boolean filled) {
        this.color = color;
        this.filled = filled;
        dateCreated = new Date();
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    public Date getDateCreated() {
        return dateCreated;
    }
 @Override
    public int compareTo(GeometricObject g) {
        if (getArea()>g.getArea()) return 1;
        else if (getArea()<g.getArea()) return -1;
        else return 0;
    }
}
实现接口的Geo类metricObject

4、分别创建一个半径为10的圆c1、半径为1的圆c2,半径为0.5的圆C3,创建一个长为1,宽为Math.PI的矩形r,输出c1和r,c2和r,c3和r的面积比较结果。

技术分享图片
public class Test{
    public static void main(String[] args) {
        GeometricObject c1 = new Circle(10);
        GeometricObject c2 = new Circle(1);
        GeometricObject c3 = new Circle(0.5);
        GeometricObject r = new Rectangle(1,Math.PI);

        System.out.println("c1:r  is "+c1.compareTo(r));
        System.out.println("c2:r  is "+c2.compareTo(r));
        System.out.println("c3:r  is "+c3.compareTo(r));
    }
}
测试代码

运行结果:

c1:r is 1
c2:r is 0
c3:r is -1

Java类和对象编程示例

标签:oct   date   src   inter   math   super   运行   rabl   java   

原文地址:https://www.cnblogs.com/yinweifeng/p/9843812.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!