Java learning Day14--polymorphism and final keywords

Polymorphism

What is polymorphism:

​ Same kind of things, showing different states at different moments

Conditions for polymorphism:

  1. There must be inheritance
  2. There must be a way to rewrite
  3. The reference of the parent class points to the object of the child class

Create animal parent class

import javaopp.Day05.InterFaceA;
import javaopp.Day05.InterFaceB;

public abstract class Animal {
    
    

    int num=10;

    abstract void eat();
    abstract void sleep();

    public static void testStatic(){
    
    
        System.out.println("Animal Static");

    }
    public  void test(){
    
    
        System.out.println("Animal Test");
    }
}

Create Dog subclass

public class Dog extends Animal {
    
    

    int num=5;

    @Override
    public void eat() {
    
    
        System.out.println("狗吃肉");
    }

    @Override
    public void sleep() {
    
    
        System.out.println("狗 to bed");
    }

    public void play(){
    
    
        System.out.println("狗玩辣子");
    }

}

Create Cat subclass

public class Cat extends Animal{
    
    

    @Override
    public void eat() {
    
    
        System.out.println("喵喵鱼");
    }

    @Override
    public void sleep() {
    
    
        System.out.println("喵喵睡觉");
    }

    public void catchMouse(){
    
    
        System.out.println("猫捉老鼠");
    }
}

Test class

public class Test2 {
    
    
   public static void main(String[] args) {
    
    

        /*
        向上类型准换
         */
        Dog dog = new Dog();//多态体现在调用方法
        CAt cat = new CAt();//父类的引用指向子类对象

        Test2 t1 = new Test2();
        t1.feedAnimal(dog);
        t1.feedAnimal(cat);
    }

    //多态应用: 同一种事物,在不同时刻表现不同的状态
      /*
    向上类型转换的弊端:无法访问子类中的特有方法
     */
    public void feedAnimal(Animal animal) {
    
    
        animal.eat();
        animal.sleep();
       /*
        向下转型,可以访问子类中的方法
        */
        if(animal instanceof Dog){
    
    
            Dog dog = (Dog) animal;
            dog.play();
        }
        /*
            animal instanceof CAt:在运行时检测判断animal 中实际包含是否是指定的类型
         */
        if (animal instanceof CAt){
    
    
            CAt cat = (CAt) animal;//向下转型:由父类类型转为子类类型
            cat.catchMouse();
        }
    }

}

Reference type and reference type conversion

The premise is the inheritance relationship

Default transformation (upcast)

    Animal dog = new Dog();
    dog.eat();//编译看左边,运行期间看右边

Forced type conversion (downcast)

  public static void main(String[] args) {
    
    

        //向上转型 ,父类类型表示所有的子类
        Animal dog = new Dog();
        Animal cat = new Cat();

        Test2 t1 = new Test2();

        t1.feedAnmal(dog);
        t1.feedAnmal(cat);
    }

     /*
        多态缺点: 向上转型为父类类型,弊端:不能访问子类中特有的方法
      */
    public void feedAnmal(Animal animal){
    
    
          animal.eat();
            //animal instanceof Dog   在运行时  检测判断 animal 中实际 包含是否是指定的类型
           if(animal instanceof Dog){
    
    
               Dog dog = (Dog)animal;//向下转型:由父类类型 转为 子类类型
               dog.play();
           }
           if(animal instanceof Cat){
    
    
               Cat cat = (Cat)animal;
               cat.catchMouse();
           }
    }
}

final keyword

final can modify attributes, methods, classes, method parameters

class FinalDemo {
    
    

    //final 修饰的变量变为常量,值不能改变
    final static int num1 = 10;//直接初始化变量,建议用static修饰,在内存中就会有一份

    //final int num;
    /*
    在创建的每一个对象中包含一个常量,必须在构造方法中赋值
     */
    public FinalDemo() {
    
    

    }
    /*public FinalDemo(int a) {
        num=a;
    }*/

    public final void test(final int a) {
    
    
        //num=5;
        System.out.println(a);
        //a=1;
    }
    public void eat() {
    
    
        System.out.println("aaaaa");
    }

    public static void main(String[] args) {
    
    
        //FinalDemo f1 = new FinalDemo(5);
        //FinalDemo f2 = new FinalDemo(10);
    }

}


/*
public class Son extends FinalDemo
final 修饰的类无法被继承
* */
public class Son extends FinalDemo{
    
    
    @Override
    public void eat() {
    
    
        super.eat();
    }

    /*
    public void test(){}
    被final修饰的方法,子类无法重写
    */
}

Guess you like

Origin blog.csdn.net/XiaoFanMi/article/details/110684180