美文网首页
类和对象例子

类和对象例子

作者: __豆约翰__ | 来源:发表于2024-02-28 17:43 被阅读0次

矩形类

public class Rectangle {
    private int width;
    private int height;

    private String shape;
    Rectangle(){
        width = 10;
        height = 10;
        shape = "#";
    }
    Rectangle(int in_width, int in_height, String in_shape){
        width = in_width;
        height = in_height;
        shape = in_shape;
    }
    int getArea(){
        int result = width * height;
        return result;
    }

    int getRoundLen(){
        int result = (width + height) * 2;
        return result;
    }

    void scaleWidth(double factor){
        if(factor <= 10){
            width *= factor;
        }
        else{
            width *= 10;
        }
    }

    void printSelf(){
        for(int i = 0; i < height; i++){
            for(int j = 0; j < width; j++){
                System.out.print(shape);
            }
            System.out.println();
        }
    }

    void changeShape(String _shape)
    {
        shape = _shape;
    }
}

测试矩形类

import java.util.Random;

public class Main3 {
    public static void main(String[] args) {
//        int a = 33;
//        int a = 90;
        Rectangle rect = new Rectangle(5,3,"@");
        int area = rect.getArea();
        int len = rect.getRoundLen();
        rect.printSelf();
//        rect.width *= 100;
        rect.scaleWidth(2);
        rect.changeShape("#");
        rect.printSelf();

        System.out.println(area);
        System.out.println(len);

        Rectangle rect2 = new Rectangle(5,8,"#");
        area = rect2.getArea();
        len = rect2.getRoundLen();
        System.out.println(area);
        System.out.println(len);

        rect2.scaleWidth(2);
        area = rect2.getArea();
        len = rect2.getRoundLen();
        System.out.println(area);
        System.out.println(len);
    }
}

Point类

public class Point {
    private double x;
    private double y;

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public Point(double _x, double _y) {
        x = _x;
        y = _y;
    }
}

Circle类

public class Circle {
    private double radius;
    private Point center;

    Circle(){
        radius = 1;
        center = new Point(0,0);
    }
    Circle(double _radius, double _center_x, double _center_y){
        radius = _radius;
        center = new Point(_center_x, _center_y);
    }
    boolean isPointInCircle(Point point){
        double distance = Math.sqrt((center.getX() - point.getX())*(center.getX() - point.getX())
                + (center.getY() - point.getY())*(center.getY() - point.getY()));
        if(distance < radius){
            return true;
        }
        else{
            return false;
        }
    }
}

测试Circle类

import java.util.Random;

public class TestCircle {
    public static void main(String[] args) {
        Circle circle = new Circle(2,2,2);
        Point p1 = new Point(0,0);
        System.out.println(circle.isPointInCircle(p1));
        Point p2 = new Point(3,3);
        System.out.println(circle.isPointInCircle(p2));

        Random random = new Random();

        int in_sum = 0;
        int sum = 0;
        for(int i = 0; i < 1000000; i++){
            double x = random.nextDouble()*4;
            double y = random.nextDouble()*4;
            Point p = new Point(x,y);
            if(circle.isPointInCircle(p))
            {
                in_sum++;
            }
            sum++;
        }

        double result = in_sum*1.0/sum;
        System.out.println(result*4);

    }
}

相关文章

  • 第二章 类与对象

    用例子讲述了“面向过程”与“面向对象”之间的区别 面向对象包括类、方法和属性 类是对象的蓝图,一个类可以有很多对象...

  • Python 面向对象编程

    类和对象 定义类 Python支持面向对象编程,下面是一个例子。我们可以看到,在Python中声明类和其他语言差不...

  • 如何理解python中的类和对象?

    什么是类和对象 类和对象,在我们的生活中其实是很容易找例子的。类是一种把对象分组归类的方法。比如动物,植物就可以看...

  • Python学习笔记——类和对象

    类和对象 1. 一个例子 2. 面向对象的特征 ① 封装 ② 继承 ③ 多态 3. self 4. _ _ in...

  • C++ 类和对象的使用

    一个例子看懂C++ 类和对象的使用,由于自己写Java比较多,对C++类和对象的使用不是很熟练,记录下代码,按自己...

  • javascript的方法

    1. javascript的方法可以分为三类: a 类方法;b 对象方法;c 原型方法 例子: //对象方法 fu...

  • 对象、类对象和元类对象

    http://www.tuicool.com/articles/mmyuUr http://blog.csdn.n...

  • JavaScript基础专题之arguments(七)

    类数组对象 简单概括: 拥有一个 length 属性和若干索引属性的对象 举个例子: 两个对象都是可读写、可获取长...

  • python 类和对象

    类和对象 目标 类和对象的概念 类和对象的关系 类的设计 01. 类和对象的概念 类 和 对象 是 面向对象编程的...

  • 类,类对象和实例对象

    Python 的类定义写完之后就成了一个类对象,而引用这个类对象的就是实例对象。 类中定义的属性和方法都是静态属性...

网友评论

      本文标题:类和对象例子

      本文链接:https://www.haomeiwen.com/subject/ubrfzdtx.html