JAVA面向对象---重写

本文详细介绍了Java中的方法重写,包括重写规则、构造方法与静态方法的区别,并通过Pet、Cat和Penguin类举例说明。此外,还探讨了Object类的toString和equals方法,解释了为何在子类中需要重写这两个方法以满足特定需求。
摘要由CSDN通过智能技术生成

重写

1、方法的重写

  • 子类根据需求对从父类继承的方法进行重新编写
  • 重写时,可以用super.方法的方式来保留父类的方法
  • 构造方法不能被重写
方法重写的规则:
  • 方法名相同
  • 参数列表相同
  • 返回值类型相同或者是其子类
  • 访问权限不能严于父类
  • 父类的静态方法不能被子类覆盖为非静态方法,父类的非静态方法不能被子类覆盖为静态方法
  • 子类可以定义与父类同名的静态方法,以便在子类中隐藏父类的静态方法(注:静态方法中无法使用super)
  • 父类的私有方法不能被子类覆盖
  • 不能抛出比父类方法更多的异常
#### 方法重写与方法重载:

重载:是让类以统一的方式,处理不同类型数据的一种手段。多个同名函数同时存在,具有不同的参数个数/类型。重载的时候,方法名要一样,但参数类型和个数不一样,返回值类型可以相同也可以不同。

重写:是当父类的方法不能满足子类需求的时候,子类需要重写这个方法达到所需的功能。

方法的重载发生在同一个类中,方法的从写发生在不同的类中。

在这里插入代码片package cn.bdqn.demo01;

public class Pet {
	private String name;
	private int health;
	private int love;

	// 添加构造方法
	public Pet() {
	}

	public Pet(String name, int health, int love) {
		this.name = name;
		this.health = health;
		this.love = love;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getHealth() {
		return health;
	}

	public void setHealth(int health) {
		this.health = health;
	}

	public int getLove() {
		return love;
	}

	public void setLove(int love) {
		this.love = love;
	}

	public void print() {
		System.out.println("宠物信息:昵称:" + this.getName() + ",健康值:"
				+ this.getHealth() + ",亲密度:" + this.getLove());
	}

}
在这里插入代码片package cn.bdqn.demo01;

public class Cat extends Pet {

	// 定义Cat类独有的属性
	private String color;

	public Cat() {
		super();// 调用父类Pet类里的无参构造方法
	}

	public Cat(String name, int health, int love, String color) {
		super(name, health, love);
		this.color = color;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}
	
	//在Cat类中重写了Pet类中的print方法
	public void print(){
		super.print();
		System.out.println("宠物颜色:"+this.getColor());
	}

}
在这里插入代码片package cn.bdqn.demo01;

public class Penguin extends Pet {

	// 定义Penguin类里独有的属性
	private String sex;

	public Penguin() {
		super();// super表示父类Pet对象,所以这个地方表示调用父类的无参构造方法
	}

	public Penguin(String name, int health, int love, String sex) {
		super(name, health, love);// 表示调用父类Pet类的有参构造方法
		this.sex = sex;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
	@Override
	public void print() {
		super.print();
		System.out.println("宠物性别:"+this.getSex());
	}

}

public class Test {

	public static void main(String[] args) {
		//创建子类对象
		Cat cat = new Cat("Tom", 99, 80, "灰色");
		cat.print();
		
		Dog dog = new Dog("来福", 100, 99, "藏獒");
		dog.print();
		
		Penguin penguin = new Penguin("QQ", 99, 98, "公");
		penguin.print();

		
		/*
		 * 各个子类对象是可以通过调用父类中的print()方法来输出子类对象的信息
		 * 但是父类中的print()只能输出相同的属性信息(昵称、健康值、亲密度)
		 * 不能输出每个子类中独有的信息,从而说明父类里的print()方法不能够满足子类对象的使用
		 * 此时需要对父类中的print()方法进行升级(方法重写)
		 * 
		 * 重写的特点:
		 * 	1)在子类中重写
		 * 	2)方法名相同
		 * 	3)参数列表相同
		 * 	4)重写后的方法返回值类型与父类相同或者是其子类
		 * 	5)访问权限修饰符不能严于父类
		 * 
		 * 
		 */

	}

}
Object类

Object类是所有类的父类

Object类被子类经常重写的方法

toString类
package cn.bdqn.demo02;

public class Student {
	private String name;
	private int age;

	public Student() {
		super();// 调用Object类里的无参构造方法
	}

	public Student(String name, int age) {
		super();// 调用Object类里的无参构造方法
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		
		return "姓名:"+this.getName()+",年龄:"+this.getAge();
	}

}

在这里插入代码片package cn.bdqn.demo02;

public class Test {

	public static void main(String[] args) {
		// 创建Student类对象
		Student stu = new Student("张三", 25);
		System.out.println(stu);
		//我们没有在Student类中定义toString()方法,为什么Student类对象可以调用?
		//虽然Student类中没有定义toString(),但是在Student类的父类Object类里有toString()方法,Student类对象可以调用
		String str =stu.toString();
		System.out.println(str);
		
		Student stu2 = new Student("李四",26);
		System.out.println(stu2);
		
		String string = new String("hello java");
		System.out.println(string);//地址值
		System.out.println(string.toString());

	}
}

equals类
  • 比较两个对象是否是同一个对象,是则返回true
  • 操作符==

​ 简单数据类型,直接比较值。如1==2

​ 引用类型,比较两者是否为同一对象

package cn.bdqn.demo03;

public class Dog {
	// 定义属性
	private String name;
	private int health;
	private int love;
	private String strain;

	// 定义构造方法
	public Dog() {
		super();// 调用父类Object类里的无参构造方法
	}

	public Dog(String name, int health, int love, String strain) {
		super();// 调用父类Object类里的无参构造方法
		this.name = name;
		this.health = health;
		this.love = love;
		this.strain = strain;
	}

	// 添加getXxx()/setXxx()方法
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getHealth() {
		return health;
	}

	public void setHealth(int health) {
		this.health = health;
	}

	public int getLove() {
		return love;
	}

	public void setLove(int love) {
		this.love = love;
	}

	public String getStrain() {
		return strain;
	}

	public void setStrain(String strain) {
		this.strain = strain;
	}

	@Override
	public String toString() {
		return "Dog [name=" + name + ", health=" + health + ", love=" + love
				+ ", strain=" + strain + "]";
	}

	public boolean equals(Dog dog){
		//比较你传递过来的dog对象与调用这个方法的对象里的昵称、品种、健康值是否一致
		if(dog.getName().equals(this.getName())&&dog.getStrain().equals(this.getStrain())&&dog.getHealth()==this.getHealth()){
			return true;
		}else{
			return false;
		}
	}
}

package cn.bdqn.demo03;

public class Test {

	public static void main(String[] args) {
		// 创建Dog类对象
		Dog dog = new Dog("旺财", 99, 99, "哈士奇");
		System.out.println(dog);
		System.out.println(dog.toString());
		
		Dog dog2 = new Dog("旺财", 99, 99, "哈士奇");
		
		
		/*
		 * Object类中的equals()方法比较是的两个对象的地址值,不能满足子类的需求:
		 * 	String类里只要两个字符串对象的内容相同,就认为两个对象是同一个对象,所以String类重写了Object类里的equals()方法
		 * 	DOg类里只要两个Dog对象的昵称、品种和健康值一致,就认为两个Dog对象是同一个对象,所以也需要在Dog类里重写Object里的equals()方法
		 * 
		 * 
		 */
		boolean result =dog2.equals(dog);
		System.out.println(result);//false
		
		System.out.println(dog);
		System.out.println(dog2);
		
		String str1 = new String("qwert");
		String str2 = new String("qwert");
		//String类重写了Object类中的equals()方法,实现只要字符串对象里的内容相同,就认为两个对象相等---》比较的不再是对象的地址值,而是对象里的内容
		System.out.println(str1.equals(str2));//true

	}

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值