標籤:java
1. 建立類 Person
建立對象的記憶體劃分圖:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6E/12/wKiom1VytnKikTlZAAD6vbVLNb8485.jpg" title="QQ20150606165859.jpg" alt="wKiom1VytnKikTlZAAD6vbVLNb8485.jpg" />
對屬性賦值,操作為
對象.屬性=賦值。
class Person{String name;int age;public void tell(){System.out.println("name:"+ name+" "+"age:"+age);}}public class MethodDemo01 {public static void main(String[] args){Person per = null;per = new Person();per.name = "thystar"; //賦值per.age = 22;per.tell();}}
賦值後,堆記憶體空間中添加資料
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6E/12/wKiom1VyuArhLG91AAEE-kBGbe8001.jpg" title="QQ20150606170549.jpg" alt="wKiom1VyuArhLG91AAEE-kBGbe8001.jpg" />
在極客學院上看到的解釋,覺得很好就先記下來:http://www.jikexueyuan.com/course/111_2.html?ss=1
2. 物件導向的編程
類的封裝性:
為了避免類中的屬性被隨意改動,將屬性定義為private,用getter和setter方法調用:右鍵-->Source-->Generate Getters and Setters
class Person{private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void tell(){System.out.println("name:"+ getName()+" "+"age:"+getAge());}}public class MethodDemo01 {public static void main(String[] args){Person per = null;per = new Person();per.setName("thystar");per.setAge(22);per.tell();}}
極客學院改課程網址:http://www.jikexueyuan.com/course/113.html
匿名對象
class Student{public void tell(){System.out.println("Hello Thystar!");}}public class Demo01 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubStudent stu = new Student();stu.tell();// 匿名對象new Student().tell(); //只能使用一次}}
構造方法
class Student{public Student(){System.out.println("Hello thystar");}}public class Demo01 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubStudent stu = new Student();}}
構造方法名稱與類名一致,切沒有傳回值,但是可以傳遞參數。
class Student{String name;int age;public Student(String name, int age){System.out.println("name:"+ name+" "+"age:"+age);}}public class Demo01 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubStudent stu = new Student("thystar", 22);}}
構造方法由系統自動定義,其方法也可以重載。
極客學院改課程網址:http://www.jikexueyuan.com/course/113_3.html?ss=1
引用傳遞
class Ref1{int temp = 10;}public class Demo02 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubRef1 r1 = new Ref1();r1.temp=20;System.out.println(r1.temp);tell(r1);System.out.println(r1.temp);}public static void tell(Ref1 r2){r2.temp = 30;}}
輸出結果:
10
20
30
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6E/10/wKioL1VyxR7Aa3eTAAEmCNpedZY175.jpg" title="QQ20150606175453.jpg" alt="wKioL1VyxR7Aa3eTAAEmCNpedZY175.jpg" />
public class Demo3 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubString str1 = "Hello";System.out.println(str1);tell(str1);System.out.println(str1);}public static void tell(String str){str = "thystar";}}
輸出結果:
Hello
Hello
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6E/10/wKioL1VyxtOAovuHAAFS7whIa9Y783.jpg" title="QQ20150606180125.jpg" alt="wKioL1VyxtOAovuHAAFS7whIa9Y783.jpg" />
極客學院課程網址: http://www.jikexueyuan.com/course/114.html
this關鍵字
使用方法
表示類中的屬性和調用方法
表示本類中的構造方法
表示當前對象
class Person{private String name;private int age;public Person(String n, int a){ this(); //調用本類中的構造方法this.name = n;//表示屬性this.age = a;}public Person(){System.out.println("xxxxxxxxxxxxxx");}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void tell(){System.out.println("name:"+ this.getName()+" "+"age:"+this.getAge());//調用方法}}public class ThisDemo01 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubPerson p = new Person("thystar", 22);p.tell();}}
static 關鍵字
1. 使用static聲明屬性 ——》 static聲明全域屬性
2. 使用static聲明方法——》 直接通過類名調用
3. 注意點: 使用static方法的時候,只能訪問static聲明的屬性和方法,而非static聲明的屬性和方法是不能訪問的。
靜態屬性和方法要用類名直接進行調用。
極客學院地址:http://www.jikexueyuan.com/course/114_3.html?ss=2
Java物件導向小記(1)