一、多态的定义
多态的实例:
package com.iflysse.test01;
public class Day031401 {
public static void main(String[] args) {
Person1 p = new man();
if(p instanceof man){
man m = (man) p;
m.work();
}
else{
woman w = (woman)p;
w.shopping();
}
// man m = new man();
// fuction(m);
// woman w = new woman();
// fuction(w);
// }
// public static void fuction(Person1 p){
// p.eat();
}
}
class Person1{
public void eat(){
System.out.println("人都会吃饭。。。");
}
}
class man extends Person1{
public void eat(){
System.out.println("男人喜欢吃肉。。。");
}
public void work(){
System.out.println("男人要勤劳的工作。。。。");
}
}
class woman extends Person1{
public void eat(){
System.out.println("多吃水果和蔬菜。。。");
}
public void shopping(){
System.out.println("买买买。。。。");
}
}
二、final关键字
修饰符:可以修饰类、属性、方法;
- 修饰类:被修饰的类,不可以被继承;
- 修饰方法:被修饰的方法,不可以被重写;
修饰属性:被final所修饰的变量是常量,不能修改;
public class Something{
public static void main(String[] args)){
Other o = new Other();
System.out.println(new Something().addOne(o));
}
public int addOne(final Other o){ //常量是对象o
return o.i++;
}
}
class Other{
public int i=5;
}