javase个人垃圾复习笔记11Java 重写(Override)与重载(Overload)和多态

方法的重写规则
参数列表与被重写方法的参数列表必须完全相同。

返回类型与被重写方法的返回类型可以不相同,但是必须是父类返回值的派生类(java5 及更早版本返回类型要一样,java7 及更高版本可以不同)。

访问权限不能比父类中被重写的方法的访问权限更低。例如:如果父类的一个方法被声明为 public,那么在子类中重写该方法就不能声明为 protected。

父类的成员方法只能被它的子类重写。

声明为 final 的方法不能被重写。

声明为 static 的方法不能被重写,但是能够被再次声明。

子类和父类在同一个包中,那么子类可以重写父类所有方法,除了声明为 private 和 final 的方法。

子类和父类不在同一个包中,那么子类只能够重写父类的声明为 public 和 protected 的非 final 方法。

重写的方法能够抛出任何非强制异常,无论被重写的方法是否抛出异常。但是,重写的方法不能抛出新的强制性异常,或者比被重写方法声明的更广泛的强制性异常,反之则可以。

构造方法不能被重写。

如果不能继承一个方法,则不能重写这个方法。

class Animal{
    
    
   public void move(){
    
    
      System.out.println("动物可以移动");
   }
}
 
class Dog extends Animal{
    
    
   public void move(){
    
    
      System.out.println("狗可以跑和走");
   }
   public void bark(){
    
    
      System.out.println("狗可以吠叫");
   }
}
 
public class TestDog{
    
    
   public static void main(String args[]){
    
    
      Animal a = new Animal(); // Animal 对象
      Animal b = new Dog(); // Dog 对象
 
      a.move();// 执行 Animal 类的方法
      b.move();//执行 Dog 类的方法
      b.bark();
   }
}
/*以上实例编译运行结果如下:

TestDog.java:30: cannot find symbol
symbol  : method bark()
location: class Animal
                b.bark();
                 ^

Super 关键字的使用
当需要在子类中调用父类的被重写方法时,要使用 super 关键字。

class Animal{
    
    
   public void move(){
    
    
      System.out.println("动物可以移动");
   }
}
 
class Dog extends Animal{
    
    
   public void move(){
    
    
      super.move(); // 应用super类的方法
      System.out.println("狗可以跑和走");
   }
}
 
public class TestDog{
    
    
   public static void main(String args[]){
    
    
 
      Animal b = new Dog(); // Dog 对象
      b.move(); //执行 Dog类的方法
 
   }
}
/*以上实例编译运行结果如下:

动物可以移动
狗可以跑和走

重载(Overload)
重载(overloading) 是在一个类里面,方法名字相同,而参数不同。返回类型可以相同也可以不同。

每个重载的方法(或者构造函数)都必须有一个独一无二的参数类型列表。

最常用的地方就是构造器的重载。

重载规则:

被重载的方法必须改变参数列表(参数个数或类型不一样);
被重载的方法可以改变返回类型;
被重载的方法可以改变访问修饰符;
被重载的方法可以声明新的或更广的检查异常;
方法能够在同一个类中或者在一个子类中被重载。
无法以返回值类型作为重载函数的区分标准。

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
Java 多态

多态使用实例

class Person
{
    
    
   public void fun1()
   {
    
    
      System.out.println("1.Person{fun1()}") ;
   }
   public void fun2()
   {
    
    
      System.out.println("2.Person{fun2()}") ;
   }
}

// Student类扩展自Person类,也就继承了Person类中的fun1()、fun2()方法
class Student extends Person
{
    
    
   // 在这里覆写了Person类中的fun1()方法
   public void fun1()
   {
    
    
      System.out.println("3.Student{fun1()}") ;
   }
   public void fun3()
   {
    
    
      System.out.println("4.Studen{fun3()}") ;
   }
}

class TestJavaDemo1
{
    
    
   public static void main(String[] args) 
   {
    
    
      // 此处,父类对象由子类实例化
      Person p = new Student() ;
      // 调用fun1()方法,观察此处调用的是哪个类里的fun1()方法
      p.fun1() ;
      p.fun2() ;
   }
}
//运行结果
/*
3.Student{fun1()}
2.Person{fun2()}
  1. 向上转型
public class Animal {
    
    
    public void eat(){
    
    
        System.out.println("animal eatting...");
    }
}

public class Cat extends Animal{
    
    

    public void eat(){
    
    

        System.out.println("我吃鱼");
    }
}

public class Dog extends Animal{
    
    

    public void eat(){
    
    

        System.out.println("我吃骨头");
    }

    public void run(){
    
    
        System.out.println("我会跑");
    }
}

public class Main {
    
    

    public static void main(String[] args) {
    
    

        Animal animal = new Cat(); //向上转型
        animal.eat();

        animal = new Dog();
        animal.eat();
    }

}

//结果:
//我吃鱼
//我吃骨头

这就是向上转型,Animal animal = new Cat();将子类对象Cat转化为父类对象Animal。这个时候animal这个引用调用的方法是子类方法。

  1. 向下转型
class A {
    
    
         public void print() {
    
    
                  System.out.println("A:print");
         }
}

class B extends A {
    
    
         public void print() {
    
            
                  System.out.println("B:print");
         }
}

public class Test{
    
    
         public static void main(String args[])
         {
    
    
                  A a = new B();          //通过子类去实例化父类
                  a.print();
         }
}
//B:print

向下转型是把父类对象转为子类对象。


Animal a = new Cat();
Cat c = ((Cat) a);
c.eat();
//输出  我吃鱼
Dog d = ((Dog) a);
d.eat();
// 报错 : java.lang.ClassCastException:com.chengfan.animal.Cat cannot be cast to com.chengfan.animal.Dog
Animal a1 = new Animal();
Cat c1 = ((Cat) a1);
c1.eat();
// 报错 : java.lang.ClassCastException:com.chengfan.animal.Animal cannot be cast to com.chengfan.animal.Cat

为什么第一段代码不报错呢?相比你也知道了,因为a本身就是Cat对象,所以它理所当然的可以向下转型为Cat,也理所当然的不能转为Dog,你见过一条狗突然就变成一只猫这种操蛋现象?

而a1为Animal对象,它也不能被向下转型为任何子类对象。比如你去考古,发现了一个新生物,知道它是一种动物,但是你不能直接说,啊,它是猫,或者说它是狗。

class X {
    
    
    public void show(Y y){
    
    
        System.out.println("x and y");
    }

    public void show(){
    
    
        System.out.println("only x");
    }
}

class Y extends X {
    
    
    public void show(Y y){
    
    
        System.out.println("y and y");
    }
    public void show(int i){
    
    

    }
}

class main{
    
    
    public static void main(String[] args) {
    
    
        X x = new Y();
        x.show(new Y());
        x.show();
    }
}
//结果
//y and y
//only x

Y继承了X,覆盖了X中的show(Y y)方法,但是没有覆盖show()方法。

这个时候,引用类型为X的x指向的对象为Y,这个时候,调用的方法由Y决定,会先从Y中寻找。执行x.show(new Y());,该方法在Y中定义了,所以执行的是Y里面的方法;

但是执行x.show();的时候,有的人会说,Y中没有这个方法啊?它好像是去父类中找该方法了,因为调用了X中的方法。

事实上,Y类中是有show()方法的,这个方法继承自X,只不过没有覆盖该方法,所以没有在Y中明确写出来而已,看起来像是调用了X中的方法,实际上调用的还是Y中的。

//哈哈哈我不想活了

猜你喜欢

转载自blog.csdn.net/qq_45864370/article/details/108586987