java物件導向-1

來源:互聯網
上載者:User

標籤:java   物件導向   this   

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/8A/9C/wKioL1g1ZkKQL7BnAAAqq1t6FqE071.png" title="1.png" alt="wKioL1g1ZkKQL7BnAAAqq1t6FqE071.png" />

代碼以及筆記如下:

class Person{//成員變數String name;Integer age;//靜態變數static String address="中國";//構造方法//兩個構造方法在同一個類中,又同名,只是參數不同//這種方法現象,就是方法重載。//如果是父類有一個方法,子類也有一個一模一樣的,連//參數也一樣的方法,只是實現這個方法的語句(方法代碼塊)//不同,這種方法現象,就是方法重寫。public Person(){//預設構造方法/*預設構造方法是可以不寫的,但是當你寫了自訂構造方法時,預設構造方法就會被覆蓋。也就是說,如果你寫了下面那個構造方法,而不屑這個空構造方法那麼:Person p = new Person();這條語句就會出錯,因為new Person()表示調用空參數的構造方法。要想成功就要寫為:Person p = new Person("lisi",23);這條語句是說調用帶兩個參數的並且第一個是String類型,第二個是Integer類型。*/}public Person(String name,Integer age){//帶參數自訂構造方法/*其中this指標表示本對象,例如:當我們要使用類中age和name變數時,是這樣使用的:Person p=new Person();p.name="zhangsan";p.age=25;而this就是指的當類產生執行個體時的那個執行個體,上述例子就代表對象p;*/System.out.println("自訂構造方法");this.name=name;this.age=age;}//成員方法public void eat(){System.out.println("吃飯...");}public void toPrint(){System.out.println("name="+this.name+"\nage="+this.age);}//靜態方法public static void staticFun(){System.out.println("靜態方法....");}}class Main{public static void main(String[] args){Person person = new Person();System.out.println("*************");Person person1 = new Person("zhangsan",25);System.out.println("*************");person.name="lisi";person.age=12;person.eat();person.toPrint();System.out.println("*************");person1.eat();person1.toPrint();/*靜態是表示,static所修飾的方法或者變數或者其它等等,屬於類,也就是說,static修飾的不需要這樣:Person person = new Person();person.staticFun();而是直接可以通過類訪問:Person.staticFun();*///前四句,是傳統的訪問方式//後兩句是直接通過類訪問。person.staticFun();System.out.println(person.address);person1.staticFun();System.out.println(person1.address);Person.staticFun();System.out.println(Person.address);}}

運行:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/8A/A0/wKiom1g1Zq3DxeQFAAAOkhfywf4630.png" title="無標題.png" alt="wKiom1g1Zq3DxeQFAAAOkhfywf4630.png" />

本文出自 “夕下等花逝” 部落格,請務必保留此出處http://sunshine2624.blog.51cto.com/3959438/1875911

java物件導向-1

聯繫我們

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