• JAVA基础—面向对象


    1、面向对象介绍

    2、类和对象

    2.1、如何定义类、得到类的对象、使用对象

    在这里插入图片描述

    2.2、类和对象的总结

    在这里插入图片描述

    2.3、定义类的补充事项—测试类与Javabean类

    在这里插入图片描述
    在这里插入图片描述
    成员变量一般无需指定初始值,存在默认值。
    但是局部变量必须定义初始值。

    成员变量的默认值:

    2.4、定义类的注意事项

    在这里插入图片描述

    2.4.1、驼峰模式

    单词之间不以空格、连接号或者底线连结(例如不应写成:camel case、camel-case或camel_case形式)。共有两种格式:
    1、小驼峰式命名法(lower camel case):
    第一个单词以小写字母开始,第二个单词的首字母大写。例如:firstName、lastName。
    2、大驼峰式命名法(upper camel case):
    每一个单词的首字母都采用大写字母,例如:FirstName、LastName、CamelCase,也被称为 Pascal 命名法。

    3、封装

    3.1、封装的意义

    在这里插入图片描述

    4、就近原则和this关键字

    4.1、this的内存原理

    作用:区分局部变量和成员变量,加this调用的是成员变量(即方法外的变量)。
    本质:this调用的是地址值。
    在这里插入图片描述

    4.2、用法

    https://blog.csdn.net/qq_56535449/article/details/125628761

    public class Person{
    	public String name;
    	public int age;
    	public double height;
    
    
    **代码一:**
    不用this关键字,直接使用成员变量。
    	public Person(String name,int a,double height){
    		name=name;
    		age=age;
    		height=height;
    	}
    
    	public void introduce(){
    	System.out.println("我叫"+name+",今年"+age+"岁");
    	}
    }
    public static void main(String[] args){
    		Person person = new Person("张三",20,178.5;
    		person.introduce();
    }
    运行结果
    我叫null,今年0**代码二:**
    使用this关键字
    public Person(String name,int age,double height){
    	this.name=name;
    	this.age=age;
    	this.height=height;
    }
    public static void main(String[] args){
    		Person person = new Person("张三",20,178.5;
    		person.introduce();
    }
    运行结果:
    我叫张三,今年20
    • 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

    一、this.属性名

    根据“同名情况下,局部变量的优先级更高”原则
    例子:

    public class Person{
    	public String name;
    	public int age;
    	public double height;
    	
    	public void introduce(){
    	System.out.println("我叫"+name+",今年"+age+"岁");
    	}
    	
    	//构造器中加上this关键字
    	public Person(String name,int age,double height){
    		this.name=name;
    		this.age=age;
    		this.height=height;
    	}
    	public static void main(String[] args){
    			Person person = new Person("张三",20,178.5;
    			person.introduce();
    	}
    	
    运行结果:
    我叫张三,今年20
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    二、this.方法名

    public class Person{
    	public String name;
    	public int age;
    	public double height;
    	
    	public Person(String name,int a,double height){
    		name=name;
    		age=age;
    		height=height;
    	}
    	
    	//增加一个方法
    	public void greet(){
    	System.out.println("hello,大家好");
    	}
    	
    	public void introduce(){
    		//在introduce方法中并没有出现其他对象,所以方法名前面的this关键字也可以省略不写。
    		this.greet();
    		System.out.println("我叫"+name+",今年"+age+"岁");
    	}
    
    	public static void main(String[] args){
    			Person person = new Person("张三",20,178.5;
    			person.introduce();
    	}
    	
    hello,大家好
    我叫张三,今年20
    • 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

    三、this关键字用法三

    在this关键字的后面加上小括号,这样就表示调用了某个类自身的构造方法

    public class Person{
    	public String name;
    	public int age;
    	public double height;
    	
    	//构造方法1
    	public Person(String name,int age,double height){
    		this(name,age);//调用构造方法2
    		this.height=height;
    	}
    	
    	//构造方法2
    	public Person(String name,int age){
    		this.name=name;
    		this.age=age;
    	}
    
    	
    	//增加一个方法
    	public void greet(){
    	System.out.println("hello,大家好");
    	}
    	
    	public void introduce(){
    		//在introduce方法中并没有出现其他对象,所以方法名前面的this关键字也可以省略不写。
    		this.greet();
    		System.out.println("我叫"+name+",今年"+age+"岁");
    	}
    
    	public static void main(String[] args){
    			Person person = new Person("张三",20,178.5;
    			person.introduce();
    	}
    	
    hello,大家好
    我叫张三,今年20
    • 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

    在这里插入图片描述

    5、构造方法

    构造方法的主要作用就是为对象成员变量赋初始值

    5.1、构造方法的格式:

    每一个类中,会有两种构造方法:空参构造(系统会自动构造)和带参构造(所有参数都需构造)
    在这里插入图片描述

    5.2、构造方法注意事项

    在这里插入图片描述

    5.3、构造方法总结

    在这里插入图片描述

    6、标准的javabean类

    成员变量使用private修饰

    在这里插入图片描述

    7、三种情况的对象内存图???

    8、基本数据类型和引用数据类型

    8.1、数据类型及其默认值

    在这里插入图片描述

    8.2、从内存角度区分基本数据类型和引用数据类型

    在这里插入图片描述

    9、this的内存原理

    在这里插入图片描述

    10、成员和局部

    10.1、成员变量和局部变量的区别

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    hdfs集群数据迁移/DataNode节点维护/集群重命名--小结
    2022.11.10 vs每次都需要重新配置环境解决方案
    JavaScript---DOM---DOM简介、获取元素、事件基础、操作元素---11.5
    7. RabbitMQ之延时队列
    来自北大算法课的Leetcode题解:69. x的平方根
    【前端设计模式】之模版方法模式
    金仓数据库 KingbaseES 插件dbms_session
    面向OLAP的列式存储DBMS-7-[ClickHouse]的常用DML操作
    【Midjourney入门教程4】与AI对话,写好prompt的必会方法
    gazebo中给机器人添加16线激光雷达跑LIO-SAM
  • 原文地址:https://blog.csdn.net/Fighting0429/article/details/127889980