Php&&Python&&C++圆类的实现(OOP)

哎......被投诉了 😭😭😭😭😭

其实也不是小编不更,这不是期末了吗(zhaojiekou~~),而且最近学的信息收集和ctf感觉好像没找到啥能更的(不过最经还是在考虑更一下CTF的密码学的)然后就被催更了  @_# 蒽

                                                        

但是天还是无绝人之路的,他丢了一道面向对象的题目给我,好家伙还要用c艹来写

好险网安还是教了php和python的,那就来这两个版本都来试一下吧

        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        

题目:

先是Python_Version:

import math
pi = math.pi
class circle():
    def __init__(self,r,x,y):
        self.r=r
        self.x=x
        self.y=y

    def __del__(self):
        print("This object is gonna destruct")

    def circuference(self):
        print("circuference = ",2*pi*self.r)

    def area(self):
        print("area = ",pi*(self.r)**2)

outprint=circle(4,0,0)
outprint.circuference()
outprint.area()
del outprint

以下就来逐行讲解吧:

1.构造函数,用来初始化对象的属性,这里是将传入的r赋给类中的属性

 def __init__(self,r,x,y):
        self.r=r
        self.x=x
        self.y=y

2.析构函数,用于在对象被销毁时执行一些清理工作

def __del__(self):
        print("This object is gonna destruct")

3.然后就是写输出周长和面积的两个函数

def circuference(self):
        print("circuference = ",2*pi*self.r)

    def area(self):
        print("area = ",pi*(self.r)**2)

(当然了,这里也可以写成return的形式,然后print,这里我就偷个懒啦,嘻嘻)

4.最后就是创建对象,调用,销毁了

outprint=circle(4,0,0)
outprint.circuference()
outprint.area()
del outprint

(这里我取得半径是4,当然你们可以任意取)

这样就完成了一个圆类得目标,以下是输出:

接下来就是PHP_Version啦

<?php
class circle{

    public $r,$x,$y;
    function __construct(int $r,int $x,int $y){
        $this->r=$r;
        $this->x=$x;
        $this->y=$y;
    } 

    function __destruct(){
        echo "</br>";
        echo "this object is gonna destruct";
    }

    function circumference(){
        echo  "circumference = ";
        print 2*pi()*$this->r;
    } 

    function area(){
        echo "area = ";
        print (pi()*($this->r)**2);
    }
}

$outprint=new circle(4,0,0);
$outprint->circumference();
echo "</br>";
$outprint->area();
unset($outprint);

?>

开始之前不得不感叹一下还是python好用,简单又简洁

1.先是构造,和析构函数了,和上面的python类似 (记得echo “</br>”哦,php可不会自动换行)

 public $r,$x,$y;
    function __construct(int $r,int $x,int $y){
        $this->r=$r;
        $this->x=$x;
        $this->y=$y;
    } 

    function __destruct(){
        echo "</br>";
        echo "this object is gonna destruct";
    }

2.然后就是写周长和面积得的计算了

function circumference(){
        echo  "circumference = ";
        print 2*pi()*$this->r;
    } 

    function area(){
        echo "area = ";
        print (pi()*($this->r)**2);
    }
}

这里的pi是有()的哦,而且属性的调用php是$this->   而python是self.    这个可不要记混!!

3.然后就是创建,调用,销毁,和上面一样:

$outprint=new circle(4,0,0);
$outprint->circumference();
echo "</br>";
$outprint->area();
unset($outprint);

这样就完成了php的圆类实现了

以上就是两种编程语言对圆类的实现,当然了我的朋友是用c艹写的 (她写的太强啦,我看不懂wuuwu~~~)

#include <iostream>
using namespace std;
class Circle
{
private:
    int r;
    int x;
    int y;
    float c;
    float s;

public:
    Circle(int a, int b, int c) : r(a), x(b), y(c) {}
    ~Circle() { cout << "析构函数的调用" << endl; }
    void Getc()
    {
        c = 3.14 * r * 2;
    }
    void Gets()
    {
        s = 3.14 * r * r;
    }
    void show()
    {
        cout << "周长:" << c << endl
             << "面积:" << s << endl;
    }
};
int main()
{
    Circle c1(1, 0, 0);
    c1.Getc();
    c1.Gets();
    c1.show();
    system("pause");
    return 0;
}

等到寒假时候学了c艹应该就能看懂了(笑死寒假想摆烂)以上就是对圆类的三种语言的实现啦

最近在学CTF那么下一篇就来更CTF的Crypto吧   ~~!!!~~~

        ​​​​​​​        ​​​​​​​        

相关推荐

  1. OMP实现压缩感知实现(MATLAB)

    2024-01-13 23:12:03       42 阅读
  2. OOP

    2024-01-13 23:12:03       80 阅读
  3. 面向对象编程在Perl中实现:解锁PerlOOP潜力

    2024-01-13 23:12:03       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-13 23:12:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-13 23:12:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-13 23:12:03       87 阅读
  4. Python语言-面向对象

    2024-01-13 23:12:03       96 阅读

热门阅读

  1. 排序算法-快速排序

    2024-01-13 23:12:03       71 阅读
  2. 基于视觉定位技术选型

    2024-01-13 23:12:03       74 阅读
  3. 知识笔记(七十六)———链式语句中limit用法

    2024-01-13 23:12:03       64 阅读
  4. 头像的渲染及表单的验证

    2024-01-13 23:12:03       55 阅读
  5. Open3D 从点云构建八叉树(13)

    2024-01-13 23:12:03       57 阅读
  6. 【UML】第20篇 UML系列完结篇

    2024-01-13 23:12:03       71 阅读
  7. Vue3 和Vue2的区别,以及钩子函数的使用

    2024-01-13 23:12:03       61 阅读
  8. vue3基础:单文件组件介绍

    2024-01-13 23:12:03       65 阅读