1,结合多态发生的条件,及继承相关知识,自己总结并验证下,哪些方法无法实现多态效果。
| 多态条件 | 1,必须要有两个类继承父子类 | 2,必须发生方法的重写 |
|---|---|---|
| 无法实现多态的方法 | 构造方法不能被继承 |
Final不能被重写 |
| Static不能重写 | ||
| Private不能被重写 |
2,自己定义一个类,类中定义3个成员变量,这3个成员变量都被final修饰,
请用3种不同方式,为这3个被final修饰的成员变量赋值。
class Test02{public static void main(String[] args) {Student student=new Student(18);System.out.println(student.name);System.out.println(student.age);System.out.println(student.gender);}}class Student{final String name="张三";//显式赋值final int age;final String gender;{this.gender="男";//gouzaodaima赋值}public Student(int age) {//构造器赋值this.age = age;}}

3,实现如下多态案例,分别定义Person类,SouthPerson(表示南方人),NorthPerson(表示北方人)
Person
eat()
SouthPerson
eat()
NorthPerson
eat()
写代码实现,eat方法的多态效果
1:人要吃饭
2:南方人吃炒菜和米饭
3:北方人吃烩菜和馒头
class Test{public static void main(String[] args) {Person southPerson=new SouthPerson();Person northPerson=new NorthPerson();southPerson.eat();northPerson.eat();}}class Person{public void eat(){System.out.println("人要吃饭");}}class SouthPerson extends Person{@Overridepublic void eat() {System.out.println("南方人吃炒菜和米饭");}}class NorthPerson extends Person{@Overridepublic void eat() {System.out.println("北方人吃烩菜和馒头");}}

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