zoukankan      html  css  js  c++  java
  • 2019.1.19equals方法重写

     1 package com.vote;
     2 
     3 public class Car {
     4     private String color;//颜色
     5     private String brand;//品牌
     6     private int plate;//车牌
     7     
     8     public Car(String color,String brand,int plate) {
     9         this.color = color;
    10         this.brand = brand;
    11         this.plate = plate;
    12     }
    13     
    14     public String getColor() {
    15         return color;
    16     }
    17     public void setColor(String color) {
    18         this.color = color;
    19     }
    20     public String getBrand() {
    21         return brand;
    22     }
    23     public void setBrand(String brand) {
    24         this.brand = brand;
    25     }
    26 
    27     public int getPlate() {
    28         return plate;
    29     }
    30 
    31     public void setPlate(int plate) {
    32         this.plate = plate;
    33     }
    34 
    35     public boolean equals(Object obj) {
    36         if(obj instanceof Car) {
    37             Car u = (Car)obj;
    38             if(u.getBrand().equals(this.getBrand())&&u.getColor().equals(this.getColor())&&u.getPlate()==this.getPlate()) {
    39                 return true;
    40             }
    41         }
    42         return false;
    43     }
    44     
    45 
    46     
    47 }
     1 package com.vote;
     2 
     3 public class TestCar {
     4     public static void main(String[] args) {
     5         Car car1 = new Car("黄色","兰博基尼",55379);
     6         Car car2 = new Car("黄色","兰博基尼",55379);
     7         System.out.println(car1==car2);
     8         System.out.println(car1.equals(car2));
     9     }
    10 }

    方法重载与方法重写的区别:

     

    方法重载:
    在同一个类中,方法名相同,参数列表必须不同,与反回值类型,访问修饰符无关,
    方法重写:
    方法名相同,参数列表相同,反回值类型必须一致或者是其子类和抽象类,访问权限不能于父类,不能抛出比父类更多的异常

    1、构造方法的作用是什么
    创建对象
    2、方法的重载有什么特点
    在同一个类中,方法名相同,参数项必须不同,与访问修饰符和返回值类型无关
    3、封装的步骤
    私有化属性、生成setter/getter方法、在方法中添加条件
    4、简述static的用法
    static修饰变量、方法、代码块
    是类级别,可以直接通过类来调用
    类一加载就优先加载static修饰的部分、并且只加载一次
    5、什么是继承
    将子类中公有的属性和方法抽取出来生成一个父类,这就是继承
  • 相关阅读:
    go语言之goroute协程
    Vue中Computed和Watch的用法及区别
    php判断复选框是否被选中的方法
    基于workerman的实时推送
    织梦引入公共模板
    织梦快速建站首页模板
    golang解决中文乱码的方法
    Vue项目中使用可视化图表echarts
    解决for循环中异步请求顺序不一致的问题
    layui多图上传实现删除功能的方法
  • 原文地址:https://www.cnblogs.com/Zhangchuanfeng1/p/10294047.html
Copyright © 2011-2022 走看看