欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 面向?qū)ο蠛头庋b全面梳理總結(jié)

 更新時間:2021年10月29日 10:06:27   作者:代碼小k  
面向?qū)ο竽耸荍ava語言的核心,是程序設(shè)計的思想,在面向?qū)ο蟪淌皆O(shè)計方法中,封裝(英語:Encapsulation)是指一種將抽象性函式接口的實現(xiàn)細(xì)節(jié)部分包裝、隱藏起來的方法。封裝可以被認(rèn)為是一個保護(hù)屏障,防止該類的代碼和數(shù)據(jù)被外部類定義的代碼隨機(jī)訪問

關(guān)于面向?qū)ο蠛头庋b的個人理解

類和對象

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

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

二者關(guān)系:類為屬性行為抽象,對象則為實體。

對象內(nèi)存圖理解:堆內(nèi)存開辟空間,成員變量出現(xiàn) 并產(chǎn)生默認(rèn)初始化值,將對象地址值記錄以便于通過對象名調(diào)用成員變量。

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

封裝

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

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

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

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

難題匯總

銀行賬戶

package test3;

public class bank {
    public static void main(String[] args) {
        //在測試類Bank中創(chuàng)建銀行賬戶類對象和用戶類對象,
        // 并設(shè)置信息,與顯示信息
        Customer customer = new Customer("李華","123456789","987456321","新華小區(qū)");
        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類型,返回卡的詳細(xì)信息
(2)取錢方法withdraw(),參數(shù)自行設(shè)計,如果取錢成功返回true,失敗返回false
(3)存錢方法save(),參數(shù)自行設(shè)計,如果存錢成功返回true,失敗返回false  
 其中Customer類有姓名、身份證號、聯(lián)系電話、家庭地址等屬性    Customer類有方法say(),
 返回String類型,返回他的個人信息。​在測試類Bank中創(chuàng)建銀行賬戶類對象和用戶類對象,
 并設(shè)置信息,與顯示信息*/
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類型,返回卡的詳細(xì)信息  號cid,余額balance,所屬用戶Customer
    public String getinfo(){
        String info = "卡號"+cid+"\n余額"+balance+"\n用戶"+customer.getName();
         return info;
    }
    //取錢方法withdraw(),參數(shù)自行設(shè)計,如果取錢成功返回true,失敗返回false
    public boolean withdraw(int out_balance)
    {
        if (out_balance <= balance)
        {
            balance -= out_balance;
            return true;
        }

        return false;
    }

    //存錢方法save(),參數(shù)自行設(shè)計,如果存錢成功返回true,失敗返回false  
    public boolean save (int in_banlance){
        if (in_banlance >=0){
            balance += in_banlance;
            return  true;
        }
        return false;
    }


}

package test3;
//其中Customer類有姓名、身份證號、聯(lián)系電話、家庭地址等屬性  
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;
    }
}

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

坐標(biāo)點

package test2;
//定義一個類,用于描述坐標(biāo)點
//
//​           0——————>X
//
//​          |
//
//​          |
//
//​          |                  P(X,Y)
//
//​          |
//
//​          |
//
//​          Y
//
//
//
//(1)具有計算當(dāng)前點到原點距離的功能
//
//(2)求到任意一點(m,n)的距離
//
//(3)求到任意一點(Point p)的距離
//
//(4)具有坐標(biāo)點顯示功能,顯示格式(x,y)
//
//(5)提供無參的構(gòu)造器和一個有參的構(gòu)造器
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)具有坐標(biāo)點顯示功能,顯示格式(x,y)
    public void show(){
        System.out.println( "("+x+","+y+")" );
    }

}

學(xué)生隨機(jī)數(shù)排序

// An highlighted block
var foo = 'bar';package test1;
//定義類Student,包含三個屬性:學(xué)號number(int),年級state(int),成績 score(int)。
//創(chuàng)建20個學(xué)生對象,學(xué)號為1到20,年級和成績都由隨機(jī)數(shù)確定。
//問題一:打印出3年級(state值為3)的學(xué)生信息。
//問題二:使用冒泡排序按學(xué)生成績排序,并遍歷所有學(xué)生信息
//提示: 1) 生成隨機(jī)數(shù):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++) {
            //給數(shù)組元素賦值
            stu[i]=new Students();
            //給Student的對象的屬性賦值
            stu[i].number = i +1;//學(xué)號
            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]
        }
        //遍歷學(xué)生數(shù)組
        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)的學(xué)生信息。
        for (int i = 0; i < stu.length; i++) {
            if (stu[i].state == 3) {
                System.out.println(stu[i].info());
            }
        }
        System.out.println( "\t");
        //問題二:使用冒泡排序按學(xué)生成績排序,并遍歷所有學(xué)生信息。
        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){
                    //如果需要換序,交換的是數(shù)組的元素,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 {
    //學(xué)號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 "學(xué)號:" + number + ",年級:" + state + ",成績" + score;
    }



到此這篇關(guān)于Java 面向?qū)ο蠛头庋b全面梳理總結(jié)的文章就介紹到這了,更多相關(guān)Java 面向?qū)ο?內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論