对象的四大特征(下)--抽象和多态

来源:互联网 发布:打卡是什么意思网络 编辑:程序博客网 时间:2024/06/03 19:51

3、抽象

3.1   抽象的关键字是abstract,被abstract修饰的方法/类就是抽象方法和抽象类

3.2  有抽象方法的类一定是抽象类

3.3  abstarct 不能跟final、private、static关键字共存,因为抽象类一定是父类,而final修饰的类不能有子类,private修饰的方法是私有方法,不能被重写,而abstract方法就是需要被重写的,若static修饰抽象方法,则调用该方法时直接用类名调用,导致抽象方法运行没意义

3.4 模板方式设计模式,如下代码:

abstract class GetTime{      public final void getTime(){          long start=System.currentTimeMillis();        runcode();          long end=System.currentTimeMillis();          System.out.println("程序运行 "+(end-start)+"毫秒");      }      public abstract void runcode();  }  class SubTime extends GetTime{      public void runcode(){          for(int i=0;i<15;i++){        try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}            System.out.println(i);          }      }  }  public class TemplateDemo{      public static void main(String[] args) {          SubTime st=new SubTime();          st.getTime();      }  }
3.5 若抽象类中的方法都是抽象方法,那么该类可以通过接口定义,定义接口的关键字是interface

      接口的特征如下:

  1、一个类可以实现多个接口

  2、接口的常量默认值修饰符是public  static final ,  接口的方法默认修饰符是public abstract

  3、只有子类将接口中的所有方法重写后,才能让子类实例化,否则子类只能是一个抽象类

  4、接口和接口可以继承,且可以多继承

举例如下:

interface A{      void methodA();  }  interface B{      void methodB();  }  interface C extends A,B //接口间可以多继承,C接口中有methodA和methodB方法  {      void methodC();  }  interface D{      void methodD();  }  class E{      void methodE(){          System.out.println("Fu E");      }  }  class F extends E implements C,D//继承一个类的同时实现2个接口,需要重写4个方法  {      public void methodA(){          System.out.println("interface A");      }      public void methodB(){          System.out.println("interface B");      }      public void methodC(){          System.out.println("interface C");      }      public void methodD(){          System.out.println("interface D");      }   }  public class InterfaceDemo{        public static void main(String[] args) {      F f = new F();    f.methodA();    f.methodB();    f.methodC();    f.methodD();    f.methodE();        }  } 

需要明白的是 继承之间父类和子类是 is a的关系,实现接口后类和接口是like a的关系,也可以理解成扩展功能

4、多态

class Animal{}class Cat extends Animal{}class Dog extends Animal{}

4.1 多态又叫【动态绑定】,是指运行期间(非编译期间)来判断所引用对象的实际类型,根据实际类型调用其方法

4.2 实现多态要满足三个条件:1、要有继承  2、要有重写 3、父类引用指向子类对象
4.3 多态对象转换,只允许父类引用指向子类对象【Animal a = new Cat()】,不允许子类引用指向子类对象【Cat c = new Anamla()】

4.4如果用instanceof 运算符已判断父类的引用指向子类实例,就可以转换该引用

if(a instanceof Cat){    Cat c = (Cat)c}

多态例子如下:

interface fly{public void toFly();public void toRun();}class Animal implements fly{String name="动物";public void sound(){System.out.println("动物叫声");}@Overridepublic void toFly() {System.out.println("动物是否会飞");}@Overridepublic void toRun() {System.out.println("动物是否会跑");}void show(){System.out.println(name);System.out.println(this.toString());}}class Cat extends Animal implements fly{String name="猫";public void sound(){System.out.println("猫叫声");}@Overridepublic void toFly() {System.out.println("猫不会飞");}@Overridepublic void toRun() {System.out.println("猫会跑");}void catchMouse(){System.out.println("猫会抓老鼠");}}public class TestAnimal {public static void main(String[] args) {//父类引用指向子类对象Animal a = new Cat();//运行时调用子类sound方法a.sound();a.toFly();a.toRun();//因为只有一个对象Cat,所以结果是StudyFiles.Cat@3a6646fca.show();//因为catchMouse方法是Cat方法,所以a对象无法使用,否则编译报错//a.catchMouse();//把a指向的对象强制转换成Cat实例Cat c1 = (Cat)a;c1.catchMouse();Cat c = new Cat();//调用父类show方法,s使用就近原则,取值是动物c.show();}}
public class duotaiDemo {public static void main(String[] args) {Father a = new Son();a.b();}}class Father {public void a() {System.out.println("Father---a");}public void b() {System.out.println("Father---b");a();}}class Son extends Father {public void a() {System.out.println("Son----a");}public void b() {System.out.println("Son----b");/* * 此处执行的是Father类的b()函数,这里明确指明是调用Father类方法区的b()函数,而在父类的b()方法中再调用a()方法时 * 执行的是Son类对象中的a()方法,因为Son类复写了a()方法,此时自动通过this引用变量可以找到子类对象的a() 方法 */super.b(); //此处打印的是子对象信息,System.out.println(super.toString());}}




 

0 0
原创粉丝点击