传智播客——毕向东25--第7天总结-6

来源:互联网 发布:淘宝商标侵权扣多少分 编辑:程序博客网 时间:2024/05/22 01:55


1.继承:
      关键字:extends
  class Person
   {.....}
  class Student extends Person
   {.....}
  java 只支持单继承

2.子类调用父类成员,函数:关键字 super
    super.num();
    this:代表本类对象的引用
    super:代表父类对象的引用
    class Fu
    {
        int num=4;
     }
    class Zi extends Fu
    {
          void show()
          {
                System.out.println(super.num);
          }
    }
    class Ex
    {
            public static void main(String[] args)
               {
                     Zi z=new Zi();
                     z.show();
                }
     }

3.子类覆盖父类中的方法:
         当子类和父类中的函数一模一样时,父类中的函数被子类覆盖,即:保留父类功能,并重写该功能;
       class Fu
         { void show()
              {
                    System.out.println("我是父类");
                }
          }
        class Zi extends Fu
        {
              void show()
                {
                       System.out.println("我是子类");
               }
        }
        class Test
         {
               public static void main(String[] args)
                  {
                         Zi z=new Zi();
                         z.show();
                   }
          }
  1.子类覆盖父类,必须保证子类权限大于父类权限;
  2.静态只能覆盖静态
记住:
    重载:只看同名函数的参数列表
    覆盖:子类方法要一模一样,包括返回值类型
4. 构造函数--子类实例化过程
      子类构造函数初始化前,会去调用父类构造函数中的无参构造函数,如果父类中没有无参构造函数,那么编译将报错。除非在
      子类构造函数中显示的调用父类构造函数
      class Fu
      {
           Fu(int x)
             {
                  System.out.println("Fu run");
              }
          }
        class Zi
        {
              Zi()
               {
                 // super();//默认隐式存在
                    super(4);
                    System.out.println("Zi run");
                }
             Zi(int x)
               {
                // super();//默认隐式存在
                   super(3);
                  System.out.pintln("zi.."+x);
              }
      }
注意:super语句一定定义在子类构造函数的第一行,this()和super()不能同时出现

5.final关键字:
         final:最终
      1.可以修饰类,函数,变量
      2.被final修饰的类不可以被继承
      3.被final修饰的方法不能被重写
      4.被final修饰的变量,该变量值以后不可变更
      5.内部定义在类的局部位置上时,只能访问该局部被final修饰的局部变量
  final class Demo
   {
        ........;
   }
  final  void show(){ };
  public static final int x=3;//相当于常量,不可变更

6.抽象类:abstract
      1.含有抽象方法的类必须是抽象类
      2.不可用new创建对象
      3.使用冲向类中的抽象方法,要复写该抽象类中的所有方法
     abstract class  Student
      {
     abstract void study();
     abstract void playgame();
       }
     class BaseStudent extends Student
      {
           void study()
             {
                 System.out.println("baseStudy");
              }
            //abstract void playgame(); 可以继续抽象,类名前需加abstract关键字
         void playgame()
         {
                Systemlout.println("playgame");
          }
      }

7.模板方法模式:
       获取一段程序的运行时间:
   abstract class GetTime
     {
            public final void getTime()
               {
                     long static =System.currentTimeMillis();
                     runcode();
                     long end=System.currentTimeMills();
                     System.out.println("毫秒:"+(end-start));
                  }
           public abstract void runcode();
      }
    class SubTinme extends GetTime
      {
             public void runCode()
               {
                   for(int x=0;x<4000;x++)
                      {
                          System.out.println(x);
                       }
                  }
      }
   class Demo
     {
          public static void main(String[] args)
            {
                  SubTime gt=new SubTime();
                  gt.getTime();
              }
      }

8.接口:
   1.格式:interface{ }
   2.接口成员修饰符是固定的
       。成员变量:public static final
       。成员函数:public abstract
   3.接口的出现将“多继承”通过另一种形式体现出来,即“多实现”
接口定义时的格式个点:
     a.接口常见的定义
     b.接口中的成员都有固定的修饰符
    常量:public static final
    方法:public abstract
接口中的成员都是public的
接口不能创建对象,即不能被new
interface Inter
{
     public static final int Num=3;
     public abstract void show();
}
class Test implements Inter
{
     public void show(){};
}
class InterfaceDemo
{
      public static void main(String[] args)
       {
               Test t=new Test();
               System.out.println(t.Num);
               System.out.println(Test.Num);
               System.out.println(Inter.Num);
        }
 }
接口的实现:implements
 1.class Test extends Demo implements Inter,InterA,InterB
 2.interface A
   {
          void method A();
    }
    interface B extends A
     {
             void method B();
      }
    interface C extends B     //interface C extends B,A
    {
               void method C();
     }
    class D implements C
    {
               public void method A(){......}
               public void method B(){......}
               public void method C(){......}
     }

 java中接口与接口之间可以多继承://interface C extends B,A
接口的特点:
 1.接口是对外暴露的规则;
 2.接口是程序的功能扩展;
 3.接口可以用来实现;
 4.类与接口之间是实现关系,且类可以继承一个类的同时实现多个接口;
 5.接口与接口之间可以有继承关系;
abstract class Sporter
{
      abstract void play();
}
interface Study

        ......;
}
class Test extends Sporter implements Study
{
     ......;
}

 

 

原创粉丝点击