标签:加载 stc .com getc ima rgs 部分 tco package
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 }
方法重载与方法重写的区别:
方法重载:
在同一个类中,方法名相同,参数列表必须不同,与反回值类型,访问修饰符无关,
方法重写:
方法名相同,参数列表相同,反回值类型必须一致或者是其子类和抽象类,访问权限不能于父类,不能抛出比父类更多的异常
标签:加载 stc .com getc ima rgs 部分 tco package
原文地址:https://www.cnblogs.com/Zhangchuanfeng1/p/10294047.html