当前位置:网站首页>Encapsulation - Super keyword
Encapsulation - Super keyword
2022-07-29 06:18:00 【Plum juice】
Catalog
this And super The difference between :
encapsulation :
Concept
Encapsulation is to encapsulate abstract data attributes and operation methods of data , The data is inside by white tiger , Other parts of the program can operate on data only by authorized operation methods .
benefits
1) Hide implementation details
2) You can validate the data , Make sure it's safe and reasonable
step
1) Property privatization
2) Public (public)set,get Method , Used to judge and assign values to attributes
public class test {
public static void main(String[] args) {
Person person = new Person();
person.setAge(30);
System.out.println(person.info());
// If we use constructors to specify properties ourselves
Person smith = new Person(30);
System.out.println("====smith Information about ======");
System.out.println(smith.info());
}
}
class Person {
private int age; //age Privatization
public void say(int n) {
}
public Person() {
}
// Constructor with three properties
public Person(int age) {
// this.age = age;
// We can set The method is written in the constructor , This can still verify
setAge(age);
}
public int getAge() {
return age;
}
public void setAge(int age) {
// Judge
if (age >= 1 && age <= 120) {// If it's a reasonable range
this.age = age;
} else {
System.out.println(" You set the wrong age , Need to be in (1-120), Give the default age 18 ");
this.age = 18;// Give a default age
}
}
// Write a method , Return attribute information
public String info() {
return " age=" + age;
}
}super keyword
1. Basic introduction
super The reference representing the parent class , Used to access the properties of the parent class 、 Method 、 Constructors 2. Basic grammar
1) Access the properties of the parent class , But not visit Of the parent class private attribute
super. Property name ;
2) Methods to access the parent class , But you can't access the parent's private Method
super. Method name ( parameter list );
3) Access the constructor of the parent class
super( parameter list ); Only in the first sentence of the constructor
3.super Convenience
1) Call the constructor of the parent class , A clear division of responsibilities , The parent class property is initialized by the parent class , Subclass properties are initialized by subclasses
2) When a subclass has members in the parent class ( Properties and methods ) When you have two names , To access members of the parent class , Must pass super. If there are no duplicate names , Use super,this, Direct access has the same effect
// call cal Method
package super_;
public class Base {
public void cal() {
System.out.println("Base Class cal() Method ...");
}
}
//A None in class cal Method
public void sum() {
System.out.println("B Class sum()");
// Subclass B No, cal Method , So I can use the following three ways
// look for cal When the method is used (cal() and this.cal()), The order is :
// (1) Find this class first , If there is , Call
// (2) without , Then find the parent class ( If there is , And can call , Call )
// (3) If the parent class doesn't have , Then continue to find the parent of the parent class , The whole rule , It's the same , until Object class
// If in the process of finding a method , eureka , But there is no access , False report , cannot access
// If in the process of finding a method , Can't find , The prompt method does not exist
//cal();
this.cal(); // Equivalent cal();
// look for cal Method (super.call()) The order is to find the parent class directly , The other rules are the same
//super.cal();
//n1 and this.n1 The rule of search is
//(1) Find this class first , If there is , Call
//(2) without , Then find the parent class ( If there is , And can call , Call )
//(3) If the parent class doesn't have , Then continue to find the parent of the parent class , The whole rule , It's the same , until Object class
// Tips : If in the process of finding properties , eureka , But there is no access , False report , cannot access
// If in the process of finding properties , Can't find , The prompt property does not exist
System.out.println(n1);
System.out.println(this.n1);///888
// look for n1 (super.n1) The order is to find the parent class attribute directly , The other rules are the same
System.out.println(super.n1);//999
}
package super_;
public class Test {
public static void main(String[] args) {
B b = new B();
b.sum();
}
}
this And super The difference between :

边栏推荐
- crawl笔记
- HR must ask questions - how to fight with HR (collected from FPGA Explorer)
- Rowkey设计
- 【软件工程之美 - 专栏笔记】21 | 架构设计:普通程序员也能实现复杂系统?
- Reading papers on false news detection (5): a semi supervised learning method for fake news detection in social media
- SimpleFOC+PlatformIO踩坑之路
- 顺序表和链表
- 基于wifi的温度采集与控制系统
- 【RoboMaster】从零开始控制RM电机(2)-CAN通信原理及电调通信协议
- CV520国产替代Ci521 13.56MHz 非接触式读写器芯片
猜你喜欢
随机推荐
Reading papers on false news detection (5): a semi supervised learning method for fake news detection in social media
动态加载数据
倾角传感器用于通信铁塔、高压电塔长期监测
利用云打码来破解登录遇到验证码的问题
TB6600+stm32F407测试
唯美girls
太原市公交路线爬取
Ml8 self study notes LDA principle formula derivation
Design and implementation of QT learning notes data management system
封装——super关键字
八大排序-----------------堆排序
低功耗蓝牙5.0芯片nrf52832-QFAA
无符号右移
JUC concurrent knowledge points
传统模型预测控制轨迹跟踪——波浪形轨迹(功能包已经更新)
STM32 MDK(Keil5) Contents mismatch错误总结
SimpleFOC+PlatformIO踩坑之路
QT learning notes - Import and export of Excel
ML7 self study notes
CS4344国产替代DP4344 192K 双通道 24 位 DA 转换器









