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


猜你喜欢
- import java.util.ArrayList;import java.util.HashMap;import java.util.I
- C# winform 新手实例:点击生成二维码效果图如下:打开exe默认界面部分代码://button点击事件private void bu
- 简介通过 pulsar-flink-connector 读取到 Apache pulsar 中的namespaces、topics的元数据信
- 为什么会有常量池的概念?不知道小伙伴们是否有思考过这个问题? 没有思考也无所谓,小编在这里类比一下,大家就会清晰了。什么是池? 我们听的最多
- 什么是注解?对于很多初次接触的开发者来说应该都有这个疑问?Annontation是Java5开始引入的新特征,中文名称叫注解。它提供了一种安
- ES是一个基于Lucene的分布式全文搜索服务器,和SQL Server的全文索引(Fulltext Index)有点类似,都是基于分词和分
- 本文实例讲述了Android播放器MediaPlayer实现均衡器效果。分享给大家供大家参考,具体如下:这几天在系统学习Android官方A
- 闲话不多说,直接上图。给大家讲讲我的编程思想吧。第一部分:沉浸式状态栏(API-Level 19, Android4.4 KitKat 之后
- 碎片,它的出现是为了更好展示UI的设计,让程序更加得到充分的展示。Fragment的出现,如微信的额主界面包含多个Fragment,使得微信
- 据JDK5的新特性,用For循环Map,例如循环Map的Keyfor(String dataKey : paraMap.keySet())&
- 今天看《第一行代码》上面关于拍照和从相册选取图片那一部分,发现始终出不来效果,所以搜索其他资料学习一下相关知识,写一个简单的Demo。&nb
- 最好使用英文,不要用汉语拼音1:包(package):用于将完成不同功能的类分门别类,放在不同的目录(包)下,包的命名规则:将公司域名反转作
- 首先要安装SpeechSDK5.1 开发包和SpeechSDK5.1 Langague Pack(中英文) 语言包,不过VS2010里是自带
- 先看一下java线程运行时各个阶段的运行状态线程是进程中的一个实体,是被系 * 立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点在运
- 前言对于信息的加密方式多种多样,之前为大家介绍了一种自己设计的加密方式,有兴趣的朋友可以欣赏一下,欢迎给予指点。今天为大家介绍一下对称加密方
- 1、背景本系统调用外围系统接口(http+json),但是发现有时外围系统服务不太稳定,有时候会出现返回一串xml或者gateway bad
- 首先感谢:http://www.codeproject.com/Articles/25487/Cryptographic-Interoper
- /** * 日期工具类 * 默认使用 "yyyy-MM-dd HH:mm:ss" 格式化日期&nbs
- 本文介绍 Spring Boot 项目中整合 ElasticSearch 并实现 CRUD 操作,包括分页、滚动等功能。之前在公司使用 ES
- 前言Android暂时还没有提供一个合适的API来获取/监听键盘的状态和高度 , 而我们又经常会有这个需求.最近我的一个项目中,在ugc页面