zoukankan      html  css  js  c++  java
  • Java多态

    package com.oop;
    
    import com.oop.demo06.Person;
    import com.oop.demo06.Student;
    
    public class Application {
        public static void main(String[] args) {
    
            //一个对象的实际类型是确定的
            //new Person();
            //new Student();
    
            //可以指向的引用类型就不确定了 : 父类的引用指向子类
    
            //Student 能调用的方法都是自己或者继承父类的!
            Student s1 = new Student();
            //Person 父类型,可以指向子类,但不能调用子类独有的方法!
            Person s2 = new Student();
            Object s3 = new Student();
    
            s2.run();//子类重写了父类的方法,执行子类的方法
            s1.run();
    
            //对象能执行那些方法,主要看对象左边的数据类型,和右边关系不大!
            //s2.eat();//Person没有eat()方法,引用失败
            s1.eat();
            ((Student) s2).eat();
        }
    }
    
    package com.oop.demo06;
    
    public class Person {
        public void run(){
            System.out.println("run");
        }
    }
    
    /*
    多态注意事项:
    1. 多态是方法的多态,属性没有多态
    2. 父类和子类,有联系   类型转换异常! ClassCastException!
    3. 存在条件:继承关系,方法需要重写,父类引用指向子类对象!  Father f1 = new Son();
    
        1. static 方法,属于类,它不属于实例
        2. final 常量;
        3. private 方法;
     */
    
    package com.oop.demo06;
    
    public class Student extends Person{
        @Override
        public void run() {
            System.out.println("son");
        }
        public void eat(){
            System.out.println("eat");
        }
    }
    

    instanceof (类型转换) 引用类型,判断一个对象是什么类型,判断子父类之间的关系

    package com.oop;
    
    import com.oop.demo06.Person;
    import com.oop.demo06.Student;
    import com.oop.demo06.Teacher;
    
    public class Application {
        public static void main(String[] args) {
    
            //Object > String
            //Object > Person > Teacher
            //Object > Person > Student
            Object object = new Student();
    
            //System.out.println(X instanceof Y);  //能不能编译通过! X 与 Y 之间的关系
    
            System.out.println(object instanceof Student);  //true
            System.out.println(object instanceof Person);   //true
            System.out.println(object instanceof Object);   //true
            System.out.println(object instanceof Teacher);  //false
            System.out.println(object instanceof String);   //false
            System.out.println("======================");
            Person person = new Student();
            System.out.println(person instanceof Student);  //true
            System.out.println(person instanceof Person);   //true
            System.out.println(person instanceof Object);   //true
            System.out.println(person instanceof Teacher);  //false
            //System.out.println(person instanceof String);   //编译报错!
            System.out.println("=======================");
            Student student = new Student();
            System.out.println(student instanceof Student);  //true
            System.out.println(student instanceof Person);   //true
            System.out.println(student instanceof Object);   //true
            //System.out.println(student instanceof Teacher);  //编译报错!
            //System.out.println(person instanceof String);   //编译报错!
        }
    }
    
    package com.oop.demo06;
    
    public class Person {
    }
    
    package com.oop.demo06;
    
    public class Student extends Person{
    }
    
    package com.oop.demo06;
    
    public class Teacher extends Person {
    }
    



    package com.oop;
    
    import com.oop.demo06.Person;
    import com.oop.demo06.Student;
    import com.oop.demo06.Teacher;
    
    public class Application {
        public static void main(String[] args) {
            //类型之间的转换: 父  子
    
            //高                 低
            Person obj = new Student();
    
            //obj 将这个对象转换为Student类型,我们就可以使用Student类型的方法了!
    
            //方式一
            Student student = (Student) obj;
            student.go();
            //方式二
            ((Student) obj).go();
    
            //子类转换为父类,可能丢失自己的本来的一些方法!
            Teacher teacher = new Teacher();
            teacher.to();
            Person person = teacher;
            //person.to(); //提示没有 to() 方法
        }
    }
    
    /*
    1. 父类引用指向子类的对象
    2. 把子类转换为父类,向上转型
    3. 把父类转换为子类,向下转型:强制转换
    4. 方便方法的调用,减少重复的代码!简洁
    
        封装、继承、多态!   抽象类、接口
    
     */
    
    package com.oop.demo06;
    
    public class Person {
        public void run(){
            System.out.println("run");
        }
    }
    
    package com.oop.demo06;
    
    public class Student extends Person{
        public void go(){
            System.out.println("go");
        }
    }
    
    package com.oop.demo06;
    
    public class Teacher extends Person {
        public void to(){
            System.out.println("to");
        }
    }
    
  • 相关阅读:
    HDU 5486 Difference of Clustering 图论
    HDU 5481 Desiderium 动态规划
    hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值
    HDU 5478 Can you find it 随机化 数学
    HDU 5477 A Sweet Journey 水题
    HDU 5476 Explore Track of Point 数学平几
    HDU 5475 An easy problem 线段树
    ZOJ 3829 Known Notation 贪心
    ZOJ 3827 Information Entropy 水题
    zoj 3823 Excavator Contest 构造
  • 原文地址:https://www.cnblogs.com/Notesdata/p/14163083.html
Copyright © 2011-2022 走看看