韓順平 java筆記 第9講 第10講 第11講 第12講 抽象 封裝 繼承 多態 方法重載 方法重寫

來源:互聯網
上載者:User

標籤:韓順平   protect   ati   name   xtend   oid   demo1   float   面向   

java物件導向編程的四大特徵

   抽象、封裝、繼承、多態

1.抽象:把一類事物的共同屬性和行為提取出來,形成一個物理類比,這種研究問題的方法。

2.封裝:把抽象出來的資料和對資料的操作封裝在一起,資料被保護在內部,程式的其他部分只有通過被授權的操作(成員方法)

   才能對資料進行操作。

 2.1     public class Demo{

      public static void main(String[] args){

      Clerk clerk1 = new Clerk("小花",24,4567.6f);

      System.out.println("名字是",+ clerk1.name + "薪水" + clerk1.getSal());

    }

   }

   class Clerk {

    public String name;

    private int age;

       private float salary;

    public Clerk(String name,int age,float sal){

      this.name = name;

      this.age = age;

      this.salary = sal;

    }

      public float getSal(){

      return this.salary;

    }

   }

  2.2 存取修飾詞

  存取層級  存取控制修飾符  同類  同包  子類  不同包

    1    public            Y        Y       Y        Y

              2              protect           Y        Y       Y       N  

    3   沒有修飾符       Y   Y       N       N 

    4              private           Y   N  N  N

3.繼承

  在父類中定義這些相同的屬性和方法,所有的子類不需要重新定義這些屬性和方法,之需要extends語句聲明繼承父類

*****子類最多隻能繼承一個父類(指直接繼承)

  java所有類都是Obiect類的子類

  package com.abc;

  public class Demo117{

    public static void main(String[] args){

      Pupil p1 = new Pupil();

      p1.printName();

    }

  }

  class Stu{

    protected int age;

    public String name;

    public float fee;

    private String job;//私人不被繼承,不希望繼承某個屬性和方法 就聲明為private

    public void printName(){

      System.out.println("名字" + this.name);

    }

  }

  class Pupil extends Stu{

    public void pay(float fee){

      this.fee=fee;

    }

  }

  class Pre extends Pupil{

    public void pay(float fee){

      this.fee=fee*1.5f;

    }

  }

4.多態

  4.1方法重載

    (1)方法名相同

    (2)方法的參數類型、個數、順序、至少有一項不同。

    (3)方法的傳回型別可以不同

    (4)方法的修飾符可以不同

  4.2方法重寫

    (1)子類的方法的傳回型別、參數、方法名稱要和父類的一樣

    (2)子類方法不能縮小父類方法的存取權限

  public class Demo{

    public static void main(String[] args){

      Cat cat1 = new Cat();

      cat1.cry();

      Dog dog1 = new Dog();

      dog1.cry();

    }

  } 

  class Animal{

    int age;

    String name;

    public void cry(){

      System.out.println("我是動物,不知道叫");

    }

  }

  class Cat extends Animal{

    public void cry(){

      System.out.println("貓貓叫");

    }

  }

     class Dog extends Animal{

    public void cry(){

      System.out.println("狗叫");

    }

  }

******super.xxx;調用父類XXX的變數或方法引用。

******super.xxx();直接存取調用父類的方法。

******super();調用父類初始化方法public xxx()方法。

    4.3 多態

  public class Demo123{

    public static void main(String[] args){

      Animal an = new Cat();

      an.cry();

      an = new Dog();

      an.cry(); 

      Master master = new Master();

      master.feed(new Dog(),new Bone());

      master.feed(new Cat(),new Fish());

    }

  }

  class Master{

    public void feed (Animal an,Food f);

    an.eat();

    f.showName();

  }

  class Animal (){

    public void eat(){

    }

  }

  class Dog extends Animal (){

    public void eat(){

    }

  }

  class Cat Extends Animal (){

    public void eat(){

    }

  }

  class Food{

    public void show Name(){

    }

  }

  class Fish extends Food{

    public void show Name(){

    }

  }

   class  Bone extends Food{

    public void show Name(){

    }

  }

 

 

 

韓順平 java筆記 第9講 第10講 第11講 第12講 抽象 封裝 繼承 多態 方法重載 方法重寫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.