JAVA学习--面向对象的特征三:多态…

来源:互联网 发布:冒名卡出售 淘宝交易 编辑:程序博客网 时间:2024/06/02 01:07

 * 面向对象的特征三:多态性
 * 1.多态性指的是什么?多态性,可以理解为一个事物的多种表型形态。
  1)方法的重载与重写  2)子类对象的多态性
 *
 * 2.子类对象的多态性使用的前提:①要有类的继承②要有子类对父类方法的重写
 *
 * 3.程序运行分为编译状态和运行状态。
  对于多态性来说,编译时,"看左边",将此引用变量理解为父类的类型
  运行时,"看右边",关注于真正对象的实体:子类的对象。那么执行的方法就是子类重写的。
   
 * 4.子类对象的多态性,并不使用于属性。
、、


public class TestPerson {
    publicstatic void main(String[] args) {
       Person p =new Person();
      p.eat();
      p.walk();

       Man m = newMan();
      m.eat();
      m.walk();
      System.out.println();

       //子类对象的多态性:父类的引用指向子类对象
       Person p1 =new Man();// 向上转型
       //虚拟方法调用:通过父类的引用指向子类的对象实体,当调用方法时,实际执行的是子类重写父类的方法
      p1.eat();
      p1.walk();
      System.out.println("$" + p1.id);//1001

//       p1.smoking =null;
       //p1.entertainment();

       Person p2 =new Woman();
      p2.eat();
      p2.walk();
       //p2.shopping();
       Woman w =(Woman) p2;// 向下转型,使用强转符:()
      w.shopping();

       //java.lang.ClassCastException
       // Woman w1= (Woman)p1;
       //w1.shopping();

       // Woman w2= (Woman)new Man();
       //instanceof:
       // 格式: 对象ainstanceof 类A:判断对象a是否是类A的一个实例.是的话,返回true;否则返回false
       //若a是A类的实例,那么a也一定是A类的父类的实例。
       if (p1instanceof Woman) {
         System.out.println("hello!");
          Woman w1 =(Woman) p1;
         w1.shopping();
       }

       if (p1instanceof Man) {
          Man m1 =(Man) p1;
         m1.entertainment();
       }

       if (p1instanceof Person) {
         System.out.println("你好!");
       }

    }

    public voidshow(Person p) {//Person p = new Man();

    }



class Person {
    privateString name;
    private intage;
    int id =1001;
    publicPerson() {
      super();
    }
    publicPerson(String name, int age) {
      super();
       this.name =name;
       this.age =age;
    }
    publicString getName() {
       returnname;
    }
    public voidsetName(String name) {
       this.name =name;
    }
    public intgetAge() {
       returnage;
    }
    public voidsetAge(int age) {
       this.age =age;
    }
   
    public voidwalk(){
      System.out.println("人走路");
    }
    public voideat(){
      System.out.println("人吃饭");
    }

}




class Man extends Person{
    boolean smoking;
    int id = 1002;
    public Man(){
      super();
    }
   
    publicMan(boolean smoking) {
      super();
       this.smoking= smoking;
    }

    publicboolean isSmoking() {
       returnsmoking;
    }

    public voidsetSmoking(boolean smoking) {
       this.smoking= smoking;
    }
   
    public voidwalk(){
      System.out.println("男人笔挺的走路");
    }
    public voideat(){
      System.out.println("男人应该多吃肉!");
    }
   
    public voidentertainment(){
      System.out.println("男人请客");
    }
}


class Woman extends Person{
    privateboolean isBeauty;

    publicboolean isBeauty() {
       returnisBeauty;
    }

    public voidsetBeauty(boolean isBeauty) {
      this.isBeauty = isBeauty;
    }

    publicWoman() {
      super();
    }

    publicWoman(boolean isBeauty) {
      super();
      this.isBeauty = isBeauty;
    }
    public voidwalk(){
      System.out.println("女人窈窕的走路。。。");
    }
   
    public voideat(){
      System.out.println("女人应该少吃,减肥");
    }
    public voidshopping(){
      System.out.println("女人爱购物");
    }
   
}
0 0
原创粉丝点击