zoukankan      html  css  js  c++  java
  • Java面向对象

    面向对象

     

    面向过程与面向对象的区别

     

    面向过程:侧重点流程,从头到尾一气呵成,牵一发而动全身,重构复杂。流水线式。

     

    面向对象:模块化(类),抽离重复的部分。模块组合完成业务。上帝视角。

     

    类和对象的区别

     

    类:模板,模块,图纸。

    对象:根据图纸构建出来的具体的事物。

     

    类如何构建

     

    属性

     

    String brand;// 品牌
    String color;// 颜色
    int speed;// 速度

     

    行为

     

    public void run() {
       System.out.println("车会跑");
    }

     

    综合代码

     

    package com.shsxt.day06;

    /**
    * 汽车类
    */
    public class Car {

       // 属性
       String brand;// 品牌
       String color;// 颜色
       int speed;// 速度

       // 行为
       public void run() {
           System.out.println(brand + "车会跑");
      }

    }

     

    属性和行为的权限问题

     

    public:公共的,公开的

    private:私有的,只能自己访问

     

    package com.shsxt.day06;

    /**
    * 汽车类
    */
    public class Car {

       // 属性
       private String brand;// 品牌
       private String color;// 颜色
       private int speed;// 速度

       // 行为
       public void run() {
           System.out.println(brand + "车会跑");
      }

       // 提供一些公开的方法
       public void setBrand(String b) {
           brand = b;
      }

       public String getBrand() {
           return brand;
      }

    }

     

    就近原则

     

    错误演示

     

      因为程序的就近原则,我们有时编写的代码如果遵循了见名知意的约定就容易出现以下比较尴尬的情况,自己赋值给自己:

    public void setBrand(String brand) {
       brand = brand;
    }

      通过下图可以看到,其实就只有一个brand变量,我们类中的brand并没有被赋值。

     

     

    正确的写法,this关键字

     

      为了避免这种情况的发生,Java语言提供了 this 关键字来区分类的变量(成员变量)和传入参数变量(局部变量)的区别。

      this 表示当前正在运行的对象。

    package com.shsxt.day06;

    /**
    * 汽车类
    */
    public class Car {

       // 属性
       private String brand;// 品牌
       private String color;// 颜色
       private int speed;// 速度

       // 行为
       public void run() {
           System.out.println(brand + "车会跑");
      }

       // 提供一些公开的方法
       // get/set方法
       public void setBrand(String brand) {
           this.brand = brand;
      }

       public String getBrand() {
           return this.brand;
      }
       
       public void setColor(String color) {
           this.color = color;
      }
       
       public String getColor() {
           return this.color;
      }
       
       public void setSpeed(int speed) {
           this.speed = speed;
      }
       
       public int getSpeed() {
           return this.speed;
      }

    }

     

    构造器

     

    • 根据类创建了对象以后,会默认有一个无参的构造器

    • 如果自定义了构造器,则无参构造器会失效,按需改写

    • 构造器可以重载

    // 无参构造器
    权限修饰符 类名() {
       
    }

    // 有参构造器
    权限修饰符 类名(参数1, 参数2) {
       
    }

     

    package com.shsxt.day06;

    /**
    * 汽车类
    */
    public class Car {

       // 属性
       private String brand;// 品牌
       private String color;// 颜色
       private int speed;// 速度

       // 无参构造器
       public Car() {

      }

       // 有参构造器-重载
       public Car(String brand, String color, int speed) {
           this.brand = brand;
           this.color = color;
           this.speed = speed;
      }
       // 有参构造器-重载
       public Car(String brand, String color) {
           this.brand = brand;
           this.color = color;
      }
       // 有参构造器-重载
       public Car(String brand) {
           this.brand = brand;
      }

       // 行为
       public void run() {
           System.out.println(this.brand + "车会跑");
      }

    }
    Car car = new Car("福特", "黑色", 110);

     

    打印所有属性 toString()

     

    public String toString() {
       return "Car [品牌:" + this.brand
           + ",颜色:" + this.color
           + ",速度:" + this.speed + "]";
    }
  • 相关阅读:
    移动端高清、多屏适配方案
    Cookie存中文乱码的问题
    手机网站-前端开发布局技巧汇总
    深入理解javascript中的立即执行函数(function(){…})()
    test
    [PAT] A1013 Battle Over Cities (25分)
    PAT索引
    [PAT] A1012 The Best Rank
    [PAT] A1091 Acute Stroke
    [PAT] A1067 Sort with Swap(0, i)
  • 原文地址:https://www.cnblogs.com/CHOI-6/p/13263054.html
Copyright © 2011-2022 走看看