java封裝、構造方法、this

來源:互聯網
上載者:User

標籤:java

一、封裝

特性:安全、將變化隔離、便於使用、提供重複性

 

例子

[[email protected] java]# cat EmployeeDemo.java class Employee {    private String id;    private String name;    private String gender;     //提供公有的set和get方法    //public 成員修飾符,公用的,誰都可以訪問    //private 成員修飾符,私人的,只有自己可以訪問     public void setName(String n) {        name = n;    }     public void setId(String i) {        id = i;    }        public void setGender(String gen) {        if ("male".equals(gen) || "female".equals(gen)) {            gender =gen;        } else {            System.out.println("please input \"male\" or \"female\"");          }     }         public String getName() {        return name;    }     public String getId() {        return id;    }     public String getGender() {        return gender;    }      public void work() {        System.out.println(id +":" +name +":" +gender +":" +"working");    } } public class EmployeeDemo {public static void main(String[] args) {         //建立對象        Employee tom = new Employee();         //調用公有方法,給成員賦值變數        tom.setName("tom");        tom.setId("1");        tom.setGender("male");         //擷取執行個體變數的值        System.out.println(tom.getName());        System.out.println(tom.getId());        System.out.println(tom.getGender());         //調用成員方法        tom.work();    } } //運行結果 [[email protected] java]# java EmployeeDemo tom1male1:tom:male:working

 

[[email protected] java]# cat Demo.java class Calculator {    public String name = "my Carculator";    public double num1;    public double num2;    public char option;     public void init(double a, char op, double b) {        num1 = a;        num2 = b;        option = op;    }      public void calculate() {        switch (option) {            case '+': System.out.println(name +":" +num1 +"+" +num2 +"=" +(num1+num2));                      break;            case '-': System.out.println(name +":" +num1 +"-" +num2 +"=" +(num1-num2));                      break;            case '*': System.out.println(name +":" +num1 +"*" +num2 +"=" +(num1*num2));                      break;            case '%': System.out.println(name +":" +num1 +"%" +num2 +"=" +(num1%num2));                      break;            case '/': if (num2 != 0) {                             System.out.println(name +":" +num1 +"/" +num2 +"=" +(num1/num2));                         } else {                             System.out.println("num2 can not be 0 !");                              }                         break;            default : System.out.println("null");        }      } } class Demo {    public static void main(String[] args) {        Calculator cal = new Calculator();        cal.init(10 ,'%' ,3);        cal.calculate();        System.out.println("bye");    } } //運行結果 [[email protected] java]# java Demomy Carculator:10.0%3.0=1.0bye


  

二、構造方法

1. 定義:

對象建立出來時,沒有值:


對象建立出來時,有值:


構造方法作用:對對象進行初始化

      

  

2. 建構函式的特點:

1)在對象建立時由jvm調用,給對象初始化

2)在建立對象時調用

3)函數名與類名一致

4)沒有傳回值類型

5)當類中沒有定義建構函式時,系統預設加上一個空參數的建構函式,有則覆蓋

6)一個類中可以定義多個建構函式,以進行不同的初始化,以重載的形式體現

 

 

3. 構造代碼塊的特點:

1)給所有的對象進行統一初始化

2)建立對象時就運行,優先於建構函式

3)一般用於將所有構造方法中公用的資訊抽取出來

4)與建構函式區別:全部對象和部分對象的區別

 

例子

[[email protected] java]# cat Demo9.java class Boy {  String name;  int age;  String gender;    // 構造代碼塊,給所有對象進行初始化  {       System.out.println("哭。。。");  }       Boy() {       System.out.println("無參構造");  }   Boy(String n, int a, String g) {       name = n;       age = a;       gender = g;       System.out.println("有參構造");  }   void run() {       System.out.println("跑...");  } } public class Demo9 {  public static void main(String[] args) {       System.out.println();       Boy b = new Boy();       Boy b2 = new Boy("jack", 1, "男");  }} //運行結果[[email protected] java]# java Demo9 哭。。。無參構造哭。。。有參構造


 

三、this關鍵字

1)表示所在函數所屬對象的引用

2)this只能在非靜態中(沒有static修飾)函數使用

3)建構函式間相互調用必須放在建構函式的第一個語句中

4)可以解決建構函式中對象屬性和函數形參的同名問題

 

class Person {       String name;       int age;        Person() {       }        Person(String n){              name=n;       }        Person(String n, int a) {              //建構函式不能直接通過函數名直接調用,編譯報錯!!!              Person(n);              age = a;       }}

 

例子

//在建構函式中列印this與建立對象時列印對象名p,結果一樣,說明this和p是一樣,都是記憶體位址值class Student {    String name;    String gender;    int age;     Student() {    }     Student(String name) {       this();       this.name = name;    }     Student(String name, String gender, int age) {       this(name);       System.out.println(this);  //[email protected]       this.gender = gender;       this.age = age;    }     void speak() {       run();       System.out.println("姓名:" + name + " 性別:" + gender + " 年齡:" + age              + " 嘿嘿");    }     void run() {       System.out.println("run.....");    } } class Demo2 {    public static void main(String[] args) {       Student p = new Student("tom", "male", 28);       System.out.println(p); // [email protected]       p.speak();    }} //運行結果[[email protected] java]# java Demo2[email protected][email protected]run.....姓名:tom 性別:male 年齡:28 嘿嘿


 


java封裝、構造方法、this

聯繫我們

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