zoukankan      html  css  js  c++  java
  • 3.02面向对象(创建一个对象的步骤)

     A:画图演示
        * 画图说明一个对象的创建过程做了哪些事情?
        * Student s = new Student();
        * 1,Student.class加载进内存
        * 2,声明一个Student类型引用s
        * 3,在堆内存创建对象,
        * 4,给对象中属性默认初始化值
        * 5,属性进行显示初始化
        * 6,构造方法进栈,对对象中的属性赋值,构造方法弹栈
        * 7,将对象的地址值赋值给s

      一个对象的创建过程做了哪些事情?

      需求:定义一个长方形类,类中有两个方法,分别求长方形的周长和面积的方法,然后定义一个测试类进行测试。

      矩形:Rectangle

      求周长的方法: getLength()

      求面积的方法: getArea()

    面向对象(长方形案例练习)

     A:案例演示

      * 需求:
            * 定义一个长方形类,定义 求周长和面积的方法,
            * 然后定义一个测试类进行测试。

    class Test1_Rectangle {                                            //Rectangle矩形

           public static void main(String[] args) {

                  Rectangle r = new Rectangle(10,20);

                  System.out.println(r.getLength());        //周长

                  System.out.println(r.getArea());           //面积

           }

    }

    /*

    * A:案例演示

           * 需求:

                  * 定义一个长方形类,定义 求周长和面积的方法,

                  * 然后定义一个测试类进行测试。

           分析:

                  成员变量:

                         宽width,高high

                  空参有参构造

                  成员方法:

                         setXxx和getXxx

                         求周长:getLength()

                         求面积:getArea()

    */

    class Rectangle {

           private int width;                         //宽

           private int high;                          //高

     

           public Rectangle(){}                    //空参构造

     

           public Rectangle(int width,int high) {

                  this.width = width;               //有参构造

                  this.high = high;

           }

     

           public void setWidth(int width) {//设置宽

                  this.width = width;

           }

     

           public int getWidth() {                 //获取宽

                  return width;

           }

     

           public void setHigh(int high) {     //设置高

           this.high = high;

           }

     

           public int getHigh() {                  //获取高

                  return high;

           }

     

           public int getLength() {        //获取周长

                  return 2 * (width + high);

           }

     

           public int getArea() {                  //获取面积

                  return width * high;

           }

    }

  • 相关阅读:
    class(类)和构造函数(原型对象)
    es6中export和export default的区别
    vue混入 (mixin)的使用
    ES6(Module模块化)
    vue-cli3构建和发布 实现分环境打包步骤(给不同的环境配置相对应的打包命令)
    Vue中使用Echarts 脱坑
    Nginx配置详解
    VUE面包屑组件
    更改 pip 默认下载源(pip 配置文件)
    常见免费API接口
  • 原文地址:https://www.cnblogs.com/zyyzy/p/12419330.html
Copyright © 2011-2022 走看看