Java笔记 方法重写和多态

方法的重写或方法的覆盖(overriding)

        1)子类根据需求对从父类继承的方法进行重新编写

        2)重写时,可以用super.方法的方式来保留父类的方法

        3)构造方法不能被重写

方法重写规则:

          1)方法名相同

          2)参数列表相同

          3)返回值类型相同或者是其子类

          4)访问权限不能严于父类

          5)父类的静态方法不能被子类覆盖为非静态方法,父类的非静态方法不能被子类覆盖为静态方法

          6)子类可以定义与父类同名的静态方法,以便在子类中隐藏父类的静态方法(注:静态方法中无法使用super)

          7)父类的私有方法不能被子类覆盖

          8)不能抛出比父类方法更多的异常

方法重写vs方法重载

   比较项

位置

方法名

参数表

返回值

访问修饰符

方法重写

子类

相同

相同

相同或是其子类

不能比父类更严格

方法重载

同类

相同

不相同

无关

无关

Object类:Object类是所有类的父类

        

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

方法

说明

toString()

返回当前对象本身的有关信息,按字符串对象返回

equals()

比较两个对象是否是同一个对象,是则返回true

hashCode()

返回该对象的哈希代码值

getClass()

获取当前对象所属的类信息,返回Class对象

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;
	}
    //重写toString()方法
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
    //重写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;
	 }
}
public class Test {

	public static void main(String[] args) {
		
		//创建两个学生对象
		Student stu1 = new Student("张三", 22);
		Student stu2 = new Student("张三", 22);
        
        //输出两个学生对象
        System.out.println(stu1.toString());
        System.out.println(stu1.toString());


        //比较两个学生对象
		boolean result2 =stu1.equals(stu2);
		System.out.println(result2);        
	}
}

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

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);
	}
	
	//定义一个Animal去医院看病的方法
	public void toHospital(){
		System.out.println("去医院找医生看病.....");
	}

}
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);
    //定义一个Cat类自己的方法
	public void play(){
		System.out.println("猫咪喜欢粘人,喜欢完球");
	}
	}
}
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);
    //定义一个Dog类自己的方法
	public void eat(){
		System.out.println("狗吃狗粮和骨头.....");
	}
	}
}
public class Master {
	//带宠物去看病,使用父类作为方法的形参,是Java中实现和使用多态的主要方式
	public void cure(Animal animal){
		//宠物健康值小于60,看病
		if(animal.getHealth()<60){
			animal.toHospital();
		}
	}
}
public class Test {

	public static void main(String[] args) {
		//创建Master类对象
		Master master = new Master();
		
		//创建宠物类对象
		Cat cat = new Cat("咪咪", 40, 80, "白色");
		master.cure(cat);
		System.out.println(cat.getHealth());
		
		//向上转型:父类的引用指向了子类的实例(对象)
		Animal animal  = new Dog("旺财", 50, 80, "哈士奇");
		master.cure(animal);

        //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、付费专栏及课程。

余额充值