面向物件和封裝的個人理解和難題
- 關于面向物件和封裝的個人理解
- 類和物件
- 封裝
- 難題匯總
- 銀行賬戶
- 坐標點
- 學生亂數排序
關于面向物件和封裝的個人理解
類和物件
類:對事物的一種描述(具有共同屬性和行為的事物的抽象),例如手機,屬性:品牌價格,行為:玩游戲,刷vx;
物件:客觀存在(在java中體現就是mian方法里面用類定義一個物件,然后用物件去呼叫方法或者呼叫成員變數)
二者關系:類為屬性行為抽象,物件則為物體,
物件記憶體圖理解:堆記憶體開辟空間,成員變數出現 并產生默認初始化值,將物件地址值記錄以便于通過物件名呼叫成員變數,
成員變數和區域變數的區別:類中位置不同,記憶體中位置不同,生命周期不同,初始化值不同(成員變數(有默認初始化值)區域變數(沒有默認初始化值,必須先定義,賦值才能使用),
封裝
private關鍵字:被private修飾的成員,只能在本類進行訪問,針對private修飾的成員變數,如果需要被其他類使用,提供相應的操作(get,set方法)
this關鍵字:this修飾的變數用于指代成員變數,其主要作用是(區磁區域變數和成員變數的重名問題),
封裝理解: 將類的某些資訊隱藏在類內部,不允許外部程式直接訪問,而是通過該類提供的方法來實作對隱藏資訊的操作和訪問(private修飾和get,set方法)
封裝的好處以及作用: 把代碼用方法進行封裝,提高了代碼的復用性, 通過方法來控制成員變數的操作,提高了代碼的安全性,
難題匯總
銀行賬戶
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;
}
}
理解類中參考類就是再寫一個就行,不用想的太復雜,
坐標點
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+")" );
}
}
學生亂數排序
// 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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/337681.html
標籤:java
