老师您可以帮我看看我这个计算机的面向编程写法提点意见吗

老师您可以帮我看看我这个计算机的面向编程写法提点意见吗

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <style>

        .calculator{

            width: 300px;

            margin: 100px auto;

        }

        .calculator_num{

            margin-bottom: 10px;

        }

        .calculator_num input{

            width: 100px;

            height: 15px;

        }

    </style>

</head>

<body>

    <div id="calculator">

        <div class="calculator_num">

            <input type="text" class="inp1">

            <em class="operator">/</em>

            <input type="text" class="inp2">

            <em>=</em>

            <span class="result">15</span>

        </div>

        <div class="calculator_btn">

            <button class="add">+</button>

            <button class="sub">-</button>

            <button class="mul">*</button>

            <button class="division">/</button>

        </div>

    </div>

    <script>

        var that;

        class Operation{

            constructor(id,inp1Class,inp2Class,result){

                that=this;

                /* 获取元素 */

                this.calculator=document.querySelector(id);

                this.inp1=this.calculator.querySelector(inp1Class);

                this.inp2=this.calculator.querySelector(inp2Class);

                this.result=this.calculator.querySelector(result);

                this.operator=this.calculator.querySelector('.operator');

                this.add=this.calculator.querySelector('.add');

                this.sub=this.calculator.querySelector('.sub');

                this.mul=this.calculator.querySelector('.mul');

                this.divisi=this.calculator.querySelector('.division');

                /* 创建对象实例时绑定事件 */

                this.init();

            }

            /* 绑定事件 */

            init(){

                this.add.onclick=this.addition;

                this.sub.onclick=this.subtraction;

                this.mul.onclick=this.multiplication;

                this.divisi.onclick=this.division;

            }

            /* 复用函数 */

            simpleoperation(symbol2){

                that.operator.innerHTML=symbol2;

            }

            /* 加法功能 */

            addition(){

                that.result.innerHTML=+that.inp1.value+ +that.inp2.value;

                that.simpleoperation(this.innerHTML)

            }

            /* 减法功能 */

            subtraction(){

                that.result.innerHTML=that.inp1.value-that.inp2.value;

                that.simpleoperation(this.innerHTML);

            }

            /* 乘法功能 */

            multiplication(){

                that.result.innerHTML=that.inp1.value*that.inp2.value;

                that.simpleoperation(this.innerHTML);

            }

            /* 除法功能 */

            division(){

                that.result.innerHTML=that.inp1.value/that.inp2.value;

                that.simpleoperation(this.innerHTML);

            }

        }

        new Operation('#calculator','.inp1','.inp2','.result');

    </script>

</body>

</html>


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好,自己不会封装这个很正常,因为你才刚入门,没有什么经验。这个也不需要你专门去训练,因为它是一个经验的积累。平时看视频,跟着老师敲几遍代码,学习老师是怎么封装的。当然了,不同的人,封装的代码可能也会不一样。所以你别着急,当下最重要的任务打好基础。等以后工作了,积累经验多了,慢慢就有自己的一套方式了。

祝学习愉快 ~

好帮手慕夭夭 2020-04-27 18:02:19

同学你好,代码是正确的,写的很不错,不用修改了。同学能够学以致用,说明基础掌握的很不错了。

要继续加油哦,祝学习愉快 !

  • 提问者 Yuuuuuuuuu #1
    谢谢老师,老师我问您一个问题,因为就是我代码没封装的能力,我是封装的比较少,多的都是面向过程编程,就是这个代码封装能力应该怎么训练,而且我对函数的理解只是别人写了我会用,让我自己封装我就不会了,我就会想半天,而且我觉得就是直接写和封装就感觉不一样,总感觉很别扭,
    2020-04-27 18:05:07
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师