一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java 面向對象和封裝全面梳理總結

Java 面向對象和封裝全面梳理總結

2022-03-06 00:46代碼小k Java教程

面向對象乃是Java語言的核心,是程序設計的思想,在面向對象程式設計方法中,封裝(英語:Encapsulation)是指一種將抽象性函式接口的實現細節部分包裝、隱藏起來的方法。封裝可以被認為是一個保護屏障,防止該類的代碼和數據

關于面向對象和封裝的個人理解

類和對象

類:對事物的一種描述(具有共同屬性和行為的事物的抽象),例如手機,屬性:品牌價格,行為:玩游戲,刷vx;

對象:客觀存在(在java中體現就是mian方法里面用類定義一個對象,然后用對象去調用方法或者調用成員變量)

二者關系:類為屬性行為抽象,對象則為實體。

對象內存圖理解:堆內存開辟空間,成員變量出現 并產生默認初始化值,將對象地址值記錄以便于通過對象名調用成員變量。

成員變量和局部變量的區別:類中位置不同,內存中位置不同,生命周期不同,初始化值不同(成員變量(有默認初始化值)局部變量(沒有默認初始化值,必須先定義,賦值才能使用)。

封裝

private關鍵字:被private修飾的成員,只能在本類進行訪問,針對private修飾的成員變量,如果需要被其他類使用,提供相應的操作(get,set方法)

this關鍵字:this修飾的變量用于指代成員變量,其主要作用是(區分局部變量和成員變量的重名問題)。

封裝理解: 將類的某些信息隱藏在類內部,不允許外部程序直接訪問,而是通過該類提供的方法來實現對隱藏信息的操作和訪問(private修飾和get,set方法)

封裝的好處以及作用: 把代碼用方法進行封裝,提高了代碼的復用性, 通過方法來控制成員變量的操作,提高了代碼的安全性。

難題匯總

銀行賬戶

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package test3;
 
public class bank {
    public static void main(String[] args) {
        //在測試類Bank中創建銀行賬戶類對象和用戶類對象,
        // 并設置信息,與顯示信息
        Customer customer = new Customer("李華","123456789","987456321","新華小區");
        Account  account = new Account(1111115646,1000000,customer);
        customer.say();
        account.withdraw( 10000 );
        account.save( 9999999 );
        System.out.println(customer.say());
        System.out.println(account.getinfo());
        if (account.withdraw( 10000 )==true){
            System.out.println("取錢成功");
            System.out.println("余額還有"+account.getBalance());
        }else{
            System.out.println("取款失敗");
        }
        if (account.save( 444444 )==true){
            System.out.println("存款成功");
            System.out.println("余額還有"+account.getBalance());
        }else{
            System.out.println("存款失敗");
        }
    }
}
package test3;
/*2.定義銀行賬戶類Account,有屬性:卡號cid,余額balance,所屬用戶Customer 
銀行賬戶類Account有方法:(1)getInfo(),返回String類型,返回卡的詳細信息
(2)取錢方法withdraw(),參數自行設計,如果取錢成功返回true,失敗返回false
(3)存錢方法save(),參數自行設計,如果存錢成功返回true,失敗返回false 
 其中Customer類有姓名、身份證號、聯系電話、家庭地址等屬性    Customer類有方法say(),
 返回String類型,返回他的個人信息。?在測試類Bank中創建銀行賬戶類對象和用戶類對象,
 并設置信息,與顯示信息*/
public class Account {
    private int cid ;
    private int balance;
    private Customer customer;
 
    public Account(Customer customer) {
        this.customer = customer;
    }
 
    public int getCid() {
        return cid;
    }
 
    public void setCid(int cid) {
        this.cid = cid;
    }
 
    public int getBalance() {
        return balance;
    }
 
    public void setBalance(int balance) {
        this.balance = balance;
    }
    public Account(String s, int balance, Customer customer){
 
    }
    public Account(int cid,int balance, Customer customer){
        this.cid=cid;
        this.balance=balance;
        this.customer=customer;
    }
 
    //(1)getInfo(),返回String類型,返回卡的詳細信息  號cid,余額balance,所屬用戶Customer
    public String getinfo(){
        String info = "卡號"+cid+"\n余額"+balance+"\n用戶"+customer.getName();
         return info;
    }
    //取錢方法withdraw(),參數自行設計,如果取錢成功返回true,失敗返回false
    public boolean withdraw(int out_balance)
    {
        if (out_balance <= balance)
        {
            balance -= out_balance;
            return true;
        }
 
        return false;
    }
 
    //存錢方法save(),參數自行設計,如果存錢成功返回true,失敗返回false 
    public boolean save (int in_banlance){
        if (in_banlance >=0){
            balance += in_banlance;
            return  true;
        }
        return false;
    }
 
 
}
 
package test3;
//其中Customer類有姓名、身份證號、聯系電話、家庭地址等屬性 
public class Customer {
    private String name;
    private String idcard;
    private String call;
    private String adress;
 
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String isIdcard() {
        return idcard;
    }
 
    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }
 
    public String getCall() {
        return call;
    }
 
    public void setCall(String call) {
        this.call = call;
    }
 
    public String getAdress() {
        return adress;
    }
 
    public void setAdress(String adress) {
        this.adress = adress;
    }
    public Customer(){
 
    }
    public Customer(String name, String idcard,String call,String adress){
        this.name=name;
        this.idcard=idcard;
        this.call = call;
        this.adress=adress;
    }
    public String say(){
        String info = "姓名"+name+"\n身份證號"+idcard+"\n電話"+call+"\n地址"+adress;
        return info;
    }
}

理解類中引用類就是再寫一個就行,不用想的太復雜。

坐標點

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package test2;
//定義一個類,用于描述坐標點
//
//?           0——————>X
//
//?          |
//
//?          |
//
//?          |                  P(X,Y)
//
//?          |
//
//?          |
//
//?          Y
//
//
//
//(1)具有計算當前點到原點距離的功能
//
//(2)求到任意一點(m,n)的距離
//
//(3)求到任意一點(Point p)的距離
//
//(4)具有坐標點顯示功能,顯示格式(x,y)
//
//(5)提供無參的構造器和一個有參的構造器
public class test2 {
    public static void main(String[] args) {
        point w=new point(10,20);
        w.oxy();
        w.mnxy( 66,77 );
        w.ponitp( 14,16 );
        w.show();
    }
}
public class point {
    int x ;
    int y ;
    int m ;
    int n ;
    public int getM() {
        return m;
    }
 
    public void setM(int m) {
        this.m = m;
    }
 
 
    public int getN() {
        return n;
    }
 
    public void setN(int n) {
        this.n = n;
    }
 
 
 
 
    public int getX() {
        return x;
    }
 
    public void setX(int x) {
        this.x = x;
    }
 
 
 
    public int getY() {
        return y;
    }
 
    public void setY(int y) {
        this.y = y;
    }
 public point(){
 
 }
 public point(int x , int  y){
        this.y=y;
        this.x = x;
 }
 
 public void  oxy(){
     System.out.println(Math.sqrt( x*x+y*y ));
 }
 //(2)求到任意一點(m,n)的距離
    public void mnxy (int m , int n ){
        this.m=m;
        this.n=n;
        System.out.println(Math.sqrt( ((m-x)*(m-x)+(n-y)*(n-y)) ));
    }
//(3)求到任意一點(Point p)的距離
    public void ponitp (int z , int k ){
        System.out.println(Math.sqrt( ((z-x)*(z-x)+(k-y)*(k-y)) ));
    }
 
//(4)具有坐標點顯示功能,顯示格式(x,y)
    public void show(){
        System.out.println( "("+x+","+y+")" );
    }
 
}

學生隨機數排序

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// An highlighted block
var foo = 'bar';package test1;
//定義類Student,包含三個屬性:學號number(int),年級state(int),成績 score(int)。
//創建20個學生對象,學號為1到20,年級和成績都由隨機數確定。
//問題一:打印出3年級(state值為3)的學生信息。
//問題二:使用冒泡排序按學生成績排序,并遍歷所有學生信息
//提示: 1) 生成隨機數:Math.random(),返回值類型double;  (Matn為工具類)([0,1})
//    2) 四舍五入取整:Math.round(double d),返回值類long 型
public class demo5 {
    public static void main(String[] args) {
        Students [] stu = new Students[20];
        for (int i = 0; i < stu.length; i++) {
            //給數組元素賦值
            stu[i]=new Students();
            //給Student的對象的屬性賦值
            stu[i].number = i +1;//學號
            stu[i].state = (int)(Math.random() * (6 - 1 + 1) + 1);//(6  + 1));//年級[1,6]
            stu[i].score = (int)(Math.random() *  (100 - 0 + 1));//(100 - 0 + 1));//成績[0,100]
        }
        //遍歷學生數組
        for (int i = 0; i < stu.length; i++) {
            //System.out.println(stu[i].number + "," + stu[i].state + ","
            //      + stu[i].score);
            System.out.println(stu[i].info());
        }
        System.out.println("*******************");
        //問題一:打印出3年級(state值為3)的學生信息。
        for (int i = 0; i < stu.length; i++) {
            if (stu[i].state == 3) {
                System.out.println(stu[i].info());
            }
        }
        System.out.println( "\t");
        //問題二:使用冒泡排序按學生成績排序,并遍歷所有學生信息。
        for (int i = 0; i < stu.length - 1; i++) {
            for (int j = 0; j < stu.length - 1 - i; j++) {
                if(stu[j].score > stu[j + 1].score){
                    //如果需要換序,交換的是數組的元素,Student對象!!!
                    Students temp = stu[j];
                    stu[j] = stu[j + 1];
                    stu[j + 1] = temp;
 
                }
            }
        }
        for (int i = 0; i < stu.length; i++) {
            System.out.println(stu[i].info());
        }
    }
}
public class Students {
    //學號number(int),年級state(int),成績 score(int)。
     int number;
     int state;
      int  score;
 
    public int getNumber() {
        return number;
    }
 
    public void setNumber(int number) {
        this.number = number;
    }
 
    public int getState() {
        return state;
    }
 
    public void setState(int state) {
        this.state = state;
    }
 
    public int getScore() {
        return score;
    }
 
    public void setScore(int score) {
        this.score = score;
    }
    public String info(){
        return "學號:" + number + ",年級:" + state + ",成績" + score;
    }

到此這篇關于Java 面向對象和封裝全面梳理總結的文章就介紹到這了,更多相關Java 面向對象 內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/m0_62265391/article/details/120965410

延伸 · 閱讀

精彩推薦
  • Java教程5種必會的Java異步調用轉同步的方法你會幾種

    5種必會的Java異步調用轉同步的方法你會幾種

    這篇文章主要介紹了5種必會的Java異步調用轉同步的方法你會幾種,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,...

    程序員Sunny11062021-11-19
  • Java教程Java Web使用POI導出Excel的方法詳解

    Java Web使用POI導出Excel的方法詳解

    這篇文章主要介紹了Java Web使用POI導出Excel的方法,結合實例形式詳細分析了Java Web使用POI導出Excel的具體操作步驟、實現技巧與相關注意事項,需要的朋友可以...

    qq73422725532020-11-04
  • Java教程使用SpringBoot內置web服務器

    使用SpringBoot內置web服務器

    這篇文章主要介紹了使用SpringBoot內置web服務器操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教...

    喜歡火影的木易楊10522022-01-19
  • Java教程聊聊Spring Cloud Cli 初體驗

    聊聊Spring Cloud Cli 初體驗

    這篇文章主要介紹了聊聊Spring Cloud Cli 初體驗,SpringBoot CLI 是spring Boot項目的腳手架工具。非常具有實用價值,需要的朋友可以參考下...

    polly12082021-04-20
  • Java教程解決RestTemplate加@Autowired注入不了的問題

    解決RestTemplate加@Autowired注入不了的問題

    這篇文章主要介紹了解決RestTemplate加@Autowired注入不了的問題,具有很好的參考價值,希望對大家有所幫助。...

    夢想注定是孤獨的旅行4722021-11-25
  • Java教程Java動態數組Arraylist存放自定義數據類型方式

    Java動態數組Arraylist存放自定義數據類型方式

    這篇文章主要介紹了Java動態數組Arraylist存放自定義數據類型方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教...

    「已注銷」8282022-03-02
  • Java教程詳解SpringCloud Config配置中心

    詳解SpringCloud Config配置中心

    這篇文章主要介紹了詳解SpringCloud Config配置中心,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧...

    liuhh6002021-07-20
  • Java教程java實現模擬進度計量器

    java實現模擬進度計量器

    這篇文章主要為大家詳細介紹了java實現模擬進度計量器,模擬血壓計實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參...

    Freedoman19904532020-07-17
主站蜘蛛池模板: 火影忍者小南裸羞羞漫画 | 国产成人久久久精品一区二区三区 | 国产一级黄色网 | 亚洲精品AV无码永久无码 | 视频大全在线观看免费 | 久久精品国产亚洲AV热无遮挡 | 欧美一区二区三区免费高 | 国产91对白在线观看 | 娇妻被老外疯狂调教 | 四虎影库紧急大通知 | 贰佰麻豆剧果冻传媒一二三区 | 亚洲欧美日韩久久一区 | 国产一区二区精品 | 青青草99热这里都是精品 | 人人澡人 | 香蕉国产成版人视频在线观看 | 四虎成人4hutv影院 | 国产日韩欧美成人 | 亚洲AV 中文字幕 国产 欧美 | 精品无码国产污污污免费网站2 | 亚州人成网在线播放 | 国产精品手机视频一区二区 | 国产激情影院 | 激情五色月 | 含羞草传媒每天免费一次破解 | yy6080午夜国产免费福利 | 亚瑟天堂久久一区二区影院 | 免费一级黄 | 性色AV一区二区三区V视界影院 | 日本成人黄色片 | 欧美三茎同入 | 无颜之月5集全免费看无删除 | 男人搡女人视频免费看 | 成人福利影院 | 精品国产91高清在线观看 | 四虎影院永久在线 | 国产综合色在线视频区色吧图片 | 五月天综合久久 | 日韩一卡2卡3卡新区网站 | 精品高潮呻吟99AV无码 | 91私密保健女子养生spa |