自學java基礎(三)

來源:互聯網
上載者:User

標籤:成員變數   src   rac   running   記憶體   ide   定義類   參數   this   

 

 

 

 

1、物件導向程式設計的三大基本特種:封裝,繼承,多態。

    封裝:將資料和方法放在一個類中。

2、定義類:

修飾符 class 類的名字{   類的內容}

  定義方法:(方法定義不能嵌套,不能在一個方法中定義另一個方法。)

修飾符 傳回值類型 方法名字([參數1,參數2,參數n]){  方法體;}

產生對象:

public class Person{}Person p1 =  new Person();

例子:

public class Test{    //兩個整數相加的方法    public int add(int a, int b)    {        return a + b;    }    //兩個整數相減的方法    public int subtract(int a, int b)    {        return a - b;    }    //兩個整數相乘的方法    public int multiply(int a, int b)    {        return a * b;    }    //兩個整數相除的方法    public int divide(int a, int b)    {        return a / b;    }    //使用void關鍵字表示方法不傳回值    public void output()    {        System.out.println("Hello World");    }    public static void main(String[] args)    {        Test test = new Test();        int x = 8;        int y = 3;                int a = test.add(x, y);        int b = test.subtract(x, y);        int c = test.multiply(x, y);        int d = test.divide(x, y);        System.out.println(a);        System.out.println(b);        System.out.println(c);        System.out.println(d);        test.output();                }}

 

3、參考型別。一個對象可以被多個引用所指向,同一時刻同一個引用只能指向一個對象。多個引用同時指向一個對象,那麼無論哪一個引用改變對象的成員變數,都會反映在其他的引用上。

     如果一個類中包含了屬性和方法,那麼該類產生的每一個對象都維護一個自己的對象,但同時擁有同一個方法。

 

public class People{  int age=20;  public void change(People people){   people=new People();   people.age=30;   System.out.println(age);//20   System.out.println(people.age);//30}  public static void main(String[] args){   People people=new People();   int age =people.age;//傳遞的是原生資料類型,直接賦值。   System.out.println(age);//20   people.change(people);   int age2=people.age;   System.out.println(age2);//20}}

 

 

4、構造方法:用於完成對象的初始化。

    特點:一:名字必須與類名相同。二:沒有傳回值,也不能出現void。三:若類中沒有構造方法,java預設添加為空白的構造方法。若聲明了構造方法,則java編譯器就不會再添加構造方法。四:通過new隱式調用

    new產生對象的三步:為對象開闢記憶體空間,調用類的構造方法,將產生對象的地址返回。

public class ParamTest{    public static void main(String[] args)    {        Person person = new Person();        person.change(person);        int age = person.age;//20        System.out.println(age);        int i = 10;        person.change2(i);        System.out.println(i);//10        System.out.println(person.age);//40        System.out.println(age);//20    }}class Person{    int age = 20;    public void change(Person person)    {        person = new Person();                person.age = 30;    }    public void change2(int age)    {        this.age = 40;    }}

 

8,、構造方法重載:只需要看參數。如果在一個構造方法中調用另外一個構造方法,可以使用this()的方式調用。this()括弧中的參數代表目標構造方法的參數。this()必須作為構造方法的第一句。

public class ConstructorOverload{    public ConstructorOverload()    {        this(3);                System.out.println("test");    }    public ConstructorOverload(int i)    {        System.out.println(++i);    }    public static void main(String[] args)    {        ConstructorOverload col = new ConstructorOverload();    }}
//4 test

 

 

9、繼承。java是單繼承。產生子類對象時,java預設首先調用父類不帶參數的構造方法,產生父類對象,然後調用子類構造方法,產生子類對象。

public class InheritenceTest3{    public static void main(String[] args)    {        Son son = new Son();    }}class Grandpa{    public Grandpa()    {        System.out.println("grandpa");    }}class Father extends Grandpa{    public Father()    {        System.out.println("father");    }}class Son extends Father{    public Son()    {        System.out.println("son");    }}//grandpa  father  son

 

 

10、super關鍵字,表示對父類對象的引用。可以使子類調用父類帶參數的構造方法,必須放在第一行(目的是保證先產生父類對象)。

      當父類沒有無參的構造方法的時候,必須使用super關鍵字顯示調用父類的構造方法。

public class InheritenceTest2{    public static void main(String[] args)    {        Dog dog = new Dog();        dog.run();    }    }class Animal{    public void run()    {        System.out.println("animal is running");    }    public void run(int i)    {        System.out.println("animal is sleepping");    }}class Dog extends Animal{    public void run()    {            System.out.println("dog is running");        super.run(1); //調用父類的run方法    }}//dog is runninganimal is sleepping

 

自學java基礎(三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.