zoukankan      html  css  js  c++  java
  • 5:类,对象,重载与重写的区别

    类:

    • 是模子,抽象出多个对象将拥有的共同属性和方法
    • 定义类的构造方法:如果没有定义构造方法,实例化对象时会调用一个默认的无参构造方法(实例化时一定会调用构造方法)
    • 构造方法的样式:public 类名(属性1,属性2){this+参数1=参数1,this+参数2=参数2}(this表示实例化的当前对象)
    • this()手动调用无参的构造方法

    对象:

    • 用来描述客观事物的一个实体,有一组属性和方法构成

    方法重载与方法重写:

    • 重载:普通方法和构造方法都可以重载(重载即同名但是参数个数不同)
    • 方法重写:即对方法进行重新改写

      package com.unit_chongxie;

      public class chongXie{

      public static void main(String[] args) {
      // TODO Auto-generated method stub
      Dog d1= new Dog("小黑","黑色");
      Dog d2= new Dog("小黑","黑色");
      if(d1.equals(d2)) {
      System.out.println("相同");
      }else {
      System.out.println("不相同");
      }
      }

      }

      package com.unit_chongxie;

      public class Dog{
      String name;
      String color;
      public Dog(String name, String color) {
      this.name = name;
      this.color = color;
      }
      public boolean equals(Dog d){//重写object类的equals方法
      if(this.name.equals(d.name)&&this.color.equals(d.color)) {
      return true;
      }else {
      return false;
      }
      }
      }

  • 相关阅读:
    2016-02-24 工作日记
    金字塔培训
    你找到自己的路了么?
    你是个成熟的职场人么?
    码农十年总结
    码农十年连载六
    码农十年连载五
    码农十年连载四
    码农十年连载三
    码农十年连载二
  • 原文地址:https://www.cnblogs.com/jiafeng1996/p/12283159.html
Copyright © 2011-2022 走看看