【java学习笔记】-- 方法重写和多态

一、方法重写

1、概念

在子类中如果创建了一个与父类中相同名称、相同返回值类型、相同参数列表的方法,只是方法体中的实现不同,以实现不同于父类的功能,这种方式被称为方法重写(override),又称为方法覆盖。

重写的原因:当父类中的方法无法满足子类需求或子类具有特有功能的时候,需要方法重写。

2、规则

  • 参数列表必须完全与被重写的方法参数列表相同。
  • 返回的类型必须与被重写的方法的返回类型相同([Java]1.5 版本之前返回值类型必须一样,之后的 Java 版本放宽了限制,返回值类型必须小于或者等于父类方法的返回值类型)。
  • 访问权限不能比父类中被重写方法的访问权限更低(public>protected>default>private)。
  • 重写方法一定不能抛出新的检査异常或者比被重写方法声明更加宽泛的检査型异常。
  • 父类的静态方法不能被子类覆盖为非静态方法,父类的非静态方法不能被子类覆盖为静态方法
  • 子类可以定义与父类同名的静态方法,以便在子类中隐藏父类的静态方法
    (注:静态方法中无法使用super)
  • 父类的成员方法只能被它的子类重写。
  • 声明为 final 的方法不能被重写。
  • 声明为 static 的方法不能被重写,但是能够再次声明。
  • 构造方法不能被重写。
  • 子类和父类在同一个包中时,子类可以重写父类的所有方法,除了声明为 private 和 final 的方法。
  • 子类和父类不在同一个包中时,子类只能重写父类的声明为 public 和 protected 的非 final 方法。
  • 如果不能继承一个方法,则不能重写这个方法。

父类Animal.java

public class Animal {
	String name;
	int health;
	int love;
	
	public Animal() {
		super();
	}
	public Animal(String name, int health, int love) {
		super();
		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.name + ",健康值:" + this.health + ",亲密度:" + this.love);
	}
}

子类Cat.java

public class Cat extends Animal {
	private String color;

	public Cat() {
		super();
	}

	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;
	}
	
	//重新定义父类里的print()方法
	public void print(){
		//输出宠物的所有信息
		super.print();//使用super调用父类Animal中的print方法
		System.out.println("宠物颜色:"+this.color);
	}
	
}	
	

测试类Test.java

public class Test {

	public static void main(String[] args) {
		Cat cat1 = new Cat("mimi",90,60,"白色");
		cat1.print();	
	}
	
}

[]

二、Object类

1、Object是所有类的父类

public class  Test  extends Object {
	……
}

2、Object类中经常被重写的方法

在这里插入图片描述

Object类里的toString方法输出的是对象的地址值

子类中直接输出对象和通过对象调用toString()方法,都是按照Object类中的toString方法输出地址值。

在应用中输出地址值没有具体的意义,所以在子类中都会重写Object类中的toString()方法

	//重写toString
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}

Object类里的equals()方法比较的对象的地址值

子类在进行比较的时候比较地址值没有具体的应用意义,所以在子类中可以重写Object类里的equals()方法实现对象的具体比较

	//重写equals方法
	public boolean equals(Student stu) {
		if (this == stu){
			return true;
		}		
		if (this.name.equals(stu.name) && this.age == stu.age){
			return true;
		}
		return false;
	}

三、多态

1、概念

同一个引用类型,使用不同的实例而执行不同操作

Java 实现多态有 3 个必要条件:继承、重写和向上转型

  • 继承:在多态中必须存在有继承关系的子类和父类。
  • 重写:子类对父类中某些方法进行重新定义,在调用这些方法时就会调用子类的方法。
  • 向上转型:在多态中需要将子类的引用赋给父类对象,只有这样该引用才既能可以调用父类的方法,又能调用子类的方法。

Animal.java

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

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

	public Animal(String name, int health, int love) {
		super();//调用父类Object类里的无参构造方法
		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.name + ",健康值:" + this.health
				+ ",亲密度:" + this.love);
	}

}

Dog.java

public class Dog extends Animal {
	private String strain;

	public Dog() {
		super();// 调用Animal类里的无参构造方法
	}
	public Dog(String name, int health, int love, String strain) {
		super(name, health, love);// 调用Animal类里的有参构造方法
		this.strain = strain;
	}
	public String getStrain() {
		return strain;
	}
	public void setStrain(String strain) {
		this.strain = strain;
	}
	public void print(){
		super.print();
		System.out.println("宠物品种:"+this.strain);
	}
	
	public void toHospital(){
		System.out.println("(狗)吃药......");
		this.setHealth(88);
	}

}

Cat.java

public class Cat extends Animal {
	private String color;

	public Cat() {
		super();
	}
	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;
	}	
	//重新定义父类里的print()方法
	public void print(){
		//输出宠物的所有信息
		super.print();
		System.out.println("宠物颜色:"+this.color);
	}
	
	public void toHospital(){
		System.out.println("(猫)打针......");
		this.setHealth(70);
	}
		
}

Master.java

public class Master {
	//带宠物去看病
	public void cure(Animal animal){
		//宠物健康值小于60,看病
		if(animal.getHealth()<60){
			animal.toHospital();
		}
	}

}

Test.java

public class Test {

	public static void main(String[] args) {
		//创建Master类对象
		Master master = new Master();
		
		//创建Cat类、Dog类、Penguin类对象
		Cat cat1 = new Cat("咪咪", 50, 90, "黑色");
		master.cure(cat1);
		System.out.println(cat1.getHealth());
		
		Dog dog1 = new Dog("旺财", 30, 88, "金毛");
		master.cure(dog1);
		System.out.println(dog1.getHealth());
	}

}

2、抽象方法

  • (1)使用abstract修饰的方法为抽象方法
  • (2)抽象方法没有方法体,即不写花括号
  • (3)抽象方法所在的类要声明为抽象类
  • (4)子类必须重写父类中所有的抽象方法,如果不重写,子类也要定义成抽象

3、抽象类

  • (1)使用abstract修饰的类为抽象类
  • (2)抽象类中可以有普通方法,也可以有抽象方法,也可以没有抽象方法
  • (3)抽象类不能实例化,实例化没有任何意义
	//定义一个Animal去医院看病的抽象方法
	public abstract void toHospital();


4、向上转型和向下转型

public class Test {

	public static void main(String[] args) {
	//Animal  animal = new Animal(); //抽象类不能实例化
		
		//向上转型:父类的引用指向子类的实例----》 子---->父
		Animal animal = new Cat("咪咪", 50, 80, "白色");
		animal.toHospital();
		
		//instanceof:判断某一个引用指向的是哪一个实例
		if(animal instanceof Cat){
			//向下转型(引用数据类型的强制转换):子类的引用指向父类的对象名---->父-->子
			Cat cat = (Cat)animal;
			cat.play();
		}else if(animal instanceof Dog){
			Dog dog = (Dog)animal;
			dog.eat();
		}	
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值