编程语言
首页 > 编程语言> > 20220727-Java中多态总结

20220727-Java中多态总结

作者:互联网

目录

多态polymorphism:方法或者对象具有多种形态

方法的多态

  1. 方法的重载可以体现多态

代码示例

// 通过方法重载,展现同一种方法的不同形态
public class PolyMethod {
    public static void main(String[] args) {
        AA aa = new AA();
        aa.f();
        aa.f(1);
        aa.f(1, 2);
    }
}
class AA {
    public void f() {
        System.out.println("f()");
    }
    public void f(int i) {
        System.out.println("f(i)");
    }
    public void f(int i, int j) {
        System.out.println("f(i,j)");
    }
}
  1. 方法的重写可以体现多态

代码示例

// 通过子类继承父类并重写父类中的方法,体现同一个方法的不同形态
public class PolyMethod02 {
    public static void main(String[] args) {
        BB bb = new BB();
        bb.f();
        BBB bbb = new BBB();
        bbb.f();
    }
}
class BB {
    public void f() {
        System.out.println("BB f()");
    }
}
class BBB extends BB {
    @Override
    public void f() {
        System.out.println("BBB f()");
    }
}

对象的多态

  1. 对象的编译类型与运行类型可以不同
  2. 对象的编译类型在定义对象的时候就确定了不能更改
  3. 对象的运行类型可以改变
  4. 对象的编译类型看创建对象时=的左边,运行类型看=的右边
    • Animal animal = new Dog();
    • animal = new Cat();

代码示例

public class PolyObject {
    public static void main(String[] args) {
        People people = new Student();
        //people编译类型为People,运行类型为Student
        //System.out.println(people.id);
        //people.f2();
        System.out.println("name=" + people.name);
        System.out.println(people.show());
    }
}
class People {
    String name = "jack";
    int age = 18;
    public void f1() {
        System.out.println("People f1()");
    }
    public String show() {
        return "name=" + name + ",age=" + age;
    }
}
class Student extends People {
    int id = 1;
    double score = 100;
    @Override
    public String show() {
        return super.show() + ",id=" + id + ",score=" + score;
    }
    public void f2() {
        System.out.println("Student f2()");
    }
}

多态的注意事项和细节

  1. 使用父类对象引用可以调用父类中的所有成员(遵守访问权限)
  2. 使用父类对象引用不能调用子类的特有成员(编译阶段,只能按编译类型访问)
  3. 最终的运行效果,看运行类型
  4. !!!属性没有多态之说,属性的值直接看编译类型
  5. instanceof比较操作符,用于判断对象的运行类型是否为XX类型或者XX类型的子类型

代码示例

public class PolyDetail02 {
    public static void main(String[] args) {
        A a = new B();
        System.out.println(a.count);
        //System.out.println(a.x);
    }
}
class A{
    int count = 10;
}
class B extends A{
    int count = 20;
    int x = 100;
}

代码示例

public class PolyDetail03 {
    public static void main(String[] args) {
        C c = new D();
        System.out.println(c instanceof C);
        System.out.println(c instanceof D);
        System.out.println(c instanceof Object);
        D d = new D();
        System.out.println(d instanceof C);
        System.out.println(d instanceof D);
        C cc = new C();
        System.out.println(cc instanceof C);
        System.out.println(cc instanceof D);
    }
}
class C {}
class D extends C {}

向下转型

  1. 语法:子类类型 变量=(子类类型)父类引用;
  2. 只能强转父类的引用,而不能强转父类的对象
  3. 要求父类的引用必须指向当前目标类型的对象
  4. 当向下转型后,可以调用子类类型中所有的成员

Java动态绑定机制

  1. 当调用对象方法的时候,该方法会和该对象的内存地址/运行类型绑定
  2. 当调用对象属性时,没有动态绑定机制,哪里声明,哪里使用

代码示例

public class DynamicBinding {
    public static void main(String[] args) {
        A a = new B();
        System.out.println(a.sum());
        System.out.println(a.sum1());
    }
}
class A {
    public int i = 10;
    public int sum() {
        return getI() + 10;
    }
    public int sum1() {
        return i + 10;
    }
    public int getI() {
        return i;
    }
}
class B extends A {
    public int i = 20;
    @Override
    public int sum() {
        return getI() + 20;
    }
    @Override
    public int sum1() {
        return i + 10;
    }
    @Override
    public int getI() {
        return i;
    }
}

标签:Java,20220727,class,System,多态,int,println,public,out
来源: https://www.cnblogs.com/zhanghuaze/p/16526879.html