软件编程
位置:首页>> 软件编程>> java编程>> Java 面向对象和封装全面梳理总结

Java 面向对象和封装全面梳理总结

作者:代码小k  发布时间:2023-10-16 08:20:27 

标签:Java,面向对象,封装

关于面向对象和封装的个人理解

类和对象

类:对事物的一种描述(具有共同属性和行为的事物的抽象),例如手机,属性:品牌价格,行为:玩游戏,刷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://blog.csdn.net/m0_62265391/article/details/120965410

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com