java的多态性

                                                                                                     java的多态性

public class DuoTaiTest {
/*
* 多态:就是指某一个事物,在不同时刻表现出来的不同状态
* 猫 m = new 猫();
* 狗 d = new 狗();
* Animal a = new Animal();
* Animal a1 = new 猫();
* Animal a2 = new 狗();
*
* 多态的前提和体现:
* 1.有继承关系(让类与类产生关系)
* 2.有方法重写(如果没有,就不能体现出猫和狗不同的动作特点)
* 3.有父类引用指向子类的对象
* 父类 对象名 = new 子类();
* 多态的分类:
* 1.具体类多态
* class 父类{ ... }
* class 子类 extends 父类{...}
* 父类 对象名 = new 子类();
* 2.抽象类多态(还算常用)
* abstract class 父类{...}
* class 子类 extends 父类{...}
* 父类 对象名 = new 子类();
* 3.接口多态(非常常用)
* interface 父类{...}
* class 子类 implements 父类{...}
*
* 父类 对象名 = new 子类();
*
* 多态中成员访问特点:
* 1.成员变量:
* 只能访问父类的成员变量(既然装,就要装到底)
* 2.构造方法:
* 子类的构造方法都会默认访问父类构造方法
* 目的:对父类的数据进行初始化
* 3.成员方法:
* 调用子类的成员方法(前提是成员方法要有重写)
* 4.静态方法:
* 静态方法不算重写,访问的还是父类方法。
* 多态的有点:
* 1.提高类代码的复用性(继承来保证)
* 2.提高了代码的扩展性(多态来保证)
* 多态的缺点:
* 父类不能访问子类特有的功能(子类所有的成员变量,独有方法)
* 怎么解决: 子类可以当父类用,父类不能当子类用?
* 转型:
* 1.向上转型 :从子到父
* 2.向下转型 :从父到子
* 用多态
* 1.向上转型:把父亲的属性打印出来
* 把三个儿子的相同方法的独特的变现方式打印出来
* 把爸爸的独有方法打印出来
* 2.向下转型:把三个儿子独有的属性打印出来
* 把三个儿子独有的方法打印出来
*/
public static void main(String[] args) {
// Cat c = new Cat();
// c.eat();
// c.sleep();

// Horse h = new Horse();
// h.eat();
// h.sleep();
Animal a1 = new Cat();//转型:向上转型
// a1.eat();
// a1.sleep();

// Animal.show(a1);
a1.show2();
Cat cc = (Cat)a1;//转型:向下转型
cc.call();
System.out.println(cc.a);
System.out.println("---------------------");
a1 = new Dog();
// a2.eat();
// a2.sleep();

// Animal.show(a1);
a1.show2();
System.out.println("---------------------");
a1 = new Horse();
// a3.eat();
// a3.sleep();

// Animal.show(a1);

a1.show2();
// System.out.println("cat----"+a1.a);
// System.out.println("dog----"+a2.a);

// a1.eat();
// a2.eat();

// a1.call();
// a1.drink();
// a2.drink();


}
}
class Animal{
int a = 100;
private String name;
private int age;

// public Animal(){
// System.out.println("父类无参");
// }
public Animal(int a){
System.out.println("父类有参");
}

public void eat(){
System.out.println("动物吃东西");
}

public void sleep(){
System.out.println("动物睡觉");
}
public static void show(Animal a){
a.eat();
a.sleep();
}
public void show2(){
eat();
sleep();
}
public static void drink(){
System.out.println("时代在变迁,猩猩会抽烟");
}
}

class Cat extends Animal{
int a = 10;
private boolean sex;

public Cat(){
super(5);
System.out.println("Cat类无参");
}
public Cat(int a){
super(5);
System.out.println("Cat类有参");
}

public void eat(){
System.out.println("猫吃鱼");
}
public void sleep(){
System.out.println("猫是侧着睡觉");
}
public void call(){
System.out.println("喵~喵~喵~");
}

public static void drink(){
System.out.println("111时代在变迁,猩猩会抽烟");
}
}

class Dog extends Animal{
int a = 15;
private boolean sex;
public Dog(){
super(5);
System.out.println("Dog类无参");
}
public Dog(int a){
super(5);
System.out.println("Dog类有参");
}
public void eat(){
System.out.println("狗吃肉");
}
public void sleep(){
System.out.println("狗是趴着睡觉");
}
public void call(){
System.out.println("汪!汪!汪!");
}

public static void drink(){
System.out.println("222时代在变迁,猩猩会抽烟");
}
}
class Horse extends Animal{
int a = 15;
private boolean sex;
public Horse(){
super(5);
System.out.println("Horse类无参");
}
public Horse(int a){
super(5);
System.out.println("Horse类有参");
}
public void eat(){
System.out.println("马吃草");
}
public void sleep(){
System.out.println("马是站着睡觉");
}
public void call(){
System.out.println("吁!吁!吁!");
}

public static void drink(){
System.out.println("222时代在变迁,猩猩会抽烟");
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值