多态:同一种事物在不同时刻表现出来不同状态条件:1.继承2.重写3.父类引用指向子类对象
示例
public abstract class Animal {
int a=10;
int b=8;
public abstract void eat();
public static void show(){
System.out.println("动物");
}
}
public class Cat extends Animal{
@Override
public void eat() {
System.out.println("猫吃鱼");
}
public static void show(){
System.out.println("猫");
}
}
public class Dog extends Animal{
@Override
public void eat() {
System.out.println("狗吃骨头");
}
public void play(){
System.out.println("狗会玩");
}
}
public class Rabbit extends Animal{
int a= 5;
@Override
public void eat() {
System.out.println("兔子吃草");
}
public static void show(){
System.out.println("兔子");
}
}
public class Test {
public static void main(String[] args) {
Animal cat=new Cat();
cat.eat();//猫吃鱼 编译看左边,运行看右边(编译时期类型是父类,运行时期类型是子类)
cat.show();//动物 静态方法编译运行都看左边
Animal rabbit=new Rabbit();
rabbit.eat();//兔子吃草
System.out.println(rabbit.a);//10 成员变量编译运行都看左边
System.out.println(rabbit.b);//8
}
}
public class Test1 {
public static void main(String[] args) {
Test1 test=new Test1();
Animal cat=new Cat();
test.feedAnimal(cat);
Animal rabbit=new Rabbit();
test.feedAnimal(rabbit);
Dog dog=new Dog();
test.feedAnimal(dog);
}
public void feedAnimal(Animal animal){
animal.eat();
Dog d=(Dog) animal;//向下转型
if(animal instanceof Dog){
d.play();
}
}
}
final关键字
final关键字
可以修饰属性,方法,类 修饰属性
修饰的成员变量为常量,值不可以再修改
修饰方法 子类不可以重写被final修饰的方法
修饰类 不可以再被继承
public class Demo1 {
public final static int NUM=0;//用final修饰的成员变量常用static修饰
public final int num1;//定义没有赋值时,应在构造方法中对其赋值
public Demo1(){
num1=0;
}
public Demo1(int num1){
this.num1=num1;
}
public final void show(){
System.out.println("示例");
}
public void show1(){
System.out.println("示例1");
}
}
public class Demo2 extends Demo1{
//public void show(){}不可以重写被final修饰的
public void show1(){
System.out.println("重写");
}
}
接口
示例
public interface MyInterface extends MyInterface1,MyInterface2,MyInterface3{
//定义常量
public static final int NUM=10;
//定义抽象方法
public abstract void show();
public abstract void sleep();
//定义静态方法
public static void showinfo(){
System.out.println("这是静态方法");
}
//定义默认方法
public default void showinfo1(){
System.out.println("这是默认方法");
}//由子类使用
}
public interface MyInterface1 {
void play();
}
public interface MyInterface2 {
void sing();
}
public interface MyInterface3 {
void run();
}
public class MyInterface1Impl implements MyInterface3,MyInterface,MyInterface2,MyInterface1{
@Override
public void show() {
}
@Override
public void sleep() {
}
@Override
public void play() {
}
@Override
public void sing() {
}
@Override
public void run() {
}
public static void main(String[] args) {
MyInterface.showinfo();
}
}
//public abstract class MyInterfaceImpl implements MyInterface{
public class MyInterfaceImpl implements MyInterface{
@Override
public void show() {
}
@Override
public void sleep() {
}
@Override
public void play() {
}
@Override
public void sing() {
}
@Override
public void run() {
}
}
接口与抽象类的异同
public abstract class Animal {
private String color;
private int age;
public Animal() {
}
public Animal(String color, int age) {
this.color = color;
this.age = age;
}
public abstract void eat();
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Dog extends Animal implements Play{
@Override
public void eat() {
System.out.println("狗吃骨头");
}
@Override
public void play() {
System.out.println("狗会玩");
}
}
public class Rabbit extends Animal implements Jump,Play {
@Override
public void eat() {
System.out.println("兔子吃草");
}
@Override
public void jump() {
System.out.println("兔子会跳");
}
@Override
public void play() {
System.out.println("兔子会玩");
}
}
public class Fish extends Animal implements Swim{
@Override
public void eat() {
System.out.println("鱼吃虾米");
}
@Override
public void swim() {
System.out.println("鱼会游");
}
}
public interface Play {
void play();
}
public interface Jump {
void jump();
}
public interface Swim {
void swim();
}
public class Test {
public static void main(String[] args) {
Animal dog=new Dog();
dog.eat();
Animal rabbit= new Rabbit();
rabbit.eat();
Animal fish =new Fish();
fish.eat();
Play p=new Dog();
p.play();
Jump j=new Rabbit();
j.jump();
Swim s=new Fish();
s.swim();
}
}
注:全部代码在一个包中,分为不同的类



