1,结合多态发生的条件,及继承相关知识,自己总结并验证下,哪些方法无法实现多态效果。

    多态条件 1,必须要有两个类继承父子类 2,必须发生方法的重写
    无法实现多态的方法 构造方法不能被继承
    Final不能被重写
    Static不能重写
    Private不能被重写

    2,自己定义一个类,类中定义3个成员变量,这3个成员变量都被final修饰,
    请用3种不同方式,为这3个被final修饰的成员变量赋值。

    1. class Test02{
    2. public static void main(String[] args) {
    3. Student student=new Student(18);
    4. System.out.println(student.name);
    5. System.out.println(student.age);
    6. System.out.println(student.gender);
    7. }
    8. }
    9. class Student{
    10. final String name="张三";//显式赋值
    11. final int age;
    12. final String gender;
    13. {
    14. this.gender="男";//gouzaodaima赋值
    15. }
    16. public Student(int age) {//构造器赋值
    17. this.age = age;
    18. }
    19. }

    image.png
    3,实现如下多态案例,分别定义Person类,SouthPerson(表示南方人),NorthPerson(表示北方人)
    Person
    eat()
    SouthPerson
    eat()
    NorthPerson
    eat()
    写代码实现,eat方法的多态效果
    1:人要吃饭
    2:南方人吃炒菜和米饭
    3:北方人吃烩菜和馒头

    1. class Test{
    2. public static void main(String[] args) {
    3. Person southPerson=new SouthPerson();
    4. Person northPerson=new NorthPerson();
    5. southPerson.eat();
    6. northPerson.eat();
    7. }
    8. }
    9. class Person{
    10. public void eat(){
    11. System.out.println("人要吃饭");
    12. }
    13. }
    14. class SouthPerson extends Person{
    15. @Override
    16. public void eat() {
    17. System.out.println("南方人吃炒菜和米饭");
    18. }
    19. }
    20. class NorthPerson extends Person{
    21. @Override
    22. public void eat() {
    23. System.out.println("北方人吃烩菜和馒头");
    24. }
    25. }

    image.png
    4,复习面向对象知识,有问题及时提出