当前位置:网站首页>Create object memory analysis and inheritance
Create object memory analysis and inheritance
2022-04-22 10:58:00 【Lei Feng】
package com.rgf.oop.Demo04;
public class Application {
public static void main(String[] args) {
Pet dog = new Pet();
dog.name=" Wangcai ";
dog.age=3;
dog.shout();
System.out.println(dog.name);
System.out.println(dog.age);
}
}
package com.rgf.oop.Demo04;
public class Pet {
public String name;
public int age;
// No arguments structure
public void shout(){
System.out.println(" Let out a cry ");
}
}
We use these two pieces of code to analyze the memory at the moment :
Memory is divided into stacks and heaps , There is also a method area in the pile .
The first step is , We will reduce the Application Some code information of this class is put into memory , That is, load... In the method area in the heap Application class , This class has main() Method , And some strings , We generally call it a constant . There is usually a constant pool , There are some constants . for example : Constant pool : Wangcai
Then we carry out main Method time ,main The method is at the bottom of the stack , We execute main Method time , We want to new One class , It's called Pet, We're going to create this class Pet, This class has name and age These two fields , There's another way called shout( ). We have Pet After this class , We carry out new In this class , We enter Pet This class .new The generated object name is in the stack ( Reference variable name ), And the real object is in the heap .
In the pile, what we new Class Pet, There will be a memory address , We assume that 0x0001, That is, the memory address of the object . And there is a default name=null,age=0, There's another way shout(), This method is new What is called is the method area in the heap Pet Of shout() Method .
After completing the above, we succeeded new Come out with an object , Then we'll assign values to these objects
And the method area in the heap also has a static method area static, The methods in this are loaded together with the class , All objects can use this static method .
In the stack ,main() Method is pushed into the bottom of the stack , Then we call other methods , After other methods are called ,main() Method pop up , Program end .
Summary of classes and objects :
package com.rgf.oop.Application;
public class Application {
/*
1. Classes and objects
Class is a template : abstract , Object is a concrete instance
2. Method
Definition 、 call
3. References to objects
Reference type : Basic types (8)
Objects are manipulated by reference : Stack ( Reference object )-----> Pile up ( Real object , Address )
4. attribute : Field Field Member variables
Default initialization :
Numbers : 0 0.0
char:u0000
boolea:false
quote :null
Definition of attribute :
Modifier Attribute types Property name = Property value !
5. Method : Define and call
6 . Object creation and use :
(1) You have to use new Keywords create objects , Constructors Person rgf=new Person();
(2) Object properties rgf.name;
(3) Object method rgf.sleep();
7. class :
Static properties attribute
Dynamic behavior Method
encapsulation 、 Inherit 、 polymorphic
*/
}
encapsulation : The dew of the dew , What should be hidden
Our programming should pursue “ High cohesion , Low coupling ”. High cohesion means that the internal data operation details of a class are completed by themselves , External interference is not allowed ;
Low coupling : Only a few methods are exposed for external use .
encapsulation ( Data hiding )
Usually , Direct access to the actual representation of data in an object should be prohibited , It should be accessed through the operation interface , This is called information hiding .
Just remember this sentence : Property private ,get/set
The concrete embodiment of encapsulation thought :
package com.rgf.oop.Demo05;
/*
1. Improve the security of the program , Protection data .
2. Hide the implementation details of the code
3. Unified interface
4. Improve the maintainability of the system
*/
public class Application {
public static void main(String[] args) {
Student stu1 = new Student();
stu1.getId();
stu1.getAge();
// stay java in , If you judge whether the two methods of a class are the same ,
// There are two main references , Method name and parameter list , If the method name is the same , The method must be the same ,
// If the parameter list is the same , Then it must be the same way .
stu1.setName("rgf");
stu1.setId(5);
stu1.setAge(5);
//stu1.setAge(999); // illegal
System.out.println(stu1.getAge()); //println Method overloading
System.out.println(stu1.getName());
System.out.println(stu1.getId());
}
}
package com.rgf.oop.Demo05;
// class private: Private access
public class Student {
// Property private
private String name; // name
private int id; // Student number
private char sex;// Gender
private int age;// Age
// Provide some methods to manipulate this property !
// Provide some public Of get、set Method .
//get Get this data
public String getName() {
return this.name;
}
public int getId() {
return id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 120 || age < 0) {
this.age = 3;
} else {
this.age = age;
}
}
//set Set a value for this data
public void setName (String name){
this.name = name;
}
public void setId ( int id){
this.id = id;
}
}
The operation interface is as follows :

We can take advantage of IDEA Conduct set and get Method implementation :
1. Right click , choice Generate.

2. Click on Generate after , Click on Getter and Setter.

3. Select the... You want to create get perhaps set Properties of method .

版权声明
本文为[Lei Feng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221055150485.html
边栏推荐
- 重点来了,最全Web3行话指南你掌握了吗?
- Pytorch semantic segmentation total convolution network
- Mina中的VRF
- 图解文件系统——我见过讲文件系统讲的最好的文章了
- How does the project solve cross domain problems
- 使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群
- Network security -- attack defense confrontation
- 阿里超大规模 Flink 集群运维体系介绍
- 中职网络空间安全技能大赛P100-使用内存取证技术追踪Meterpreter会话
- 基于PyQt5实现数据动态可视化
猜你喜欢
![[leetcode] the first mock exam of two tree traversal iteration method.](/img/2a/4fb0de0f001738729a0051c14a1e5d.jpg)
[leetcode] the first mock exam of two tree traversal iteration method.

点云配准(一)— ICP方法

C language advanced level 1 ------ "data storage (original code inverse code complement + size end judgment + integer promotion + floating point number storage)

创建对象内存分析与继承

postman接口测试工具视频教程,零基础入门到高手毕业

Graphic file system - I've seen the best article on file system

Qdebug() print debugging information

msfvenom --- msf 组件 shell生成工具

P100 - clue penetration test using searchsploit

MySql5.7.26安装
随机推荐
MySQL basic collection
Pytorch semantic segmentation total convolution network
824. 山羊拉丁文 / 剑指 Offer II 012. 左右两边子数组的和相等
2022年中职组“网络空间安全”广东省竞赛 赛项规程
Function, purpose and implementation of API gateway
Symfony3. 4. The database reverse generation entity has been solved
C language example 100 (IV)
【SQL server速成之路】数据库的查询
uboot目录结构分析
Mina中的VRF
Oracle account is locked. Unlocking method
360 released the annual report: the operating revenue was about 10.886 billion yuan, and the security business increased by more than 70%
Php get IP Limits submissions in minutes for verification code Getting too frequently
Que sait la commande ADB? Les ordres de la BAD arrivent.
大家都在努力,你凭什么不努力
P100 - clue penetration test using searchsploit
110T oracle故障恢复
Batch update software and security optimization (taking openssh as an example) -- the road of building a dream
Addition, deletion, modification and query of advanced MySQL data (DML)
ffmpeg filtergraph同时添加缩放和osd滤镜